summaryrefslogtreecommitdiff
path: root/gcd.pas
blob: 20a43f7b06ed17ab7e89f1f87ee1da268bc4081d (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
{Tested with FreePascal 2.4.0}

program GCD(output);
uses sysutils; {StrToInt}

function gcd2(a: integer; b: integer): integer;
var c: integer;
begin
    while b <> 0 do
    begin
        c := b;
        b := a mod b;
        a := c;
    end;
    gcd2 := a;
end;

function gcdn(n: array of integer): integer;
var i: integer;
begin
    gcdn := n[0];
    for i := 1 to High(n) do {See also <http://wiki.freepascal.org/for-in_loop> }
        gcdn := gcd2(gcdn, n[i]);
end;


var
    n: array of integer;
    i: integer;
begin
    SetLength(n, ParamCount);
    for i := 1 to ParamCount do
        n[i-1] := StrToInt(ParamStr(i));
    Writeln(gcdn(n))
end.