← Whiteboard questions

Pow(x, n)

Raise a decimal to a whole-number power

DSA, Medium, Math & Geometry

Which structure or algorithm, and what does it cost.

Write my_pow(x, n) that returns x raised to the whole-number power n. The number x is a decimal. The power n can be negative, and a power of 0 gives 1. Do not use a built-in power function or operator. Any answer within 0.00001 of the exact value is accepted.

Examples

Input x = 1.5, n = 4
Output 5.0625
Why 1.5 times itself four times is 5.0625.
Input x = 4.0, n = -2
Output 0.0625
Why a negative power is 1 divided by the positive power: 1 / 16.
Input x = -2.0, n = 5
Output -32.0
Why an odd power keeps the minus sign.

Constraints

Your answer must contain

Target Aim for O(log n) time, where n is the size of the power, and O(1) extra memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Doing n multiplications is far too slow when n is two billion. Look for a way to cut the work down much faster.

Hint 2 the approach

Squaring a number doubles its power. Think about building the power you need from halves.

Hint 3 the key step

Write the power in binary. Square a base every round. Multiply it into the answer when the lowest bit is 1. For a negative n, invert x first.

Walkthrough

Full walkthrough brute force to best

Brute force

Multiply 1.0 by x, |n| times, and take 1 divided by the answer if n is negative. With n up to two billion that is up to 2,000,000,000 multiplications: O(|n|) time, far too slow, with O(1) memory.

Best method

Use the binary digits of the power. Keep a base, which starts as x, and a result, which starts as 1. If the lowest bit of n is 1, multiply the result by the base. Then square the base and drop the lowest bit of n. Repeat until n is 0. For a negative n, first replace x by 1 / x and n by -n. This is correct because the result times the base to the power of the remaining n always equals x to the power of the first n. When the remaining n is even, the base squared with half the power is the same value. When it is odd, one factor of the base moves into the result.

Hand trace

Run on x = 1.5, n = 4. Start with result 1.0, base 1.5 and n 4. The lowest bit of 4 is 0, so skip. Square the base to 2.25, and n becomes 2. The lowest bit of 2 is 0, so skip. Square the base to 5.0625, and n becomes 1. The lowest bit is 1, so the result becomes 5.0625. Square again, and n becomes 0, so stop. The result is 5.0625.

Code

def my_pow(x, n):
    if n < 0:
        x, n = 1 / x, -n
    result = 1.0
    while n:
        if n & 1:
            result *= x
        x *= x
        n >>= 1
    return result

Time and memory

O(log |n|) time, because n loses one bit per round: at most 31 rounds for two billion. O(1) memory: a base, a result and the power.

Common mistakes

  • Looping n times. For n of two billion the program does not finish in time.
  • Forgetting the powers 0 and below 0. A power of 0 must give 1.0, and a negative power needs 1 / x.
  • Shifting a negative n right. The value -1 stays -1 forever, so the loop never ends. Make n positive first.

Related problems

More Math & Geometry problems
Pow(x, n): hints, walkthrough and Python solution