Pow(x, n)
2 approaches, code in every language
Problem description
Implement exponentiation. Given a real base x and an integer exponent n, return x raised to the power n, written x^n.
The exponent can be zero or negative. A negative exponent means the reciprocal of the corresponding positive power, so x^(-k) is 1 divided by x^k.
Example 1
Input: x = 2.00000, n = 10
Output: 1024.00000
Example 2
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 and n is an integerEither x is non-zero or n is greater than 0The final result stays within the range [−10^4, 10^4]
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Zoom Stage Multiplier is our own problem on the technique behind Pow(x, n): a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free