Pow(x, n) asks you to compute x raised to the power n. The naive loop multiplies x by itself n times — but a classic trick computes the same answer in O(log n) by squaring the base and halving the exponent.
Problem. Implement pow(x, n), which computes x raised to the power n (i.e. x^n). x is a
floating-point number and n is an integer that may be negative.
Example: x = 2.0, n = 10 → answer 1024.0. And x = 2.0, n = -2 → answer 0.25.
The slow way first
The obvious idea: start at 1.0 and multiply by x exactly n times. That works, but it is O(n) — for n = 1,000,000,000 that is a billion multiplications. Far too slow.
The question to ask: can I reuse work instead of redoing it? Computing x^2 gives me x^4 for one more multiply, then x^8, then x^16... Each squaring doubles the exponent I have covered, so I reach x^n in about log n steps.
The idea: square the base, halve the exponent
Write n in binary. Every power of x we need is a power-of-two power (x^1, x^2, x^4, x^8, ...), and x^n is the product of the ones whose bit is set. So walk the bits of n from the bottom: keep a running x that you square each step, and whenever the current bit is 1 (that is, n is odd), fold the running x into result. Then halve n and repeat until it reaches 0.
Negative exponents are handled up front: x^-n = (1/x)^n, so we replace x with 1/x and flip n positive, then run the same loop.
Walk through it
Step through the animation with x = 2.0, n = 10. The three cells are x, n, and result. Watch x square (2 → 4 → 16 → 256), n halve (10 → 5 → 2 → 1 → 0), and result pick up a factor only on the odd steps (n = 5 and n = 1). After just 4 iterations result is 1024.0.
Pseudocode
result = 1.0
if n is negative:
x = 1 / x # x^-n = (1/x)^n
n = -n
while n > 0:
if n is odd:
result = result * x # fold in this power-of-two power
x = x * x # square the base
n = n // 2 # drop the lowest bit
return resultThe Python solution
def my_pow(x, n):
result = 1.0
if n < 0:
x = 1 / x
n = -n
while n > 0:
if n % 2 == 1:
result *= x
x *= x
n //= 2
return resultresultstarts at1.0, the identity for multiplication.- The
if n < 0block turns a negative exponent into a positive one by invertingx. n % 2 == 1tests whether the current lowest bit is set — i.e. whethernis odd.- Line 8 is the only place we touch
result: we fold in the runningxexactly when the bit is1. x *= xsquares the base, andn //= 2drops the bit we just handled, shrinking the problem each pass.
Complexity
| Case | Time | Notes |
|---|---|---|
| Naive (multiply n times) | O(n) (moderate) | one multiply per unit of n |
| Fast exponentiation | O(log n) (fast) | n is halved each iteration |
O(1) (fast)The exponent is halved every pass, so the loop runs about log2(n) times — for a billion, roughly 30 steps instead of a billion. The iterative version uses O(1) extra space (a recursive version would use O(log n) stack frames).
When this pattern shows up
Whenever you must apply an operation n times and that operation is associative (powers, matrix
multiplication, modular exponentiation), reach for binary exponentiation: square the operand and halve
the count. It turns O(n) into O(log n).
Do not forget negative n. Invert x to 1/x and flip n positive before the loop. Also be careful
with the most-negative integer: in languages with fixed-width ints, -n can overflow — Python integers
are unbounded, so this code is safe as written.
Practice
For x = 2.0, n = 10, the binary of 10 is 1010. On which loop iterations (by the value of n) does result actually change?
1. Why is fast exponentiation O(log n) instead of O(n)?
2. When do we multiply result by x?
3. How is a negative exponent handled?
4. What is the extra space of the iterative solution?