summaryrefslogtreecommitdiffstats
path: root/01-algorithmic_toolbox/01-intro/03-gcd/gcd.cpp
blob: fc1339275fb767120175031ab47a994ce818be5e (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
#include <iostream>

int gcd(int a, int b)
{
    int d, r;

    if ((a == b) || (a == 0))
        return a;

    if (b == 0)
        return b;

    if (b > a) {
        d = a;
        a = b;
        b = d;
    }

    for(;;) {
        d = a / b;
        r = a - (d * b);
        if (r == 0)
            break;
        a = b;
        b = r;
    }

    return b;
}

int main() {
  int a, b;
  std::cin >> a >> b;
  std::cout << gcd(a, b) << std::endl;
  return 0;
}