Last active
July 3, 2016 16:00
-
-
Save yonatanmn/c537332f5962562ccd97f874f098ba13 to your computer and use it in GitHub Desktop.
create combination of exactly 10 coins to achieve requested amount (available coins: 1,2,5,10)
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
"use strict"; | |
function calc(s10, s5, s2, s1){ | |
return s10*10 + s5*5 + s2*2 + s1 | |
} | |
const resDict = {}; | |
function prepareResults() { | |
for(let s10=0;s10<=10;s10++){ | |
for(let s5=0;s5<=10;s5++){ | |
for(let s2=0;s2<=10;s2++){ | |
for(let s1=0;s1<=10;s1++){ | |
if(s10 + s5 + s2 + s1 === 10){ | |
resDict[calc(s10, s5, s2, s1)] = true; | |
} | |
} | |
} | |
} | |
} | |
} | |
function loop(n){ | |
return function (func) { | |
Array.from(Array(n).keys()).forEach(func); | |
} | |
} | |
const loop0to10 = loop(11); | |
function prepareResults2() { | |
loop0to10(s10 => { | |
loop0to10(s5 => { | |
loop0to10(s2 => { | |
loop0to10(s1 => { | |
if (s10 + s5 + s2 + s1 === 10) { | |
resDict[calc(s10, s5, s2, s1)] = true; | |
} | |
}); | |
}); | |
}); | |
}); | |
} | |
// prepareResults(); | |
prepareResults2(); | |
// console.log(resDict); | |
function check(num) { | |
return !!resDict[num]; | |
} | |
console.log(!check(9)); | |
console.log(check(10)); | |
console.log(check(58)); | |
console.log(check(95)); | |
console.log(!check(96)); | |
console.log(check(100)); | |
console.log(!check(105)); | |
// console.log(Array.from(Array(100).keys()).filter(i=>!check(i))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment