MediumMath · Recursion

Pow(x, n)

Implement myPow(x, n), which returns x raised to the integer power n (which may be negative), without built-in power functions.

x = 2, n = 5 → 32x = 1.1, n = 10 → 2.59374x = 2, n = −3 → 0.125

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

Exponent

Bits of |n| with the power of x each one stands for

Time and space complexity

n means |n|, the size of the exponent. The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. x strictly between −100 and 100, n from −200 to 200 (so the brute force stays steppable), and |xⁿ| ≤ 10⁴ as the problem guarantees.