summaryrefslogtreecommitdiff
path: root/gcd.d
blob: b9599e4c498c06d867878096fa3456719f18a2a6 (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
// D: https://dlang.org
//
// Synopsis:
//
// GNU D Compiler (tested GCC 12):
// $ gdc gcd.d -o gcd-d
// $ ./gcd-d 11 22 33 121
//
// LLVM D Compiler (tested LDC2 1.24.0):
// $ ldc2 --run gcd 11 22 33 121
// or
// $ ldc2 --of=gcd-d gcd.d
// $ ./gcd-d 11 22 33 121
//

import std.algorithm: map, reduce;
import std.conv: to;
import std.stdio: writeln;

ulong gcd2(ulong a, ulong b) {
  ulong c;
  while (b > 0) {
    c = b;
    b = a % b;
    a = c;
  }
  return c;
}

void main(string[] args) {
  if (args.length > 1) {
    auto gcd = args[1..$].map!(to!ulong).reduce!gcd2();
    writeln(gcd);
  }
}