Skip to content
BytePatterns

Fast Power

MediumRecursion#recursion#divide-and-conquer~20m

Problem

Raise a number to an integer exponent without multiplying it by itself that many times. The exponent may be zero or negative, and a negative exponent means one divided by the positive-exponent result. Return the value.

Examples

Input:  base = 2, exp = 10
Output: 1024
Why:    ten multiplications are not needed; four calls suffice
Input:  base = 2, exp = -3
Output: 0.125
Why:    a negative exponent inverts the positive result
Input:  base = 3, exp = 0
Output: 1
Why:    edge case, any base to the zero is one

Hints

0 / 3

Stuck on the idea rather than the code? Factorial and Fibonacci covers it.