Sub topic 12
Exponentiation by Squaring
This chapter assumes you have read the previous chapter on linear recursive power, can read recursion trees, and know Big-O notation and Python's // and % operators.
The Idea: Halve the Exponent
The linear power(x, n) makes one call for each unit of the exponent, so it does n multiplications. Exponentiation by squaring uses a different identity:
- If n is even, x^n = (x^(n/2))^2.
- If n is odd, x^n = (x^(n//2))^2 · x.
Both cases need only x^(n//2). You compute that once, square it, and multiply by x one more time when n is odd. Each call halves the exponent, so the recursion reaches the base case n = 0 after about log₂ n steps.
This is divide-and-conquer recursion. You split the problem into a half-sized subproblem, solve it once, and combine the result cheaply.
The Implementation
This version handles non-negative integer exponents. The next chapter extends it to negative ones.
def fast_power(x, n):
if n == 0:
return 1
half = fast_power(x, n // 2)
if n % 2 == 0:
return half * half
return half * half * x
print(fast_power(2, 10)) # 1024
print(fast_power(3, 5)) # 243
print(fast_power(7, 0)) # 1The base case is n = 0, which returns 1. The recursive case reduces n to n // 2, which is strictly smaller for every n ≥ 1, so the recursion always terminates.
Tracing the Calls
For fast_power(2, 10) the exponents go 10, 5, 2, 1, 0. Each call makes exactly one recursive call, so the recursion tree is a single chain rather than a branching tree.
Counting Multiplications
Each call with n > 0 does one multiplication to square half. Odd n adds a second multiplication for the extra x. So the total equals the number of bits in n plus the number of 1 bits in n, which is at most 2⌊log₂ n⌋ + 2. That is O(log n). The code below tracks the count directly:
def fast_power_count(x, n):
if n == 0:
return 1, 0
half, mults = fast_power_count(x, n // 2)
if n % 2 == 0:
return half * half, mults + 1
return half * half * x, mults + 2
for n in (10, 100, 1000):
_, mults = fast_power_count(2, n)
print(n, mults)
# 10 6
# 100 10
# 1000 16| Exponent n | Linear `power` multiplications | `fast_power` multiplications |
|---|---|---|
| 10 | 10 | 6 |
| 100 | 100 | 10 |
| 1000 | 1000 | 16 |
The recursion depth also drops from n to about log₂ n. Even an exponent of 10^18 needs only about 61 stack frames, so the depth limit is never a concern here.
Common Errors
- Using
/instead of//. In Python 3,n / 2returns a float. The exponent then never equals the integer base case cleanly, and the parity test stops making sense. - Forgetting the extra x for odd n. If you always return
half * half, the function computes x^(2·(n//2)) and returns x^4 for x^5. - Treating n = 1 as a special case you need.
fast_power(x, 1)already works:halfis 1, and the odd branch returns 1 · 1 · x.
Exercises
Exercise
List the exponents passed to each call of fast_power(3, 13), and give the number of multiplications performed.
Show answer
The exponents are 13, 6, 3, 1, 0. The calls for 13, 3, and 1 are odd and do 2 multiplications each, and the call for 6 is even and does 1, for a total of 7. In binary, 13 is 1101: 4 bits plus 3 one-bits gives 7.
Exercise
A colleague replaces half * half with fast_power(x, n // 2) * fast_power(x, n // 2) in both branches. The results are still correct. What happens to the running time, and why?
Show answer
The running time becomes O(n) instead of O(log n). Each call now makes two recursive calls on n // 2, so the recursion tree is binary with about log₂ n levels. That gives roughly 2^(log₂ n) = n calls, the same order as linear recursion.