JavaScript Implementation of Problem 20

View source code here on GitHub!

p0020()

Project Euler Problem 20

Problem:

Returns:

number --

 1/**
 2 * Project Euler Problem 20
 3 *
 4 * Problem:
 5 * @return {number}
 6 */
 7exports.p0020 = function() {
 8    const ten13 = 10000000000000;
 9    const numbers = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
10    for (let i = 2; i <= 100; i++) {
11        for (let j = 0; j < numbers.length; j++) {
12            numbers[j] *= i;
13        }
14        for (let j = 0; j < numbers.length - 1; j++) {
15            if (numbers[j] > ten13) {
16                numbers[j + 1] += 0 | (numbers[j] / ten13);
17                numbers[j] %= ten13;
18            }
19        }
20    }
21    let answer = 0;
22    let power = 1;
23    while (power < ten13) {
24        for (let j = 0; j < numbers.length; j++) {
25            answer += 0 | ((numbers[j] / power) % 10);
26        }
27        power *= 10;
28    }
29    return answer;
30};

Tags: large-numbers, digit-sum, factorial