0
0
DSA Pythonprogramming

Fast Exponentiation Power in Log N in DSA Python

Choose your learning style9 modes available
Mental Model
To find a number raised to a power quickly by breaking the problem into smaller parts and reusing results.
Analogy: Like folding a paper in half repeatedly to reach a big thickness quickly instead of stacking sheets one by one.
power(n, e)
  ├─ if e is 0 -> return 1
  ├─ else split e into half
  ├─ compute power(n, e//2)
  └─ combine results by squaring or multiplying by n if e is odd
Dry Run Walkthrough
Input: Calculate 3^5 using fast exponentiation
Goal: Compute 3 raised to the power 5 efficiently using repeated squaring
Step 1: Calculate power(3, 5), split exponent 5 into half 2
power(3,5) calls power(3,2)
Why: We break down the problem to smaller powers to reuse calculations
Step 2: Calculate power(3, 2), split exponent 2 into half 1
power(3,2) calls power(3,1)
Why: Keep splitting exponent until base case
Step 3: Calculate power(3, 1), split exponent 1 into half 0
power(3,1) calls power(3,0)
Why: Base case is when exponent is zero
Step 4: power(3, 0) returns 1 (base case)
return 1
Why: Any number to power 0 is 1
Step 5: power(3, 1) computes half_power=1, exponent odd so multiply by base: 1 * 1 * 3 = 3
return 3
Why: For odd exponent, multiply half power squared by base
Step 6: power(3, 2) computes half_power=3, exponent even so square: 3 * 3 = 9
return 9
Why: For even exponent, square the half power
Step 7: power(3, 5) computes half_power=9, exponent odd so multiply by base: 9 * 9 * 3 = 243
return 243
Why: Combine results for odd exponent
Result:
3^5 = 243
Annotated Code
DSA Python
class FastExponentiation:
    def power(self, base: int, exponent: int) -> int:
        if exponent == 0:
            return 1  # base case: any number^0 = 1
        half_power = self.power(base, exponent // 2)  # recursive call for half exponent
        if exponent % 2 == 0:
            return half_power * half_power  # even exponent: square the half power
        else:
            return half_power * half_power * base  # odd exponent: multiply by base once more

# Driver code
fe = FastExponentiation()
result = fe.power(3, 5)
print(result)
if exponent == 0:
check base case to stop recursion
half_power = self.power(base, exponent // 2)
recursive call to compute power for half the exponent
if exponent % 2 == 0:
check if exponent is even to decide how to combine results
return half_power * half_power
square half power for even exponent
return half_power * half_power * base
multiply by base once more for odd exponent
OutputSuccess
243
Complexity Analysis
Time: O(log n) because the exponent is divided by 2 each recursive call, reducing the problem size quickly
Space: O(log n) due to recursion call stack depth proportional to log of exponent
vs Alternative: Naive approach takes O(n) time by multiplying base n times, which is slower than this logarithmic method
Edge Cases
exponent is 0
returns 1 immediately as per definition
DSA Python
if exponent == 0:
exponent is 1
returns base itself after one recursive call
DSA Python
if exponent == 0:
large exponent
efficiently computes power without timeout due to logarithmic steps
DSA Python
half_power = self.power(base, exponent // 2)
When to Use This Pattern
When you need to compute large powers quickly, look for fast exponentiation using repeated squaring to reduce time from linear to logarithmic.
Common Mistakes
Mistake: Multiplying base by itself exponent times directly causing slow performance
Fix: Use recursive halving of exponent and combine results by squaring to achieve logarithmic time
Mistake: Not handling odd exponents correctly by missing the extra multiplication by base
Fix: Check if exponent is odd and multiply the squared half power by base once more
Summary
Computes base raised to exponent quickly using repeated squaring.
Use when you need fast power calculation especially for large exponents.
Split exponent in half recursively and combine results by squaring to save time.