aboutsummaryrefslogtreecommitdiff
path: root/challenge-109/abigail/node/ch-1.js
blob: 0f75cf4962de39958e5a9422e1c42842aa4b48e7 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/local/bin/node

//
// See ../README.md
//

//
// Run as: node ch-1.js [plain | compute]
//

let PLAIN   =  0
let COMPUTE =  1

let COUNT   = 20

function divisor_sum (n) {
    let sum = 0
    for (let i = 2; i <= Math . floor (n / 2); i ++) {
        if (n % i == 0) {
            sum += i
        }
    }
    return (sum)
}

let type = PLAIN
if (process . argv . length > 2 &&
    process . argv [2] == "compute") {
    type = COMPUTE
}

if (type == PLAIN) {
    console . log ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, " +
                   "0, 15, 0, 9, 8, 14, 0, 20, 0, 21")
}

if (type == COMPUTE) {
    for (let i = 1; i <= COUNT; i ++) {
        if (i > 1) {
            process . stdout . write (", ")
        }
        process . stdout . write (divisor_sum (i) . toString ())
    }
    process . stdout . write ("\n")
}