Last active
April 15, 2021 06:17
-
-
Save ruan4261/7c60a81656c5846c0a6e7a4ce65cf679 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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