Skip to content

Commit 1538f41

Browse files
committed
add solution for Project Euler 020
1 parent b26baef commit 1538f41

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Project-Euler/Problem020.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Factorial digit sum
3+
4+
n! means n × (n − 1) × ... × 3 × 2 × 1
5+
6+
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
7+
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
8+
9+
Find the sum of the digits in the number 100!
10+
*/
11+
12+
const findFactorialDigitSum = (num) => {
13+
let result = 0
14+
const stringifiedNumber = factorize(num).toLocaleString('fullwide', { useGrouping: false })
15+
stringifiedNumber.split('').map(num => { result += Number(num) })
16+
return result
17+
}
18+
19+
const factorize = (num) => num === 0 ? 1 : num * factorize(num - 1)
20+
21+
console.log(findFactorialDigitSum(100))

0 commit comments

Comments
 (0)