|
11 | 11 | * Find the sum of the digits in the number 100!
|
12 | 12 | */
|
13 | 13 |
|
14 | | -const factorialDigitSum = function (n = 100) { |
15 | | - |
| 14 | +const factorialDigitSum = (n = 100) => { |
16 | 15 | // Consider each digit*10^exp separately, right-to-left ([units, tens, ...]).
|
17 | | - let digits = [1]; |
| 16 | + const digits = [1] |
18 | 17 |
|
19 | | - for (let x=2; x<=n; x++) { |
20 | | - let carry = 0; |
21 | | - for (let exp=0; exp<digits.length; exp++) { |
22 | | - const prod = digits[exp]*x + carry; |
23 | | - carry = Math.floor(prod/10); |
24 | | - digits[exp] = prod % 10; |
| 18 | + for (let x=2; x<=n; x++) { |
| 19 | + let carry = 0 |
| 20 | + for (let exp=0; exp<digits.length; exp++) { |
| 21 | + const prod = digits[exp]*x + carry |
| 22 | + carry = Math.floor(prod/10) |
| 23 | + digits[exp] = prod % 10 |
25 | 24 | }
|
26 | 25 | while (carry > 0) {
|
27 | | - digits.push(carry%10); |
28 | | - carry = Math.floor(carry/10); |
| 26 | + digits.push(carry%10) |
| 27 | + carry = Math.floor(carry/10) |
29 | 28 | }
|
30 | 29 | }
|
31 | 30 |
|
|
0 commit comments