-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path034.js
More file actions
31 lines (25 loc) · 778 Bytes
/
034.js
File metadata and controls
31 lines (25 loc) · 778 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
31
/*
Problem 34: Digit factorials
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the numbers and the sum of the 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.
*/
const digitFactorial = () => {
let sum = 0;
const numbers = [];
const factorials = { 0: 1, 1: 1, 2: 2 };
for (let x = 10; x < 1000000; x++) {
let arr = x.toString().split('').map(Number);
let sumFactorials = 0;
arr.forEach(el => {
if (!factorials[el]) factorials[el] = factorials[el - 1] * el;
sumFactorials += factorials[el];
})
if (sumFactorials === x) {
sum += x;
numbers.push(x);
}
}
return { sum, numbers };
}
console.log(digitFactorial());