Skip to content

Instantly share code, notes, and snippets.

@p13i
Last active September 3, 2015 02:16
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 p13i/2b95b80ead8498928a81 to your computer and use it in GitHub Desktop.
Save p13i/2b95b80ead8498928a81 to your computer and use it in GitHub Desktop.
import math
def GCD(a , b):
if(b == 0):
return a
return GCD(b, a - math.floor(a/b)*b)
# Input values here and run
print(GCD(9876, 54321))
def bea(a,b): # "bea" = "backwards Euclidean Algorithm"
if b == 0:
return [1,0]
else:
x, y = bea(b, a % b)
return [y, x - a//b * y] # "//" operator is floor (i.e. rounding down)
print(bea(12345,67890))
# Will return solution [x, y]
# in the form of ax + by = gcd(a,b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment