Sub topic 8
How Fast Is It? Lamé's Theorem and Fibonacci Worst Cases
This chapter assumes you can trace Euclid's algorithm, that you have seen why the remainders strictly decrease, and that you are comfortable with proofs by induction and with logarithms. We use the Fibonacci numbers with F(1) = 1, F(2) = 1, and F(k) = F(k−1) + F(k−2).
The termination proof only shows that the remainders shrink, so you may expect the number of steps to grow with the size of b. In fact it grows only with the number of digits in b. This chapter proves that bound and finds the inputs that come closest to it.
Counting division steps
A "step" is one pass of the loop, which replaces (a, b) with (b, a mod b). The function below counts the steps and prints them for pairs of consecutive Fibonacci numbers.
def gcd_steps(a, b):
steps = 0
while b != 0:
a, b = b, a % b
steps += 1
return a, steps
a, b = 1, 1 # F(2), F(1)
for n in range(1, 11):
a, b = a + b, a # now (F(n+2), F(n+1))
g, steps = gcd_steps(a, b)
print(n, a, b, g, steps)
print(gcd_steps(100, 55)) # (5, 4)The last line of the loop prints 10 144 89 1 10. The pair (F(n+2), F(n+1)) always takes exactly n steps. The pair (100, 55) is larger than (89, 55), but it finishes in 4 steps. Consecutive Fibonacci numbers take more steps because every quotient except the last is 1, so each step makes the least possible progress.
Lamé's theorem
Theorem (Lamé, 1844). Let a > b > 0. If Euclid's algorithm takes n steps on (a, b), then b ≥ F(n+1) and a ≥ F(n+2).
Proof. Write the remainder sequence as r₀ = a, r₁ = b, r₂, …, rₙ, rₙ₊₁ = 0, so that rₙ = gcd(a, b). Each step is
r(i−1) = qᵢ · rᵢ + r(i+1), with qᵢ ≥ 1,
so r(i−1) ≥ rᵢ + r(i+1). Now work backwards from the end of the sequence:
- rₙ ≥ 1 = F(2), because the gcd is positive.
- rₙ₋₁ = qₙ · rₙ and rₙ₋₁ > rₙ, so qₙ ≥ 2 and rₙ₋₁ ≥ 2 = F(3).
- Suppose rₙ₋ⱼ ≥ F(j+2) for every j ≤ k. Then rₙ₋ₖ₋₁ ≥ rₙ₋ₖ + rₙ₋ₖ₊₁ ≥ F(k+2) + F(k+1) = F(k+3).
By induction, rₙ₋ₖ ≥ F(k+2) for every k. Setting k = n−1 gives b = r₁ ≥ F(n+1), and setting k = n gives a ≥ F(n+2). ∎
The Fibonacci pairs achieve equality at every step, so the bound is tight.
From Fibonacci to logarithms
Let φ = (1 + √5)/2 ≈ 1.618. A short induction shows that F(k) ≥ φ^(k−2) for k ≥ 1. Combined with Lamé's theorem, this gives
φ^(n−1) ≤ F(n+1) ≤ b, so n ≤ 1 + log_φ(b).
Suppose b has d decimal digits, so b < 10^d. Since log_φ(10) ≈ 4.785, we get n < 4.785·d + 1. Because n is a whole number, n ≤ 5d. This is the form in which Lamé stated the result.
| Smaller input b | Digits d | Bound 5d | Worst actual case |
|---|---|---|---|
| 1 to 9 | 1 | 5 | (13, 8): 5 steps |
| 10 to 99 | 2 | 10 | (144, 89): 10 steps |
| 100 to 999 | 3 | 15 | (1597, 987): 15 steps |
Exercise
Without running code, how many division steps does Euclid's algorithm take on (233, 144)? Which earlier pair gives the same number of steps?
Show answer
233 = F(13) and 144 = F(12), so this is (F(n+2), F(n+1)) with n = 11, which takes 11 steps. No smaller pair gives 11 steps, because Lamé's theorem requires b ≥ F(12) = 144 and a ≥ F(13) = 233.
Exercise
The proof uses qₙ ≥ 2 for the last quotient. Why must the last quotient be at least 2, while all the earlier quotients may equal 1?
Show answer
The last step divides rₙ₋₁ by rₙ with remainder 0, so rₙ₋₁ = qₙ · rₙ. Remainders strictly decrease, so rₙ₋₁ > rₙ, and therefore qₙ cannot be 1. Earlier steps leave a nonzero remainder, so a quotient of 1 is possible there.
Exercise
Use the bound n ≤ 1 + log_φ(b) to find the largest possible step count when b = 1,000,000.
Show answer
log_φ(10^6) = 6 × 4.785 ≈ 28.7, so n ≤ 29.7 and n ≤ 29. As a check, F(30) = 832,040 ≤ 10^6 < F(31) = 1,346,269. Lamé's theorem gives F(n+1) ≤ b, so n+1 ≤ 30 and n ≤ 29. The pair (F(31), F(30)) has a larger first number but uses the same b-range, and it takes exactly 29 steps.