Bird
0
0
DSA Cprogramming

Fast Exponentiation Power in Log N in DSA C

Choose your learning style9 modes available
Mental Model
We can find a number raised to a power quickly by breaking the problem into smaller parts using repeated squaring.
Analogy: Imagine folding a paper multiple times to reach a big thickness quickly instead of stacking sheets one by one.
power(n, e)
  e even -> power(n * n, e / 2)
  e odd  -> n * power(n * n, (e - 1) / 2)
Dry Run Walkthrough
Input: Calculate 3^13 using fast exponentiation
Goal: Compute 3 raised to the power 13 efficiently using repeated squaring
Step 1: Since 13 is odd, separate one 3 and reduce power to 12
result = 3 * power(3 * 3, 6)
Why: Odd powers are handled by taking one base out and making the exponent even
Step 2: Calculate power(9, 6) since 3*3=9 and 6 is even
power(9, 6) = power(9 * 9, 3) = power(81, 3)
Why: For even powers, square the base and halve the exponent
Step 3: Since 3 is odd, separate one 81 and reduce power to 2
power(81, 3) = 81 * power(81 * 81, 1) = 81 * power(6561, 1)
Why: Odd powers handled by separating one base and reducing exponent
Step 4: Calculate power(6561, 1) which is base itself
power(6561, 1) = 6561
Why: Power of 1 returns the base
Step 5: Multiply back all separated bases: 3 * 81 * 6561
result = 3 * 81 * 6561 = 1594323
Why: Combine all parts to get final power
Result:
1594323
Annotated Code
DSA C
#include <stdio.h>

// Function to compute n^e using fast exponentiation
long long fast_power(long long n, int e) {
    if (e == 0) return 1; // base case: any number^0 = 1
    if (e % 2 == 0) {
        // even exponent: square base and halve exponent
        long long half = fast_power(n * n, e / 2);
        return half;
    } else {
        // odd exponent: separate one base and reduce exponent
        return n * fast_power(n * n, (e - 1) / 2);
    }
}

int main() {
    long long base = 3;
    int exponent = 13;
    long long result = fast_power(base, exponent);
    printf("%lld\n", result);
    return 0;
}
if (e == 0) return 1; // base case: any number^0 = 1
stop recursion when exponent is zero
if (e % 2 == 0) {
check if exponent is even
long long half = fast_power(n * n, e / 2);
square base and halve exponent for even power
return half;
return result of recursive call for even exponent
return n * fast_power(n * n, (e - 1) / 2);
for odd exponent, multiply base once and recurse with squared base and halved exponent
OutputSuccess
1594323
Complexity Analysis
Time: O(log e) because the exponent is halved at each recursive call
Space: O(log e) due to recursion call stack depth proportional to log of exponent
vs Alternative: Naive approach takes O(e) time by multiplying base e times, which is slower for large exponents
Edge Cases
exponent is zero
returns 1 immediately as any number to power 0 is 1
DSA C
if (e == 0) return 1;
exponent is one
returns base itself without further recursion
DSA C
return n * fast_power(n * n, (e - 1) / 2);
large exponent
efficiently computes power in logarithmic steps without overflow in recursion depth
DSA C
recursive calls halve exponent each time
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 e times instead of using repeated squaring
Fix: Use recursion or loop to square base and halve exponent to reduce steps
Mistake: Not handling odd exponents separately
Fix: Separate one base multiplication for odd exponents before recursion
Mistake: Forgetting base case when exponent is zero
Fix: Add condition to return 1 when exponent is zero
Summary
Computes power of a number efficiently using repeated squaring.
Use when you need to calculate large powers quickly without multiplying many times.
The key is to reduce the exponent by half each step by squaring the base.