Skip to content

Instantly share code, notes, and snippets.

@rtm
Last active March 24, 2016 11:03
Show Gist options
  • Save rtm/43b6e4d3d740f8a326a4 to your computer and use it in GitHub Desktop.
Save rtm/43b6e4d3d740f8a326a4 to your computer and use it in GitHub Desktop.
function *find(total, count, [val, ...remaining]) {
if (!count && Math.abs(total) < 0.001) yield [];
if (!count || total < 0 || !val) return;
for (let i = 0; i <= count; i++, total -= val)
for (let counts of find(total, count - i, remaining))
yield [i, ...counts];
}
for (let solution of find(100, 100, [3.44, 2.87, 1.99, 0.59]))
console.log(solution);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment