Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

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

 Messages: [ Previous | Next ]
 Ken Starks Posts: 20 Registered: 12/6/04
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

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, b-a
p, q, r, s = p-r, q-s, r, s
elif a < b:
a, b = a, b-a
p,q,r,s = p,q,r-p,s-q

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 )

Date Subject Author
9/30/06 LookSkywalker@gmail.com
9/30/06 SirKnight
9/30/06 Tim Peters
10/1/06 kilian heckrodt
10/3/06 Gerry Myerson
10/1/06 KBH
10/1/06 KBH
10/2/06 KBH
10/4/06 Ken Starks