Sub topic 3
How Fast Is It? Lamé's Theorem and Fibonacci Worst Cases
This chapter assumes you know the loop a, b = b, a % b from the previous chapter and can follow a short induction argument.
How Fast Is It? Lamé's Theorem and Fibonacci Worst Cases
Each remainder is smaller than the one before it, but the slowest shrinkage happens when every quotient is 1. With quotients of 1 the remainders form the Fibonacci sequence in reverse. Lamé's theorem states the bound: if the algorithm takes k division steps on a > b > 0, then b ≥ F(k+1), where F(1) = F(2) = 1. Fibonacci numbers grow like φ^k with φ ≈ 1.618, so the step count is O(log min(a, b)). It is at most about five times the number of decimal digits of b.
def steps(a, b):
count = 0
while b != 0:
a, b = b, a % b
count += 1
return count
print(steps(89, 55)) # 9, consecutive Fibonacci numbers
print(steps(100, 55)) # 4From GCD to Least Common Multiple
The identity lcm(a, b) · gcd(a, b) = |a · b| gives the least common multiple directly. Divide before you multiply so the intermediate value stays small.
The Extended Euclidean Algorithm and Bézout's Identity
Bézout's identity says that integers x and y always exist with a·x + b·y = gcd(a, b). The extended algorithm tracks these coefficients alongside the remainders, and it uses the same number of steps as the plain algorithm.
Computing Modular Inverses
An inverse of a modulo m exists exactly when gcd(a, m) = 1. In that case a·x + m·y = 1, so a·x ≡ 1 (mod m), and x mod m is the inverse.
def gcd(a, b):
a, b = abs(a), abs(b)
while b != 0:
a, b = b, a % b
return a
def lcm(a, b):
if a == 0 or b == 0:
return 0
return abs(a // gcd(a, b) * b)
def extended_gcd(a, b):
# Returns (g, x, y) with a*x + b*y == g == gcd(a, b), for a, b >= 0
old_r, r = a, b
old_s, s = 1, 0
old_t, t = 0, 1
while r != 0:
q = old_r // r
old_r, r = r, old_r - q * r
old_s, s = s, old_s - q * s
old_t, t = t, old_t - q * t
return old_r, old_s, old_t
def mod_inverse(a, m):
g, x, _ = extended_gcd(a % m, m)
if g != 1:
raise ValueError("inverse does not exist")
return x % m
print(lcm(4, 6)) # 12
print(extended_gcd(240, 46)) # (2, -9, 47)
print(mod_inverse(3, 11)) # 4
print(pow(3, -1, 11)) # 4, built-in since Python 3.8Binary GCD: Stein's Algorithm
Stein's algorithm uses shifts and subtraction instead of division. It first removes the common factors of two, then repeatedly subtracts one odd value from the other.
| Method | Core operation | Steps |
|---|---|---|
| Division (Euclid) | `a % b` | O(log n) |
| Subtraction | `a - b` | up to O(n) |
| Binary (Stein) | shifts, `a - b` | O(log n) bit steps |
def binary_gcd(a, b):
a, b = abs(a), abs(b)
if a == 0:
return b
if b == 0:
return a
shift = 0
while (a | b) & 1 == 0:
a >>= 1
b >>= 1
shift += 1
while a & 1 == 0:
a >>= 1
while b != 0:
while b & 1 == 0:
b >>= 1
if a > b:
a, b = b, a
b -= a
return a << shift
print(binary_gcd(48, 18)) # 6In production code, use math.gcd and math.lcm (Python 3.9+).
Exercise
Find the inverse of 7 modulo 26, and explain why 13 has no inverse modulo 26.
Show answer
7 · 15 = 105 = 4 · 26 + 1, so the inverse is 15. gcd(13, 26) = 13, which is not 1, so no inverse exists.