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 b and c , b c , then r j = ( b , c ) for some j , and r j + 1 = 0 . Put E ( b , c ) = j + 1 , so that E ( b , c ) is the number of divisions performed in executing the algorithm. Show that E ( F n + 2 , F n + 1 ) = n for all positive integers n . Prove that r j F 2 , r j 1 F 3 , r j 2 F 4 , , and that b F j + 3 . More generally, prove that if F n + 2 b c , then E ( b , c ) n , with equality if and only if b = F n + 2 , and c = F n + 1 . Conclude that if b c , then E ( b , c ) < ( log b ) log ( ( 1 + 5 ) 2 ) . (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 b = F 5 , c = F 4 , we obtain

F 5 = F 4 + F 3 = F 4 q 1 + r 1 , q 1 = 1 , 0 < r 1 = F 3 = 2 < F 4 F 4 = F 3 + F 2 = r 1 q 2 + r 2 , q 2 = 1 , 0 < r 2 = F 2 = 1 < r 1 , F 3 = 2 F 2 + 0 = r 2 q 3 + r 3 , q 3 = 2 , 0 < r 3 = 0 < r 2 .

Since r 1 0 , r 2 0 and r 3 = 0 , then j = 2 , and for n = 3 , E ( F n + 2 , F n + 1 ) = j + 1 = 3 = n .

We note an exception in the last division, where the quotient is not 1 , but 2 , because we divide by 1 . Since F 2 = F 1 = 1 , F 3 = F 2 + F 1 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

b = r 1 , c = r 0 , (1) r i = r i + 1 q i + 2 + r i + 2 , ( 1 i j 1 ) , (2) r j = b c , r j + 1 = 0 . (3)

Here r 1 = b r 0 = c , and

0 < r i + 2 < r i + 1 ,  if  0 i j 1 . (4)

Then E ( b , c ) = j + 1 is the number of divisions performed in executing the algorithm.

(a)
Suppose that b = F n + 2 = r 1 and c = F n + 1 = r 0 . We prove by induction that 𝒫 ( i ) : r i = F n + 1 i

for 1 i n 1 .

  • r 1 = F n + 2 and r 0 = F n + 1 , so 𝒫 ( 1 ) and 𝒫 ( 0 ) are true.
  • Suppose now that 𝒫 ( i ) and 𝒫 ( i + 1 ) are true for some i , where 1 i n 3 . Then

    r i = F n + 1 i , r i + 1 = F n i .

    The sequence ( F k ) k 2 is strictly increasing. Here i n 3 , thus F n i 1 < F n i , so

    { r i = F n + 1 i = F n i + F n i 1 , 0 F n i 1 < F n i r i = r i + 1 q i + 2 + r i + 2 , 0 r i + 2 < r i + 1

    the unicity of the division gives q i + 2 = 1 and r i + 2 = F n i 1 , thus 𝒫 ( i + 2 ) is true.

  • The induction is done, so

    i [ [ 1 , n 1 ] ] , r i = F n + 1 i .

In particular, r n 2 = F 3 = 2 , r n 1 = F 2 = 1 , so

r n 2 = 2 r n 1 + 0 ,

thus r n = 0 , and j = n 1 . So

E ( F n + 2 , F n + 1 ) = j + 1 = n .

(b)
We prove by induction on k that 𝒬 ( k ) : r j k F k + 2

is true for all k such that 0 k j .

  • By definition of j , r j 0 , thus r j 1 = F 2 , so 𝒬 ( 0 ) is true.

    Moreover r j 1 > r j , so r j 1 r j + 1 2 = F 3 , so 𝒬 ( 1 ) is true.

  • Suppose that 𝒬 ( k ) and 𝒬 ( k + 1 ) are both true for some k j 2 , so that

    r j k F k + 2 , r j k 1 F k + 3 .

    Then by (2) and (4),

    r j k 2 = r j k 1 q j k + r j k r j k 1 + r j k ( since  q j k 1 ) F k + 3 + F k + 2 (by the induction hypothesis ) = F k + 4 .

    So r j k 2 F k + 4 , which proves 𝒬 ( k + 2 ) .

  • The induction is done, thus

    k [ [ 0 , j ] ] , r j k F k + 2 .

In particular,

r 1 F j + 1 , c = r 0 F j + 2 . (5)

Since b c and b = c q 1 + r 1 ( 0 r 1 < b ) , then q 1 1 , thus b c + r 1 F j + 1 + F j + 2 = F j + 3 , so

b F j + 3 . (6)
(c)
Suppose now that F n + 2 b c . Then by (6), F n + 2 F j + 3 . Since the Fibonacci sequence is strictly increasing from rank 2 , we obtain n + 2 j + 3 (otherwise n + 2 < j + 3 implies F n + 2 < F j + 3 ), so n j + 1 = E ( b , c ) .

We saw in part (a) that if b = F n + 2 and c = F n + 1 then E ( b , c ) = n .

Conversely, suppose that E ( b , c ) = n . Then n = j + 1 , thus by (6), b F n + 2 . By hypothesis, b F n + 2 , so b = F n + 2 .

Moreover, by (5) c F j + 2 = F n + 1 , and q 1 1 since b c , thus

F n + 2 = b = c q 1 + r 1 c + r 1 F n + 1 + r 1 ,

thus r 1 F n + 2 F n + 1 = F n . But r 1 F j + 1 = F n by (5), so r 1 = F n .

Then F n + 2 = c q 1 + F n , thus c q 1 = F n + 1 , where q 1 1 , so c F n + 1 (and c F n + 1 by (5)), so c = F n + 1 .

In conclusion, if F n + 2 b c , then E ( b , c ) n , with equality if and only if b = F n + 2 , and c = F n + 1 .

(d)
Suppose that c b . Since b is a positive integer, there is some positive integer n such that F n + 1 b F n + 2 .

Since b F n + 2 , then by part (c), E ( b , c ) n .

  • Consider first the worst case, where E ( b , c ) = n . Then, by part (c), b = F n + 2 and c = F n + 1 . By Problem 3,

    ( 1 + 5 2 ) n < F n + 2 = b ,

    thus

    E ( b , c ) = n < log ( b ) log ( 1 + 5 2 ) .

  • If E ( b , c ) n , then E ( b , c ) n 1 , and by Problem 3

    ( 1 + 5 2 ) n 1 < F n + 1 b ,

    thus

    E ( b , c ) n 1 < log ( b ) log ( 1 + 5 2 ) .

In both cases,

E ( b , c ) < log ( b ) log ( 1 + 5 2 ) ( b c ) .

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

User profile picture
2025-02-08 12:37
Comments