Pow(x, n)

medium

You are given a floating-point number x and an integer n. Compute x raised to the power n — that is, x^nwithout calling your language's built-in power function.

The exponent can be negative or zero:

  • x^0 = 1 for any valid input.
  • A negative exponent means repeated division: x^-n = 1 / x^n.

Return the result as a floating-point number.

Hints

Multiplying x into a running product n times is correct — but n can be around two billion. What algebraic identity lets you skip most of those multiplications?
x^10 = (x^5)^2. If you already know x^5, one extra multiplication gives you x^10. Squaring the base doubles the exponent — so halving the exponent costs only one squaring.
Walk the exponent's binary digits: square the base every step, and multiply it into the answer whenever the current bit is 1. Handle n < 0 by flipping x to 1/x first — and negate n in a 64-bit variable, because -(-2^31) overflows 32 bits.

Common doubts

Because -n overflows: 2^31 does not fit in a 32-bit int, so negating -2^31 in place is undefined or wraps around. Copy the exponent into a 64-bit variable (or a plain number in Python/JavaScript) before negating.
Use the identity x^-n = (1/x)^n: replace x with 1 / x, make the exponent positive, and run the exact same loop. The statement guarantees you never face x = 0 with a negative n, so the division is always safe.
Not for correctness of the algorithm — both the loop and binary exponentiation accumulate only tiny rounding error over at most ~31 multiplications, and answers are compared with fixed decimal precision.
Because 1 is the multiplicative identity — the empty product. Starting at x silently adds one extra factor, which corrupts every even exponent and makes x^0 return x instead of 1.

Interview follow-ups

Same skeleton — binary exponentiation — but take the remainder after every multiplication: this is modular exponentiation, the core of RSA and Diffie-Hellman. The exponent can even arrive as a string of thousands of digits; you process its bits the same way.
Yes: pow(x, n) = pow(x*x, n/2), times x when n is odd — still O(log n) time but O(log n) stack. The iterative bit-walk is the same idea with O(1) space, so prefer it and mention the recursion as the cleaner-to-derive form.
Replace the number x with a 2x2 matrix: the n-th power of [[1,1],[1,0]] contains F(n). Binary exponentiation works over anything with associative multiplication — numbers, matrices, permutations.

Fun facts

  • Doubling-based multiplication appears in the Rhind papyrus from ancient Egypt (~1650 BC), and exponentiation by squaring shows up in Pingala's work on Sanskrit poetic meters over two thousand years ago — this may be the oldest algorithm you will ever implement.
  • Every HTTPS handshake your browser performs runs this exact square-and-multiply loop, just with huge integers under a modulus — binary exponentiation is quietly executed billions of times a day.
  • The square-and-multiply skeleton generalises to any associative operation: matrix powers for O(log n) Fibonacci, string repetition, even function composition.

Asked at

GoogleAmazonMetaMicrosoftAppleBloomberg
Frequently Sometimes Occasionally
Example 1
Input: x = 2.00000, n = 10
Output: 1024.00000
Example 2
Input: x = 2.10000, n = 3
Output: 9.26100
Example 3
Input: x = 2.00000, n = -2
Output: 0.25000
2^-2 = 1 / 2^2 = 1/4 = 0.25
Constraints

- -100.0 < x < 100.0 - -2^31 <= n <= 2^31 - 1 - n is an integer - Either x is not zero or n > 0 - -10^4 <= x^n <= 10^4

Solve this problem →