-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path034.js
More file actions
30 lines (24 loc) · 668 Bytes
/
034.js
File metadata and controls
30 lines (24 loc) · 668 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
// Find the sum of all numbers which are equal to the sum of the factorial of
// their digits.
// Note: as 1! = 1 and 2! = 2 are not sums they are not included.
function fac(digit) {
var facts = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
return facts[parseInt(digit, 10)];
}
function findCuriousNumbersSum(max) {
var total = 0;
var sum, digits, i;
for (i = 10; i <= max; i++) {
sum = 0;
digits = (i+'').split('');
while (digits.length) {
sum += fac(digits.shift());
}
if (sum === i) {
total += i;
}
}
return total;
}
console.log( findCuriousNumbersSum(1000000) );