Exponentiation by Squaring with Negative Exponents
Math & Bit ManipulationAuto-graded · 23 test casesLast updated
Compute a floating-point base raised to a signed 32-bit integer exponent in logarithmic time, including negative exponents, using repeated squaring.
Fast Integer Powers
Write a function that computes base raised to the integer power exponent, without using the built-in power operator (**), pow, or math.pow.
The base is a floating-point number and the exponent is a signed 32-bit integer, so it can be positive, zero, or negative. The function returns the result as a float.
Rules
- A positive exponent means multiplying
baseby itself that many times. - A negative exponent means the reciprocal:
base^(-m) = 1 / base^m. - Any base raised to the exponent
0gives1.0. - A negative base is allowed. The sign of the result follows from ordinary multiplication.
- Multiplying
baseby itself|exponent|times is too slow for large exponents. Your solution should use a number of multiplications proportional to the number of bits inexponent, which isO(log |exponent|). - Results are floating-point values, so tiny rounding differences are expected. The standard repeated-squaring approach produces the intended values.
Constraints
-2^31 <= exponent <= 2^31 - 1baseis a finite floating-point number.- If
exponent < 0, thenbase != 0. - The true result fits within the usual range of a double-precision float.
Examples
Example 1
Input: fast_power(2, -2)
Output: 0.25
Example 2
Input: fast_power(2, 10)
Output: 1024
Example 3
Input: fast_power(2.1, 3)
Output: 9.261000000000001
Hints
Hint 1
Write the exponent in binary. For example, x^13 = x^8 * x^4 * x^1, because 13 is 1101 in binary.
Hint 2
Keep a running 'current power' that is squared on every step (x, x^2, x^4, ...). Whenever the lowest bit of the exponent is 1, multiply that value into the result, then shift the exponent right by one.
Hint 3
Handle a negative exponent once, up front, by replacing the base with its reciprocal and negating the exponent. Python integers do not overflow, so negating -2^31 is safe.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Use exponentiation by squaring.
- If the exponent is negative, replace
basewith1 / baseand make the exponent positive. This is valid becausebase^(-m) = (1/base)^m. - Keep
result = 1.0. Then loop while the exponent is non-zero:- If the lowest bit of the exponent is 1, multiply
resultby the currentbase. - Square
base. - Shift the exponent right by one bit.
- If the lowest bit of the exponent is 1, multiply
- When the exponent reaches 0,
resultholds the answer.
Why it is correct
The loop keeps this invariant: result * base^exponent equals the original base^exponent.
- If the exponent is odd, moving one factor of
baseintoresultleaves an even exponent and does not change the product. - Squaring
basewhile halving the exponent gives(b^2)^(e/2) = b^e, so the product is unchanged. - At exponent 0,
base^0 = 1, soresultequals the original power.
A negative base needs no special handling, since the sign emerges from the multiplications. An exponent of 0 skips the loop and returns 1.0.
Complexity
- Time:
O(log |exponent|), with one iteration per bit, so at most about 32 iterations here. - Space:
O(1).
Reference solution
def fast_power(base, exponent):
if exponent < 0:
base = 1.0 / base
exponent = -exponent
result = 1.0
while exponent:
if exponent & 1:
result *= base
base *= base
exponent >>= 1
return result