Last active
September 3, 2015 02:16
-
-
Save p13i/2b95b80ead8498928a81 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
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