aboutsummaryrefslogtreecommitdiff
path: root/challenge-089/ash/bash/ch-1.bash
blob: cb3cd93dbbe5546399ff28b8f0754ef33005fab8 (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
#!/usr/bin/bash

# Usage:
# $ bash ch-1.bash 100
# 13015
#
# N. B. Try with smaller arguments, as computations
# for the input number of 100 can take a while (~ a minute).

gcd() {
    a=$1;
    b=$2;
    while [ $b -ne 0 ]; do
        t=$b
        b=`expr $a % $b`
        a=$t
    done

    gcd=$a
}

n=${1:-3} # default is 3

s=0
x=1
while [ $x -le $n ]; do
    next=`expr $x + 1`;
    y=$next;
    while [ $y -le $n ]; do
        y=`expr $y + 1`;
        gcd $x $y;
        s=`expr $s + $gcd`;      
    done 
    x=$next;
done

echo $s