Implement pow(x, n), which calculates x
raised to the power n
(i.e., xn
).
Example 1:
Input: x = 2.00000, n = 10 Output: 1024.00000
Example 2:
Input: x = 2.10000, n = 3 Output: 9.26100
Example 3:
Input: x = 2.00000, n = -2 Output: 0.25000 Explanation: 2-2 = 1/22 = 1/4 = 0.25
Constraints:
-100.0 < x < 100.0
-231 <= n <= 231-1
n
is an integer.x
is not zero or n > 0
.-104 <= xn <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The basic idea is to repeatedly multiply the base number by itself. If the exponent is positive, we just multiply the base that many times. If the exponent is negative, we'll deal with that case separately, because it is equivalent to one divided by the base raised to the positive exponent.
Here's how the algorithm would work step-by-step:
def power_function_brute_force(base, exponent):
if exponent == 0:
return 1
if exponent > 0:
result = base
# Repeatedly multiply the base by itself
exponent_tracker = exponent - 1
while exponent_tracker > 0:
result *= base
exponent_tracker -= 1
return result
# Account for negative exponent by taking reciprocal
else:
positive_exponent = abs(exponent)
result = base
# Repeatedly multiply the base by itself
exponent_tracker = positive_exponent - 1
while exponent_tracker > 0:
result *= base
exponent_tracker -= 1
# Invert result for negative powers
return 1 / result
Calculating a number to a large power by multiplying it repeatedly is slow. A faster way involves repeatedly squaring the base and adjusting the exponent, effectively reducing the number of multiplication operations required.
Here's how the algorithm would work step-by-step:
def power(base, exponent):
if exponent == 0:
return 1
if exponent == 1:
return base
# Handle negative exponents by inverting the base.
if exponent < 0:
base = 1 / base
exponent = -exponent
result = 1
# Repeatedly square the base and halve the exponent.
while exponent > 0:
# If the exponent is odd, multiply the result by the base.
if exponent % 2 == 1:
result *= base
base *= base
# Integer division to halve the exponent.
exponent //= 2
return result
Case | How to Handle |
---|---|
x is 0 and n is negative | Return positive infinity (or throw an exception depending on language) as division by zero is undefined. |
x is 0 and n is 0 | Return 1 as 0^0 is generally considered 1. |
n is Integer.MIN_VALUE (most negative integer) | Handle potential overflow when negating n by first converting x to its inverse if x!=1 and x!=-1 and then processing it correctly, or consider long. |
x is 1 | Return 1 immediately as 1 raised to any power is 1. |
x is -1 and n is even | Return 1. |
x is -1 and n is odd | Return -1. |
n is 0 | Return 1 as any number raised to the power of 0 is 1. |
Large x and n leading to potential floating point overflow | Be aware of the limits of floating point representation and the potential for overflow, and potentially suggest using logarithms and exponentiation if extremely large numbers are expected. |