Skip to content

Instantly share code, notes, and snippets.

@paveleremin
Last active September 10, 2015 09:29
Show Gist options
  • Save paveleremin/b9230a042d1be1decace to your computer and use it in GitHub Desktop.
Save paveleremin/b9230a042d1be1decace to your computer and use it in GitHub Desktop.
Count lucky tickets regarding max length
var Tickets = function(maxLen){
if (maxLen % 2) {
throw Error('Max length must be even: 2, 4, 6, ...');
}
this.maxLen = parseInt((new Array(maxLen / 2 + 1)).join(9));
};
Tickets.prototype.count = function(num, undefined){
var digits = {},
i,
tmp;
for (i = 0; i <= this.maxLen; i += 1) {
tmp = i % 10 + ((i % 100) - (i % 10)) / 10 + (i / 100) ^ 0;
if (digits[tmp] === undefined) {
digits[tmp] = 1;
} else {
digits[tmp] += 1;
}
}
tmp = 0;
for (i in digits) {
tmp += digits[i] * digits[i];
}
return tmp;
};
console.log((new Tickets(2)).count()); // 10
console.log((new Tickets(4)).count()); // 670
console.log((new Tickets(6)).count()); // 55252
console.log((new Tickets(8)).count()); // 953668
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment