aboutsummaryrefslogtreecommitdiff
path: root/challenge-089/ash/php/ch-1.php
blob: 9209076d610b1149210fea3fe3df62e40a047bfc (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
<?php
    // Run as:
    // $ php ch-1.php 100
    // 13015

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

        return $a;
    }

    $n = $argc == 2 ? $argv[1] : 3;

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

    echo "$s\n";

?>