Skip to content

Instantly share code, notes, and snippets.

@kemingy
Created June 12, 2016 01:23
Show Gist options
  • Save kemingy/b08f63b3cffe4b405ee2ed12bd64b569 to your computer and use it in GitHub Desktop.
Save kemingy/b08f63b3cffe4b405ee2ed12bd64b569 to your computer and use it in GitHub Desktop.
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