Pow(x, n)
MediumMath & Geometry
Implement my_pow(x, n), which returns x raised to the integer power n (that is, x^n).
nmay be negative:x^nis then1 / x^(-n).x^0is1for anyxin range.
n can be as large as about two billion in magnitude, so multiplying x by itself |n| times is too slow. Do not use ** or math.pow. Answers are accepted if they are within 10^-5 of the exact value.
Example 1
- Input:
- x = 3.0, n = 5
- Output:
- 243.0
- Explanation
- 35 = 3 * 3 * 3 * 3 * 3 = 243.
Example 2
- Input:
- x = 4.0, n = -2
- Output:
- 0.0625
- Explanation
- A negative exponent means the reciprocal: 4-2 = 1 / 42 = 1 / 16 = 0.0625.
Example 3
- Input:
- x = -1.5, n = 3
- Output:
- -3.375
- Explanation
- (-1.5)3 = -3.375. An odd power of a negative base stays negative.
Constraints
-100.0 < x < 100.0-2^31 <= n <= 2^31 - 1- Either
xis not zero orn > 0 -10^4 <= x^n <= 10^4