Let's imagine we're trying to compute . Our above approach computes
which requires three multiplications. In general, it requires multiplications to compute an power.
The trick is realizing that we don't have to just multiply by here. Here's a different approach:
We've saved a multiplication by using twice!
OK, I'll admit this example is pretty lame. You probably didn't need me to tell you that . But this approach scales far better than the old one.
For example, to compute , we can use the following series of multiplications:
The old approach required 31 multiplications—now we can do it in 5.
Each multiplication doubles the exponent, so in the best case we can compute in multiplications. That's an exponential speedup over our old approach!
You might have a question at this point: how do we adapt this approach to work with exponents that aren't powers of two? How would we compute , for example?
Let's imagine we're trying to compute . Our above approach computes
which requires three multiplications. In general, it requires multiplications to compute an power.
The trick is realizing that we don't have to just multiply by here. Here's a different approach:
We've saved a multiplication by using twice!
OK, I'll admit this example is pretty lame. You probably didn't need me to tell you that . But this approach scales far better than the old one.
For example, to compute , we can use the following series of multiplications:
The old approach required 31 multiplications—now we can do it in 5.
Each multiplication doubles the exponent, so in the best case we can compute in multiplications. That's an exponential speedup over our old approach!
You might have a question at this point: how do we adapt this approach to work with exponents that aren't powers of two? How would we compute , for example?