summaryrefslogtreecommitdiff
path: root/gcd.pas
blob: 425380716059e558633f0e017cc5e0e1399f023d (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
41
42
43
44
45
46
{
FreePascal: https://www.freepascal.org/
Tested with FreePascal 2.4.0, 3.0.0

Usage:

  $ fpc gcd.pas -ogcd-pas
  $ ./gcd-pas 44 55 66 121
  11

}

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.