Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


Math Forum
»
Discussions
»
sci.math.*
»
sci.math
Notice: We are no longer accepting new posts, but the forums will continue to be readable.
Topic:
ax + by = gcd(a,b)
Replies:
8
Last Post:
Oct 4, 2006 4:06 PM




Re: ax + by = gcd(a,b)
Posted:
Oct 4, 2006 4:06 PM


>> Is there a general method to finding at least one solution pair x,y in >> INTEGERS such that >> ax + by = gcd(a,b) and a,b are positive integers ? >> Example: >> 686x + 511y = 7 >> or >> 1989x + 251y = 1 (picked on pupose to have gcd = 1) >>
Take Euclid's algorithm as: while a not equal to b replace the larger one by the difference
The equal value you end up with is the gcd.
The main point is that a single step is equivalent to matrix multiplication by one of
1 1 0 1
or
1 0 1 1
so if you keep track of the product of all the matrices used you can do the whole thing in one go. This also gives the solution to your problem.
In python, using 'Long integers':
def display_gcd(A,B): a, b = long(A), long(B) p, q, r, s = 1L, 0L, 0L, 1L while a != b: if a > b: a, b = a, ba p, q, r, s = pr, qs, r, s elif a < b: a, b = a, ba p,q,r,s = p,q,rp,sq
print "gcd of %s and %s = % s " % (A,B, a) print "%s = (%s x %s) + (%s x %s) % (a, p, A, q, B ) print "%s = (%s x %s) + (%s x %s) % (a, r, A, s, B )



