aboutsummaryrefslogtreecommitdiff
path: root/challenge-089/ash/rust/ch-1.rs
blob: f72d85746ab707dc70a5d91d1c7ba6e4a447606f (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
// To compile and run:
// $ rustc ch-1.rs
// $ ./ch-1 100
// 13015

use std::env;

fn gcd(a: u32, b: u32) -> u32 {
    let mut x = a;
    let mut y = b;

    while y != 0 {
        let t = y;
        y = x % y;
        x = t;
    }

    return x;
}

fn main() {
    let args: Vec<String> = env::args().collect();

    let n;
    if args.len() == 2 {
        n = args[1].parse().unwrap();
    }
    else {
        n = 3;
    }

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

    println!("{}", s);
}