Homepage › Solution manuals › Ivan Niven › An Introduction to the Theory of Numbers › Exercise 4.4.10 (Upper bound of the runtime of the Euclidean algorithm)
Exercise 4.4.10 (Upper bound of the runtime of the Euclidean algorithm)
If the Euclidean algorithm is applied to the positive integers and , , then for some , and . Put , so that is the number of divisions performed in executing the algorithm. Show that for all positive integers . Prove that and that . More generally, prove that if , then , with equality if and only if , and . Conclude that if , then . (This bound was given by Gabriel Lamé in 1845. It was the first occasion in which the worst-case running time of a mathematical algorithm was precisely determined.)
Answers
Example: With the notations of Theorem 1.1, if we write the Euclidean algorithm for , we obtain
Since and , then , and for , .
We note an exception in the last division, where the quotient is not , but , because we divide by . Since , is not a Euclidean division (I fell into this trap first!).
Proof. As in Theorem 1.1, we write the Euclidean algorithm in the form
Here , and
Then is the number of divisions performed in executing the algorithm.
- (a)
-
Suppose that
and
. We prove by induction that
for .
- and , so and are true.
-
Suppose now that and are true for some , where . Then
The sequence is strictly increasing. Here , thus , so
the unicity of the division gives and , thus is true.
-
The induction is done, so
In particular, , so
thus , and . So
- (b)
-
We prove by induction on
that
is true for all such that .
-
By definition of , , thus , so is true.
Moreover , so , so is true.
-
Suppose that and are both true for some , so that
Then by (2) and (4),
So , which proves .
-
The induction is done, thus
In particular,
Since and , then , thus , so
-
- (c)
-
Suppose now that
. Then by (6),
. Since the Fibonacci sequence is strictly increasing from rank
, we obtain
(otherwise
implies
), so
We saw in part (a) that if and then .
Conversely, suppose that . Then , thus by (6), . By hypothesis, , so .
Moreover, by (5) , and since , thus
thus . But by (5), so .
Then , thus , where , so (and by (5)), so .
In conclusion, if , then , with equality if and only if , and .
- (d)
-
Suppose that
. Since
is a positive integer, there is some positive integer
such that
Since , then by part (c), .
-
Consider first the worst case, where . Then, by part (c), and . By Problem 3,
thus
-
If , then , and by Problem 3
thus
In both cases,
-
Note: To test these results we can use the following (naive) programs in python, or sagemath:
def F(n): f2, f1 = 1, 0 for i in range(n): f2, f1 = f1, f1 + f2 return f1 def E(b,c): counter = 0 while c != 0: b, c = c, b % c counter += 1 return counter n = 4 print(E(F(n+2),F(n+1))) 4