aboutsummaryrefslogtreecommitdiff
path: root/challenge-089/ash/javascript/ch-1.js
blob: c090b4480861164ad4cc1d2349ec2681451cb72d (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
// Test run:
// $ node ch-1.js 100
// 13015

let n = process.argv.length == 3 ? process.argv[2] : 3;

let s = 0;
for (let x = 1; x <= n; x++) {
    for (let y = x + 1; y <= n; y++) {
        s += gcd(x, y);
    }
}

console.log(s);

function gcd(a, b) {
    while (b) {
        let t = b;
        b = a % b;
        a = t;
    }

    return a;
}