forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcd.cpp
executable file
·44 lines (35 loc) · 861 Bytes
/
gcd.cpp
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
#include <cstdlib>
#include <iostream>
bool PromptForValues(int& num1, int& num2);
int gcd(int a, int b);
int gcd_impl(int a, int b);
int main() {
int num1;
int num2;
while (PromptForValues(num1, num2)) {
std::cout << "GCD(" << num1 << "," << num2 << ") = " << gcd(num1, num2) << '\n';
}
return 0;
}
bool PromptForValues(int& num1, int& num2) {
std::cout << "Enter two numbers: ";
return static_cast<bool>(std::cin >> num1 >> num2);
}
int gcd(int a, int b) {
int absA = std::abs(a);
int absB = std::abs(b);
return (absA > absB) ? gcd_impl(absA, absB) : gcd_impl(absB, absA);
}
int gcd_impl(int a, int b) {
if (b == 0) {
return a;
}
if (b == 1) {
return 1;
}
int remainder = a % b;
if (remainder == 0) {
return b;
}
return gcd_impl(a, remainder);
}