Fast Powers by Squaring
What is 745 on a 33-hour clock? Multiplying 7 by itself 44 times works, but there's a much faster way: keep squaring, then combine just the pieces you need.
- A power is repeated multiplication: 73 = 7 × 7 × 7. The small raised number is the exponent.
- Squaring a number multiplies it by itself: 72 = 49. Squaring a power doubles its exponent: 74 × 74 = 78.
- Clock math (also called modulo, written "mod"): on a clock with 33 hours, you keep only the remainder after dividing by 33. 49 mod 33 = 16, because 49 = 1 × 33 + 16.
- Binary writes a number using only powers of 2: 45 = 32 + 8 + 4 + 1, which is 101101 in binary.
- Write the exponent as a sum of powers of 2 (its binary digits).
- Make a ladder by squaring: 71, 72, 74, 78, 716, 732. Each rung is the one before it, squared. Wind each one around the clock as you go, so the numbers stay small.
- Multiply together only the rungs whose binary digit is 1. Since 45 = 32 + 8 + 4 + 1, 745 = 732 × 78 × 74 × 71.
- Doubling the exponent (45 → 90) doubles the work for one-at-a-time multiplying. How much more work is it for the squaring method?
- Which exponents need the fewest multiplications for their size? Which need the most?
- Why is it safe to wind around the clock after every multiplication, instead of only at the very end?
Curator's answers
- Just one more squaring. 90 in binary is 1011010: the same digits as 45 with a 0 added on the end.
- Powers of 2 (like 64 = 1000000) need only squarings. Numbers whose binary digits are all 1s (like 63 = 111111) need a multiplication on every rung.
- On a clock, only the remainder matters. Adding or removing whole laps of 33 never changes where you land, even after multiplying. So 49 × 49 and 16 × 16 land on the same hour of a 33-hour clock.
Why it's clever
One-at-a-time multiplying needs one multiplication for each step of the exponent. Squaring needs about two for each binary digit. RSA locks a message by computing a power like this, with exponents hundreds of digits long. Multiplying one at a time would never finish. Squaring takes a few thousand multiplications, and a computer does it in a blink. Press A really big one to see.
From the archive
A version of the trick appears in the Chandaḥśāstra, a book about the rhythms of poetry by the Indian scholar Piṅgala, probably written more than 2,000 years ago. To count rhythm patterns he needed powers of 2, and he computed them by halving the exponent and squaring. Today the same method runs every time your browser opens a secure website.