aboutsummaryrefslogtreecommitdiff
path: root/challenge-008/zapwai/javascript/ch-1.js
blob: 44351de7327d86863c3caa4e36deb996cbf78ee1 (plain)
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
function is_perfect(num) {
    let divs = list_divisors(num);
    let sum = 0;
    divs.forEach(item => {
	sum += item;
    });
    return (num == sum);
}

function list_divisors(num) {
    let divs = [1];
    for (let i = 2; i <= num/2; i++) {
	if (num % i == 0)
	    divs.push(i);
    }
    return divs;
}

let perfect_numbers = [];

for (let p = 2; p <= 15; p++) {
    let num = Math.pow(2, p-1) * (Math.pow(2,p) - 1);
    if (is_perfect(num))
	perfect_numbers.push(num);
    if (perfect_numbers.length == 5)
	break;
}

console.log(perfect_numbers);