-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorial-digit-sum.js
43 lines (34 loc) · 965 Bytes
/
factorial-digit-sum.js
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
32
33
34
35
36
37
38
39
40
41
42
43
/*
n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!
*/
function factorial(num) {
let ans = num;
for (let i = 2; i < num; i++) {
ans *= i;
}
console.debug(ans);
return ans;
}
function hawler (num) {
let str = num.toString();
let ary = str.split("");
return ary;
}
function brawler (ary) {
let num = 0;
for (let i = 0; i < ary.length; i++) {
num += Number(ary[i]);
}
return num;
}
function calc (num) {
let a = factorial(num);
let b = hawler(a);
let c = brawler(b);
return c;
}
console.log(calc(10));
//console.log(brawler("93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000".split("")));