Last active
March 24, 2016 11:03
-
-
Save rtm/43b6e4d3d740f8a326a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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