-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.js
36 lines (30 loc) · 775 Bytes
/
21.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
function findFactors(number) {
let factors = [];
for (let i = 1; i < number; i++) {
if (number % i === 0) {
factors.push(i);
}
}
return factors;
}
// best practice is not always a best way
Array.prototype.sum = function () {
return this.reduce((acc, f) => acc + f, 0);
};
function main() {
let amicables = [];
for (let i = 1; i < 10000; i++) {
let factorsSum = findFactors(i).sum();
let factorSumOfFactorSum = findFactors(factorsSum).sum();
// if the pair it recursive (sum of factors of n gives back n)
// then I don't want
if (factorsSum === factorSumOfFactorSum) {
continue;
}
if (i === factorSumOfFactorSum) {
amicables.push(i);
}
}
return amicables;
}
console.log("SUM: ", main().sum());