Math Fundamentals18 sections · 814 units
Open in Course

Power - The Idea

Squaring reduces work

Notice this: x10=(x5)2x^{10} = (x^5)^2. If you compute x5x^5 once, you can square it to get x10x^{10}. That's 1 multiplication instead of 5.

This is exponentiation by squaring. When nn is even, compute xn/2x^{n/2} and square it. When nn is odd, compute x(n1)/2x^{(n-1)/2}, square it, and multiply by xx once more.

Each step cuts nn roughly in half. Instead of O(n)O(n) steps, you do O(logn)O(\log n) steps. For n=109n = 10^9, that's about 30 multiplications instead of a billion.