Sub topic 14
Modular Exponentiation for Large Numbers
This chapter assumes you have read the chapter on exponentiation by squaring. You should also know Python's % (remainder) operator and Big-O notation. The goal here is to compute x^n mod m when x^n itself is far too large to work with directly.
Why Reduce as You Go
Cryptography, hashing, and primality tests often need values like 3^200 mod 13. Python integers have no size limit, so 3**200 % 13 does work. The trouble is that 3^200 has 96 digits, and the cost of each multiplication grows with the size of the numbers. If the exponent has thousands of bits, the full power has too many digits to store at all.
The fix comes from a property of remainders:
- (a × b) mod m = ((a mod m) × (b mod m)) mod m
You can take the remainder after every multiplication and the final answer stays the same. Every intermediate value then stays below m, so the numbers being multiplied stay below m².
The Recursive Algorithm
Modular exponentiation is exponentiation by squaring with a % m after each multiplication:
def mod_power(x, n, m):
if n == 0:
return 1 % m
half = mod_power(x, n // 2, m)
result = (half * half) % m
if n % 2 == 1:
result = (result * x) % m
return result
print(mod_power(3, 200, 13)) # 9
print(pow(3, 200, 13)) # 9The structure matches fast_power. The exponent halves on each call, so there are O(log n) calls and at most two multiplications per call. Python's built-in three-argument pow uses the same idea, so it is a reliable check.
Tracing a Call
Here is a trace of mod_power(3, 13, 7). The calls go down until n reaches 0. The results are then combined on the way back up.
| n | half | half² mod 7 | odd? | returns |
|---|---|---|---|---|
| 0 | n/a | n/a | n/a | 1 |
| 1 | 1 | 1 | yes | 3 |
| 3 | 3 | 2 | yes | 6 |
| 6 | 6 | 1 | no | 1 |
| 13 | 1 | 1 | yes | 3 |
You can check the intermediate rows by hand: 3³ = 27, and 27 mod 7 = 6. Also 3⁶ = 729 = 7 × 104 + 1, so 3⁶ mod 7 = 1.
Common Errors
- Reducing only at the end. The answer is still correct, but the program loses the whole benefit of the method because intermediate values grow to the full size of x^n.
- Skipping the reduction after the odd-case multiply. Without it,
result * xcan be as large as m × x, and the next squaring makes it larger still. - A huge base. If x is much larger than m, start with
x = x % mso the first multiplication stays small. - Negative exponents. Taking
1 / ...asfast_powerdoes gives a float, which has no meaning in modular arithmetic. What you need is a modular inverse: a value y with x·y mod m = 1. Python 3.8+ computes it withpow(x, -1, m), and the inverse exists only when x and m share no common factor. For example,pow(3, -1, 7)returns 5, because 15 mod 7 = 1.
Exercise
Without running the code, what does mod_power(2, 10, 1000) return? About how many recursive calls does it make?
Show answer
2^10 = 1024, and 1024 mod 1000 = 24, so it returns 24. The exponents visited are 10, 5, 2, 1, 0, which is 5 calls, roughly log2(10) + 2.
Exercise
What does mod_power(5, 0, 1) return, and why does the base case use 1 % m to get it?
Show answer
It returns 0. Modulo 1, every integer leaves remainder 0. A bare return 1 would give 1, which is not a valid remainder modulo 1.