Last active
March 11, 2019 16:57
-
-
Save yalovek/b5f928236446c5d55d162e1cea0e52c9 to your computer and use it in GitHub Desktop.
Lucky ticket
This file contains 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(count){ | |
if (count % 2 !== 0) { | |
throw Error('Must be even number'); | |
} | |
const result = {}; | |
const length = count / 2; | |
const sums = Array.from({length: Math.pow(9, length) / 2}); | |
const nums = Array.from({length: 10}); | |
const go = (l, m, n) => { | |
if (l === 0) { | |
if (Number(m) === n) { | |
result[m] = result[m] ? result[m] + 1 : 1; | |
} | |
} else { | |
for (const a in nums) { | |
go(l - 1, m, Number(n) + Number(a)); | |
} | |
} | |
}; | |
for (const x in sums) { | |
go(length, x, 0); | |
} | |
return Object.values(result).reduce((a, b) => a + Math.pow(b, 2), 0); | |
})(6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment