Skip to main content

Pow(x, n)

MediumPremium

Implement pow(x, n), which returns x raised to the power n. Don't use a built-in power function.

n can be negative, in which case x^n is 1 / x^-n. The tests round your answer to 5 decimal places before comparing.

Example 1

Input: x = 2.0, n = 10 Output: 1024.0

Example 2

Input: x = 2.0, n = -2 Output: 0.25

2^-2 is 1 / 2^2, which is 1 / 4.

Constraints

  • -100.0 < x < 100.0
  • -2^31 <= n <= 2^31 - 1
  • x isn't 0 when n is negative or 0.
  • The answer is between -10^4 and 10^4 after rounding.