The Euclidean Algorithm
The following is Euclidean algorithm. Given two positive integers and , find .
Euclidean algorithm
- Input
- Output
| 1 | while do | |
| 2 | // | |
| 3 | ||
| 4 | ||
| 5 | return |
Proof
The variables and change. Write and for the two inputs. At the start of each iteration, just before the guard on line 1 is tested, let be the assertion that , , and .
Initialization. Before the first iteration, and , both positive. Thus holds.
Maintenance. Assume holds before an iteration and the guard holds, so . By Theorem 1.5 there is an integer with and . Line 2 sets to that remainder. By Theorem 3.2, . Lines 3 and 4 then replace by , in that order, so receives the old value of before receives . The new is positive and the new is nonnegative, and holds at the next test of the guard.
Termination. Whenever the guard holds, line 2 produces , and line 4 sets equal to . If the guard holds again, this new is a strictly smaller positive integer. The successive values of at the guard are therefore a strictly decreasing sequence of positive integers. If the sequence were infinite, its set of values would be a nonempty subset of with no least element, contradicting the well-ordering principle. So the guard fails after finitely many iterations.
Exit. The guard has failed and still holds, so forces , and . Thus divides both and , and no larger positive integer divides , hence . By , line 5 returns .
The Euclidean algorithm can take quite a few steps. Given any positive integer , there are integers and for which it runs exactly steps to find . And can be large, so the algorithm can feel slow.
Consecutive Fibonacci numbers are such examples.
Apply the Euclidean algorithm to and :
That is steps, and the value returned is , so .
We have the general result:
Note that this theorem does not only illustrate our point, that for every such we can find an example on which the Euclidean algorithm runs for exactly steps. Along the way it also proves that the consecutive Fibonacci numbers and are coprime: their greatest common divisor is .
From Example 3.2 we already have the idea of the proof. At every step except the last, the quotient is . At the last step the quotient is , and the remainder is .
Proof
By the recurrence of the Fibonacci sequence, for every integer ,
Note that . This is the crucial point: it shows that divided by has quotient and remainder . By Theorem 1.5, that quotient and remainder are unique.
So the Euclidean algorithm gives
The algorithm returns , which means . And it takes steps.
The Complexity of the Euclidean Algorithm
We can prove a theorem of Gabriel Lamé: an upper bound on the number of steps the Euclidean algorithm takes.
The number of divisions needed to find the greatest common divisor of two positive integers by the Euclidean algorithm does not exceed five times the number of decimal digits in the smaller of the two integers.
Mathematically, suppose with , and suppose it takes steps to find . Then , where is the number of decimal digits of .
The result is a nice one. We finally have an upper bound on the worst case of the Euclidean algorithm. At the same time the conclusion is baffling. What do decimal digits have to do with it, and where does the coefficient come from?
And you’d be surprised that the proof uses the Fibonacci sequence, and a property of it that we introduced, (1.8).
Proof
Suppose it takes steps. Let and . The Euclidean algorithm gives
Since ,
From (3.6) we should at once think of the recurrence for the Fibonacci sequence:
This is where the Fibonacci sequence first shows itself.
Now , and , so . It then follows that
In summary, we have an estimate for by a Fibonacci number indexed from the number of steps taken:
Using (1.8), we have
At this point we already have the inequality that relates . Taking the logarithm base , we move from the exponent down into a product:
Suppose has decimal digits. Then , and hence . Therefore
We can show that
and then obtain
That is where the comes from. Since is an integer, .
We could, of course, use a calculator to check and be done. But I find the estimate itself interesting.
The idea is to show . Recall that we have
So
And
so
Thus we obtain the estimate .
Expressing Greatest Common Divisors as Linear Combinations
Question: how do we express the gcd, , as a linear combination of and ?
We can trace the Euclidean algorithm backward, as it is described in the original textbook. The divisions are
where .
Every step but the last leaves a remainder, and that remainder can be moved to the other side of the equation. The second-to-last step is the one whose remainder is itself. You see, we have
and so is already a linear combination of and .
The step before that does the same for :
Thus is a linear combination of the two terms that precede it, and . Substitute this expression into (3.7). What was a combination of and becomes a combination of and :
And so on. Each time, the latest remainder still standing in the expression is rewritten, by the division that produced it, as a combination of two earlier ones. We keep working backward until the only numbers left are and . By then we are expressing as a linear combination of and .
The problem seems solved. But if you think about writing a program to find the coefficients and such that
then you will realize that this argument does not hand you and on the way down. The quotients appear only as the divisions are performed, and the backward steps need them in reverse: . One pass runs the Euclidean algorithm and stores those quotients. Another pass starts from and walks back to and , building the coefficients as it goes. Two passes, and a list that has to sit there until the second one is finished. An uncomfortable feeling of wasting both time and space.
There is another way to look at the same divisions, a forward view.
Take a minute and think. Do you get what I mean?
Yes. Think this way. The right-hand column is the same division, solved for the new remainder. At the first step, is a linear combination of and . At the second step, is a linear combination of and . We can already write in terms of and , and is , so replacing writes in terms of and as well. Each new remainder is a combination of the two that precede it, and those two are already combinations of and , so the new one is too. Keep going. It is the same replacement every time, and at the end is a linear combination of and .
Nothing has to be saved for a later pass. The quotient is used when it appears, to rewrite the new remainder, and then we do not need it again. One pass through the Euclidean algorithm, and no list of ’s. This is, basically, the idea of the extended Euclidean algorithm.
Call the coefficients and , so that
From (3.8) we can read off the first two:
We can fill in the definitions for and . We have
so
With in hand, the recurrence for and is easy to derive. Suppose
The next step is to express in and . From the Euclidean algorithm,
Substitute and , and we obtain
Matching coefficients gives
for . The cases and hold under the definitions just made for and .
Let with , and let be the number of steps the Euclidean algorithm takes on and . Let be the quotients in that algorithm, and define and by
and, for ,
Then
Proof
Let , , and let be the later remainders, so and, by (3.8),
for . Let be the assertion that .
Base case. The values , , , and give and . Thus and hold. There are two base cases because the recurrence looks back two steps.
Inductive step. Let , and assume and . Then
where the last step is the recurrence. Thus holds.
By induction, holds for every with . In particular, is .
In a program, we can use the following, the so-called extended Euclidean algorithm, to express as a linear combination of and .
Extended Euclidean algorithm
- Input
- Output
- , ,
| 1 | |
| 2 | |
| 3 | while do |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | return , , |