Created
June 12, 2016 01:23
-
-
Save kemingy/b08f63b3cffe4b405ee2ed12bd64b569 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
def gcd(x, y): | |
if x < y: | |
x, y = y, x | |
two = 0 | |
while y > 0: | |
if x & 1 == 0 and y & 1 == 0: | |
two += 1 | |
x = x >> 1 | |
y = y >> 1 | |
elif x & 1 == 0: | |
x = x >> 1 | |
if x < y: | |
x, y = y, x | |
elif y & 1 == 0: | |
y = y >> 1 | |
else: | |
x, y = x - y, y | |
if x < y: | |
x, y = y, x | |
return x * 2 ** two |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment