Implement pow(x, n), which calculates x raised to the power n (xn).
Input: 2.10000, 3
-100.0 < x < 100.0
n is a 32-bit signed integer, within the range [−2^31, 2^31 − 1]
Two branch for computing
pow(x, n) with divide and conquer approach.
n is odd, pow(x, n) = x * pow(x, n-1)
n is even, pow(x, n) = pow(x, n/2) * pow(x, n/2).
Note when x is equal with 1.0, we could do a optimization here.
Another corner case is when
n is -2^31,
-n will trigger a integer overflow.
Join my Email List for more insights, It's Free!😋