Skip to content

Instantly share code, notes, and snippets.

@yalovek
Last active March 11, 2019 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yalovek/b5f928236446c5d55d162e1cea0e52c9 to your computer and use it in GitHub Desktop.
Save yalovek/b5f928236446c5d55d162e1cea0e52c9 to your computer and use it in GitHub Desktop.
Lucky ticket
(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