🧠
Recursive Fast Exponentiation (Divide and Conquer)
💡 This approach improves efficiency by reducing the number of multiplications using recursion and the property that x^n = (x^(n/2))^2.
Intuition
Instead of multiplying x n times, recursively compute x^(n/2) and square it. If n is odd, multiply by x once more.
Algorithm
- If n is zero, return 1.
- If n is negative, convert to positive and invert result later.
- Recursively compute half = myPow(x, n//2).
- Square half to get half * half.
- If n is odd, multiply by x once more.
- Return the result, invert if original n was negative.
💡 The recursion and halving make it less obvious at first, but it reduces the problem size exponentially.
def myPow(x: float, n: int) -> float:
if n == 0:
return 1.0
negative = n < 0
n = abs(n)
half = myPow(x, n // 2)
if n % 2 == 0:
result = half * half
else:
result = half * half * x
return 1 / result if negative else result
# Driver code
if __name__ == '__main__':
print(myPow(2.0, 10)) # 1024.0
print(myPow(2.1, 3)) # 9.261
print(myPow(2.0, -2)) # 0.25
Line Notes
if n == 0:Base case: any number to power 0 is 1
negative = n < 0Track if exponent is negative for inversion
half = myPow(x, n // 2)Recursive call to compute half power
if n % 2 == 0:Check if exponent is even or odd to decide multiplication
return 1 / result if negative else resultInvert result if original exponent was negative
public class Solution {
public static double myPow(double x, int n) {
if (n == 0) return 1.0;
boolean negative = n < 0;
long N = Math.abs((long)n);
double half = myPowHelper(x, N);
return negative ? 1 / half : half;
}
private static double myPowHelper(double x, long n) {
if (n == 0) return 1.0;
double half = myPowHelper(x, n / 2);
if (n % 2 == 0) {
return half * half;
} else {
return half * half * x;
}
}
public static void main(String[] args) {
System.out.println(myPow(2.0, 10)); // 1024.0
System.out.println(myPow(2.1, 3)); // 9.261
System.out.println(myPow(2.0, -2)); // 0.25
}
}
Line Notes
if (n == 0) return 1.0;Base case for recursion
boolean negative = n < 0;Flag for negative exponent
double half = myPowHelper(x, N);Compute power recursively
if (n % 2 == 0)Check even/odd exponent to decide multiplication
return negative ? 1 / half : half;Invert result if original exponent was negative
#include <iostream>
#include <cmath>
using namespace std;
double myPowHelper(double x, long long n) {
if (n == 0) return 1.0;
double half = myPowHelper(x, n / 2);
if (n % 2 == 0) {
return half * half;
} else {
return half * half * x;
}
}
double myPow(double x, int n) {
if (n == 0) return 1.0;
bool negative = n < 0;
long long N = abs((long long)n);
double result = myPowHelper(x, N);
return negative ? 1 / result : result;
}
int main() {
cout << myPow(2.0, 10) << endl; // 1024.0
cout << myPow(2.1, 3) << endl; // 9.261
cout << myPow(2.0, -2) << endl; // 0.25
return 0;
}
Line Notes
if (n == 0) return 1.0;Base case for recursion
double half = myPowHelper(x, n / 2);Recursive call to compute half power
if (n % 2 == 0)Check if exponent is even or odd
return negative ? 1 / result : result;Invert result if exponent was negative
function myPowHelper(x, n) {
if (n === 0) return 1.0;
let half = myPowHelper(x, Math.floor(n / 2));
if (n % 2 === 0) {
return half * half;
} else {
return half * half * x;
}
}
function myPow(x, n) {
if (n === 0) return 1.0;
let negative = n < 0;
n = Math.abs(n);
let result = myPowHelper(x, n);
return negative ? 1 / result : result;
}
// Test cases
console.log(myPow(2.0, 10)); // 1024.0
console.log(myPow(2.1, 3)); // 9.261
console.log(myPow(2.0, -2)); // 0.25
Line Notes
if (n === 0) return 1.0;Base case for recursion
let half = myPowHelper(x, Math.floor(n / 2));Recursive call to compute half power
if (n % 2 === 0)Check if exponent is even or odd
return negative ? 1 / result : result;Invert result if exponent was negative
TimeO(log n)
SpaceO(log n) due to recursion stack
Each recursive call halves n, so the depth is log n. Multiplications per call are constant.
💡 For n=1024, this means about 10 multiplications instead of 1024, a huge speedup.
Interview Verdict: Accepted
This approach is efficient and accepted in interviews, but recursion stack depth can be a concern for very large n.