JavaScript Implementation of Problem 21

Includes

Problem Solution

View source code here on GitHub!

p0021()

Project Euler Problem 21

Problem:

Returns:

number --

 1/**
 2 * Project Euler Problem 21
 3 *
 4 * Problem:
 5 * @return {number}
 6*/
 7exports.p0021 = function() {
 8    let answer = 0;
 9    const toCheck = [...Array(10000).keys()];
10    for (const a of toCheck) {
11        const b = d(a);
12        if (a !== b && d(b) === a) {
13            answer += a + b;
14            if (b < 10000) {
15                toCheck.splice(toCheck.indexOf(b), 1);
16            }
17        }
18    }
19    return answer;
20};
21
22/**
23 * @param {number} i
24 * @return {number}
25*/
26function d(i) {
27    return Array.from(factors.properDivisors(i)).reduce((x, y) => x + y, 0);
28}
29
30const factors = require('./lib/factors');

Tags: divisor-sum, prime-number, factorization, divisibility, js-iterator