Sub topic 8
How Fast Is It? Lamé's Theorem and Fibonacci Worst Cases
The previous chapter showed that Euclid's algorithm always stops, because the remainders strictly decrease. That alone does not say how long it runs, since a decreasing sequence of integers starting at a billion could, in principle, take a billion steps. This chapter shows that the real bound is much smaller: the number of steps grows with the number of digits of the input, not with its size. The worst inputs are consecutive Fibonacci numbers.
This chapter assumes you know the iterative form of Euclid's algorithm, the termination argument from the previous chapter, the Fibonacci sequence, and proof by induction.
What Counts as a Step
A step here is one division with remainder, which means one execution of a, b = b, a % b. To study the algorithm, it helps to count steps directly:
def gcd_steps(a, b):
"""Return (gcd, number of division steps) for a, b >= 0."""
steps = 0
while b != 0:
a, b = b, a % b
steps += 1
return a, steps
print(gcd_steps(1071, 462)) # (21, 3)
print(gcd_steps(34, 21)) # (1, 7)
print(gcd_steps(21, 34)) # (1, 8)The steps for 1071 and 462 are the three remainders 147, 21, and 0. The pair 34 and 21 is much smaller, yet it needs more than twice as many steps. Swapping the arguments adds one more step.
Notation for the Remainder Sequence
Write the run of the algorithm as a sequence of remainders. Set r₀ = a and r₁ = b with a > b > 0. Step i divides r₍ᵢ₋₁₎ by rᵢ:
- r₍ᵢ₋₁₎ = qᵢ · rᵢ + r₍ᵢ₊₁₎, with 0 ≤ r₍ᵢ₊₁₎ < rᵢ
If the algorithm takes n steps, then r₍ₙ₊₁₎ = 0 is the first zero remainder, and rₙ = gcd(a, b) ≥ 1. Each step's quotient qᵢ is at least 1, because r₍ᵢ₋₁₎ > rᵢ.
How fast the remainders shrink depends on the quotients. A large quotient makes the next remainder small compared with the current one. A quotient of 1 gives the slowest possible shrinkage: r₍ᵢ₊₁₎ = r₍ᵢ₋₁₎ − rᵢ. Inputs where every quotient is 1 are the worst case. If you run that relation backwards from the end, you get the Fibonacci recurrence.
The Fibonacci Worst Case
Use the convention F₁ = 1, F₂ = 1, and Fₘ = F₍ₘ₋₁₎ + F₍ₘ₋₂₎. Run the algorithm on F₉ = 34 and F₈ = 21:
| Step | Pair (a, b) | Quotient | Remainder |
|---|---|---|---|
| 1 | (34, 21) | 1 | 13 |
| 2 | (21, 13) | 1 | 8 |
| 3 | (13, 8) | 1 | 5 |
| 4 | (8, 5) | 1 | 3 |
| 5 | (5, 3) | 1 | 2 |
| 6 | (3, 2) | 1 | 1 |
| 7 | (2, 1) | 2 | 0 |
Every quotient is 1 except the last one. The last quotient must be at least 2, because r₍ₙ₋₁₎ is a multiple of rₙ and larger than it. Each remainder is the Fibonacci number just below the divisor, so the pair moves down the Fibonacci sequence one term per step.
In general, the pair (F₍ₙ₊₂₎, F₍ₙ₊₁₎) takes exactly n steps. The next section proves that no smaller pair takes that many.
Lamé's Theorem
Theorem (Lamé, 1844). If Euclid's algorithm on a > b > 0 takes n steps, then b ≥ F₍ₙ₊₁₎ and a ≥ F₍ₙ₊₂₎.
Proof. We show by downward induction that rᵢ ≥ F₍ₙ₊₂₋ᵢ₎ for i = n, n − 1, …, 0.
- Base case i = n: rₙ = gcd(a, b) ≥ 1 = F₂.
- Base case i = n − 1: the last step has remainder 0, so r₍ₙ₋₁₎ = qₙ · rₙ. Also r₍ₙ₋₁₎ > rₙ, so qₙ ≥ 2 and r₍ₙ₋₁₎ ≥ 2 = F₃.
- Inductive step: for 1 ≤ i ≤ n − 1, the relation r₍ᵢ₋₁₎ = qᵢ · rᵢ + r₍ᵢ₊₁₎ with qᵢ ≥ 1 gives r₍ᵢ₋₁₎ ≥ rᵢ + r₍ᵢ₊₁₎ ≥ F₍ₙ₊₂₋ᵢ₎ + F₍ₙ₊₁₋ᵢ₎ = F₍ₙ₊₃₋ᵢ₎.
Setting i = 1 and i = 0 gives b = r₁ ≥ F₍ₙ₊₁₎ and a = r₀ ≥ F₍ₙ₊₂₎. ∎
The proof also shows when the bound is tight. Equality requires gcd = 1, a last quotient of exactly 2, and every other quotient equal to 1. Those conditions describe consecutive Fibonacci numbers.
Checking the Theorem by Brute Force
The theorem predicts that, for each step count n, the pair with the smallest a that needs n steps is (F₍ₙ₊₂₎, F₍ₙ₊₁₎). A search confirms this for small inputs:
def gcd_steps(a, b):
steps = 0
while b != 0:
a, b = b, a % b
steps += 1
return a, steps
def smallest_pair_for_steps(max_a):
"""For each step count, the first pair (a, b) with a > b > 0 that needs it."""
best = {}
for a in range(2, max_a + 1):
for b in range(1, a):
_, s = gcd_steps(a, b)
if s not in best:
best[s] = (a, b)
return best
for steps, pair in sorted(smallest_pair_for_steps(100).items()):
print(steps, pair)
# 1 (2, 1)
# 2 (3, 2)
# 3 (5, 3)
# 4 (8, 5)
# 5 (13, 8)
# 6 (21, 13)
# 7 (34, 21)
# 8 (55, 34)
# 9 (89, 55)Every row is a pair of consecutive Fibonacci numbers. No input below 100 needs 10 steps, because F₁₂ = 144.
From Fibonacci Numbers to Logarithms
Lamé's theorem gives a bound in terms of Fibonacci numbers. To turn it into a logarithmic bound, use the golden ratio φ = (1 + √5)/2 ≈ 1.618, which satisfies φ² = φ + 1.
Lemma. Fₘ ≥ φ^(m−2) for all m ≥ 1.
The lemma holds for F₁ = 1 ≥ φ⁻¹ and F₂ = 1 = φ⁰. For m ≥ 3, induction gives Fₘ = F₍ₘ₋₁₎ + F₍ₘ₋₂₎ ≥ φ^(m−3) + φ^(m−4) = φ^(m−4)(φ + 1) = φ^(m−4) · φ² = φ^(m−2).
Combining the lemma with Lamé's theorem gives b ≥ F₍ₙ₊₁₎ ≥ φ^(n−1), so:
- n ≤ 1 + log_φ b ≈ 1 + 1.44 · log₂ b
The number of steps is logarithmic in the smaller input.
The Five-Digits Rule
Lamé stated his result in decimal digits: the number of steps is at most five times the number of decimal digits of the smaller input. Suppose b has k digits, so b < 10ᵏ. If n ≥ 5k + 1, then b ≥ φ^(n−1) ≥ φ^(5k) = (φ⁵)ᵏ. Since φ⁵ ≈ 11.09 > 10, that gives b > 10ᵏ, which is a contradiction. Therefore n ≤ 5k.
The following code checks both forms of the bound on a large Fibonacci pair:
import math
def gcd_steps(a, b):
steps = 0
while b != 0:
a, b = b, a % b
steps += 1
return a, steps
def fib_pair(n):
"""Return (F(n+2), F(n+1)), the smallest pair needing n steps."""
x, y = 1, 1 # F(1), F(2)
for _ in range(n):
x, y = y, x + y
return y, x
phi = (1 + math.sqrt(5)) / 2
a, b = fib_pair(100)
_, steps = gcd_steps(a, b)
print(steps) # 100
print(len(str(b)), 5 * len(str(b))) # 21 105
print(math.floor(1 + math.log(b, phi))) # 100The Fibonacci pair needs exactly 100 steps. The logφ bound rounds down to exactly 100, so it is tight. The five-digits rule allows 105 steps and is slightly looser, because it rounds logφ 10 ≈ 4.785 up to 5.
A Simpler but Weaker Bound
A shorter argument gives a bound of the same order. Every two steps, the remainder at least halves: r₍ᵢ₊₁₎ < r₍ᵢ₋₁₎ / 2. There are two cases:
- If rᵢ ≤ r₍ᵢ₋₁₎ / 2, then r₍ᵢ₊₁₎ < rᵢ ≤ r₍ᵢ₋₁₎ / 2.
- If rᵢ > r₍ᵢ₋₁₎ / 2, then the quotient is 1 and r₍ᵢ₊₁₎ = r₍ᵢ₋₁₎ − rᵢ < r₍ᵢ₋₁₎ / 2.
This argument gives at most about 2 · log₂ b steps. It is often enough for a quick O(log b) claim. The table compares the bounds:
| Bound | Formula | Steps allowed for b ≈ 10⁶ |
|---|---|---|
| Halving argument | about 2 · log₂ b | about 40 |
| Five-digits rule | 5 · (number of digits of b) | 35 |
| Lamé via φ | 1 + log_φ b | about 29 |
| Actual worst case below 10⁶ | Fibonacci pair (F₃₁, F₃₀) | 29 |
Typical Inputs Are Faster
Fibonacci pairs are rare. For random inputs, quotients larger than 1 are common, and each one speeds up the shrinkage. Analyses by Heilbronn and Dixon show that the average number of steps for inputs up to N is about (12 ln 2 / π²) · ln N ≈ 0.843 · ln N. The worst case is ln N / ln φ ≈ 2.078 · ln N, so the average is about 40% of the worst case. You can measure the average yourself:
import math
import random
def gcd_steps(a, b):
steps = 0
while b != 0:
a, b = b, a % b
steps += 1
return a, steps
random.seed(1)
N = 10**12
trials = 20000
total = 0
for _ in range(trials):
a = random.randint(2, N)
b = random.randint(1, a - 1)
total += gcd_steps(a, b)[1]
print(round(total / trials, 2)) # measured average
print(round(0.843 * math.log(N), 2)) # 23.29, leading-term estimate
print(round(math.log(N) / math.log((1 + 5 ** 0.5) / 2), 2)) # 57.42, worst-case scaleThe measured average should land near the 0.843 · ln N estimate. The two differ by a small constant, which the leading term leaves out.
Steps Versus Actual Running Time
The comparison with the subtraction method from an earlier chapter is direct. One division step with quotient qᵢ replaces qᵢ subtractions, so the subtraction method takes q₁ + q₂ + … + qₙ steps. For gcd(10⁹, 1), that is close to a billion subtractions against a single division. The subtraction method is fast only when every quotient is small, which happens exactly in the Fibonacci case where division is at its worst.
Exercise
Without running code, how many division steps does gcd(144, 89) take, and what are the quotients? Then give an upper bound on the steps for any pair a > b > 0 where b has 9 decimal digits.
Show answer
144 = F₁₂ and 89 = F₁₁, so the pair is (F₍ₙ₊₂₎, F₍ₙ₊₁₎) with n = 10. It takes 10 steps, with nine quotients of 1 followed by a final quotient of 2 (on the pair (2, 1)). For a 9-digit b, the five-digits rule gives at most 45 steps. The sharper bound 1 + log_φ b with b < 10⁹ gives at most 44 steps.
Exercise
The proof of Lamé's theorem uses the fact that the last quotient qₙ is at least 2. Explain why, and give the pair that would be the "worst case" if a last quotient of 1 were allowed.
Show answer
At the last step, r₍ₙ₋₁₎ = qₙ · rₙ with remainder 0, and r₍ₙ₋₁₎ > rₙ because each remainder is smaller than the previous divisor (or because a > b when n = 1). A quotient of 1 would force r₍ₙ₋₁₎ = rₙ, which is impossible, so qₙ ≥ 2. If qₙ = 1 were allowed, the chain would end at (1, 1), and the extremal pairs would shift down to (F₍ₙ₊₁₎, Fₙ). That would describe impossible runs and give a weaker bound.
Exercise
Modify gcd_steps so that it also returns the list of quotients. Use it to confirm that the number of subtractions the subtraction-based method needs for gcd(1071, 462) equals the sum of the quotients.
Show answer
Collect a // b before each update: quotients.append(a // b); a, b = b, a % b. For (1071, 462) the quotients are [2, 7, 3], with sum 12. The subtraction method stops when a == b. That saves the final subtraction, which would produce 0, so it performs 12 − 1 = 11 subtractions. In general, the subtraction count is the sum of the quotients minus 1, so the two counts differ by exactly one subtraction at the end.