The Math Forum

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Ken Starks

Posts: 20
Registered: 12/6/04
Re: ax + by = gcd(a,b)
Posted: Oct 4, 2006 4:06 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

>> 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


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, 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 )

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.