Fast Exponentiation Power in Log N in DSA Rust - Time & Space Complexity
We want to understand how fast the power calculation grows as the exponent gets bigger.
How does the number of steps change when we raise a number to a large power?
Analyze the time complexity of the following code snippet.
fn fast_power(base: i64, exponent: i64) -> i64 {
if exponent == 0 {
return 1;
}
let half = fast_power(base, exponent / 2);
if exponent % 2 == 0 {
half * half
} else {
half * half * base
}
}
This code calculates the power of a number using a method that splits the problem in half each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls that halve the exponent each time.
- How many times: About log base 2 of the exponent times.
Each step cuts the exponent roughly in half, so the number of steps grows slowly as the exponent grows.
| Input Size (exponent) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Doubling the exponent adds only one more step.
Time Complexity: O(log n)
This means the steps needed grow very slowly compared to the size of the exponent.
[X] Wrong: "The function runs in linear time because it looks like it calls itself many times."
[OK] Correct: Each call reduces the problem size by half, so the total calls grow logarithmically, not linearly.
Knowing how to quickly calculate powers is a useful skill that shows you understand efficient problem solving.
"What if we changed the code to multiply the base exponent times without halving? How would the time complexity change?"
Practice
Solution
Step 1: Understand repeated multiplication time
Repeated multiplication multiplies the base n times, so it takes O(n) time.Step 2: Understand fast exponentiation time
Fast exponentiation uses squaring and halves the exponent each step, so it takes O(log n) time.Final Answer:
It reduces the time complexity from O(n) to O(log n) -> Option AQuick Check:
Fast exponentiation = O(log n) [OK]
- Thinking it avoids multiplication
- Confusing time complexity with memory
- Believing it only works for positive exponents
Solution
Step 1: Check parameters needed
Fast exponentiation needs base and exponent as inputs, both integers.Step 2: Check return type
It returns the computed power as an integer, so return type is i32.Final Answer:
fn fast_pow(base: i32, exp: i32) -> i32 -> Option AQuick Check:
Correct signature includes base, exp, returns i32 [OK]
- Missing exponent parameter
- Wrong return type (void)
- Swapping parameter order incorrectly
fn fast_pow(mut base: i32, mut exp: i32) -> i32 {
let mut result = 1;
while exp > 0 {
if exp % 2 == 1 {
result *= base;
}
base *= base;
exp /= 2;
}
result
}
fn main() {
println!("{}", fast_pow(3, 5));
}Solution
Step 1: Understand fast_pow(3, 5)
3^5 = 3*3*3*3*3 = 243.Step 2: Trace the code logic
Loop squares base and halves exp, multiplying result when exp is odd, correctly computing 243.Final Answer:
243 -> Option BQuick Check:
3^5 = 243 [OK]
- Calculating 5^3 instead of 3^5
- Confusing multiplication steps
- Forgetting to multiply result when exp is odd
fn fast_pow(base: i32, mut exp: i32) -> i32 {
let mut result = 1;
let mut base = base;
while exp > 0 {
if exp % 2 == 0 {
result *= base;
}
base *= base;
exp /= 2;
}
result
}Solution
Step 1: Check condition for multiplying result
Multiplying result should happen when exp is odd (exp % 2 == 1), not even.Step 2: Confirm other parts
Mutable base is correct, result starts at 1, loop condition is correct.Final Answer:
The condition should check if exp % 2 == 1, not 0 -> Option DQuick Check:
Multiply result only when exp is odd [OK]
- Multiplying when exponent is even
- Starting result at 0
- Loop running one extra time
Solution
Step 1: Understand modulo exponentiation
To compute (base^exp) % mod efficiently, apply modulo after each multiplication to avoid overflow.Step 2: Modify fast exponentiation
Insert modulo operation after multiplying result and squaring base to keep values within range.Final Answer:
Add modulo operation after each multiplication to keep numbers small -> Option CQuick Check:
Modulo after each step prevents overflow [OK]
- Using floats causing precision errors
- Not applying modulo each step causing overflow
- Incorrectly multiplying on even exponent
Ready to prove it?
Pass the test and this pattern moves unknown
