Skip to content

Instantly share code, notes, and snippets.

@aadimator
Created July 19, 2016 05:51
Show Gist options
  • Select an option

  • Save aadimator/7beba9f85722ee14d3ecaf24b9acc584 to your computer and use it in GitHub Desktop.

Select an option

Save aadimator/7beba9f85722ee14d3ecaf24b9acc584 to your computer and use it in GitHub Desktop.
Greatest Common Divisor
#include <iostream>
int gcd(int a, int b) {
int current_gcd = 1;
for (int d = 2; d <= a && d <= b; d++) {
if (a % d == 0 && b % d == 0) {
if (d > current_gcd) {
current_gcd = d;
}
}
}
return current_gcd;
}
int euclidGCD (int a, int b) {
if (b == 0) return a;
return euclidGCD(b, a%b);
}
int main() {
int a, b;
std::cin >> a >> b;
// std::cout << gcd(a, b) << std::endl;
std::cout << euclidGCD(a, b) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment