summaryrefslogtreecommitdiff
path: root/gcd.php
blob: 0ab08715bea6733da7dd963e815d9bf8460eaa8f (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
<?php

function gcd2($a, $b)
{
    $c = 0;
    while ($b != 0)
    {
        $c = $b;
        $b = $a % $b;
        $a = $c;
    }
    return $a;
}

function gcd($numbers)
{
    $r = 0;
    foreach ($numbers as $n)
    {
        $r = gcd2($n, $r);
    }
    return $r;
}

print gcd(array_slice($argv, 1));

?>