Fast Power
Talk it through out loud with your AI coach.
The problem
- medium
- Pro
Write a function that raises a number x to a whole-number power n, where n can be zero, negative or very large, without calling a built-in power function. Multiplying x by itself n times is too slow. Explain a faster way and its cost.
What a strong answer covers
- The idea of halving the exponent instead of counting down by one.
- How odd and even powers are treated.
- Zero and negative powers, and a subtle integer-range problem.
- How many multiplications are needed.
Common mistake: Calling the half-power twice at each step instead of once, which throws away the speed-up and does linear work again.
Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.
Practise it out loud