Euclid's Algorithm
Given three integers , can you write in the form
for integers and ? If so, is there more than one solution, and what are they? Before answering this, let us answer a seemingly unrelated question:
How do you find the greatest common divisor (gcd) of two given integers ?
We denote the greatest common divisor of and by , or sometimes just if no confusion is caused. If we say and are coprime.
The obvious answer is to list all the divisors and , and look for the greatest one they have in common. However, this requires and to be factorized, and it is not known how we can do this efficiently.
Euclid's algorithm (also known as the Euclidean algorithm) finds by repeatedly doing something that most people learn in primary school: division and remainder. We give an example of how it works, and leave the proof of the general case to the reader.
Suppose we wish to compute . Then first we divide the bigger one by the smaller one:
Suppose an integer divides the left-hand side. In other words, suppose . Suppose in addition . Then in order for to divide the right-hand side we require . Hence any common divisor of and , including the greatest one, is a divisor of . Moreover, every integer that divides 6 and also divides 27 clearly must divide 33. Thus , and we have reduced the original problem to a smaller problem. We proceed in the same way; division and remainder:
and by the same argument . Lastly
So since 6 is a perfect multiple of 3, , and we have found that .
This algorithm does not involve factorizing numbers. It is quite fast. We obtain a crude bound for the number of steps required by observing that if we divide by to get , and , then in the next step we get a remainder . Thus every two steps, the numbers shrink by at least one bit.
Extended Euclidean Algorithm
The above equations actually give us more information than just the gcd of two numbers. We can use them to find integers such that
First rearrange all the equations so that the remainders are the subjects:
Then we start from the last equation, and substitute the next equation into it:
And we are done: . (If there were more equations, we would repeat this procedure, until we have used all the equations and found and .)
Thus in general, given integers and , let . Then we can find integer and such that
using the extended Euclidean algorithm.
The General Solution
We can now answer the question posed at the start of this page, that is, given integers find all integers such that
Let . Now is a multiple of for any integers , thus if is not a multiple of then there are no solutions.
So say . Using the extended Euclidean algorithm we can find such that , thus we have a solution .
Suppose is another solution. Then
Rearranging,
Since is the greatest common divisor, does not divide . But it must divide the right-hand side (since appears there) so is some multiple of , that is
for some integer . Then solving for gives
Thus and for some integer .
But if we replace with any integer, and still satisfy . Thus there are infinitely many solutions, and they are given by
for all integers .
Later, we shall often be given this case: coprime integers and are given, and we wish to solve . Then the above becomes