Fast power and remainders
🌍 In real life. Every time you open a mobile banking app, its encryption raises numbers hundreds of digits long to huge powers and keeps only the remainder after dividing by another huge number. Multiplying the base by itself a billion times would take far too long; instead the phone squares and squares again, and the whole sum takes a few hundred multiplications.
The idea in one sentence: write the exponent in binary, square the base again and again (base, base², base⁴, base⁸, …), and multiply in the squares whose switch is on, taking the remainder after every multiplication so the numbers stay small.
Its words:
- remainder (modulo):
a % m, what is left after dividing by m; in Python it is never negative when m is positive - modular arithmetic:
(a × b) % m == ((a % m) × (b % m)) % m, so you may take the remainder early and often - fast power (exponentiation by squaring): about two multiplications per bit of the exponent
pow(base, exp, mod): Python's built-in fast power
Step by step:
- result = 1
- look at the exponent's lowest switch: if it is on, result = result × base % mod
- base = base × base % mod, and shift the exponent right by one
- repeat until the exponent is 0
Watch 3 to the power 13 (13 = 0b1101), remainder 100:
Why it is right. 13 = 8 + 4 + 1, so 3 to the power 13 is 3⁸ × 3⁴ × 3¹: exactly the squares whose switch is on. Squaring 3^(2^k) gives 3^(2^(k+1)), the next square. Taking the remainder at each step doesn't change the final remainder, because of the rule for (a × b) % m.
What it costs, by counting. The plain way multiplies exp − 1 times: 999,999 multiplications for an exponent of 1,000,000. That exponent has only 20 switches, so fast power needs at most 40 multiplications: O(log exp) instead of O(exp).
def fast_pow(base, exp, mod):
result = 1 % mod
base %= mod
while exp:
if exp & 1: # this switch of the exponent is on
result = result * base % mod
base = base * base % mod # base, base², base⁴, base⁸, …
exp >>= 1
return result
print(fast_pow(3, 13, 100), pow(3, 13, 100))
print(fast_pow(2, 1000000, 1000000007))
print(-7 % 3, (17 * 23) % 5, (17 % 5) * (23 % 5) % 5)Mistakes and edge cases.
- forgetting to square the base: then every on switch multiplies in plain base, not its square
- an exponent of 0: the answer is 1, or 0 when mod is 1 (everything leaves remainder 0), which is why the code starts with
1 % mod - taking the remainder only at the end: the answer is still right in Python, but the numbers grow to millions of digits and it crawls
- negative remainders: Python's
-7 % 3is 2; some other languages give −1 base ** exp % 10for a huge exp: Python builds the whole gigantic number first; usepow(base, exp, 10)
Quiz
What does this print?
print(pow(2, 10, 1000))What does this print?
print(-7 % 3)About how many multiplications does fast power need for an exponent of 1,000,000?
- At most 40: two per switch of the exponent
- About 999,999
- About 1,000
What does this print?
print((17 * 23) % 5, ((17 % 5) * (23 % 5)) % 5)fast_pow(3, 13, 100) gives 64 instead of 23. Which line is wrong?
def fast_pow(base, exp, mod): result = 1 % mod base %= mod while exp: if exp & 1: result = result * base % mod base = base * 2 % mod exp >>= 1 return result
Coding tasks
Write
fast_pow(base, exp, mod):baseto the powerexp, remainder after dividing bymod, by squaring (withoutpow).expis a whole number, maybe very large;modis at least 1.Write
last_digit(base, exp): the last digit ofbaseto the powerexp.expcan be as big as 10 to the power 18.