Skip to content

Instantly share code, notes, and snippets.

@ruan4261
Last active April 15, 2021 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruan4261/7c60a81656c5846c0a6e7a4ce65cf679 to your computer and use it in GitHub Desktop.
Save ruan4261/7c60a81656c5846c0a6e7a4ce65cf679 to your computer and use it in GitHub Desktop.
// better
int gcd(int x, int y) {
while (y != 0) {
int tmp = x % y;
x = y;
y = tmp;
}
return x;
}
// simple, but slow and make more stacks(in the int case, when the ratio of two numbers is close to the 1.61(Golden Ratio), up to 80(effective 40) stacks can be made)
int gcd(int x, int y) {
return x == 0 ? y : gcd(y % x, x);
}
int gcd(int large, int small) {
int temp;
for (; ; ) {
if (small > large) {
temp = large;
large = small;
small = temp;
}
if (small == 0) return large;
temp = large % small;
large = small;
small = temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment