Skip to content

Instantly share code, notes, and snippets.

@gizemiskender
Created April 16, 2014 17:38
Show Gist options
  • Select an option

  • Save gizemiskender/10911862 to your computer and use it in GitHub Desktop.

Select an option

Save gizemiskender/10911862 to your computer and use it in GitHub Desktop.
#euclid recurtion
def ebob(a, b):
if b == 0:
return a
else:
return ebob(b, a % b)
print ebob(120, 54)
#euclid iteration
def ebob2(a, b):
while(b != 0):
t = a
a = b
b = t % b
return a
print ebob2(1201, 534)
# binary ebob
def ebob3(a, b):
g = 1
x = a
y = b
while(x % 2 == 0 and y % 2 == 0):
x = x / 2
y = y / 2
g = 2 * g
while(a != 0):
while(a % 2 == 0):
a = a / 2
while(b % 2 == 0):
b = b / 2
t = (a - b) / 2
if (t < 0):
t = t - 2 * t
if (a >= b):
a = t
else:
b = t
return g * b
print ebob3(12, 546)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment