What is the main idea behind Fast Exponentiation (Power) algorithm?
It reduces the number of multiplications by using the property that x^n = (x^(n/2))^2 when n is even, and x^n = x * x^(n-1) when n is odd, allowing calculation in logarithmic time.
Click to reveal answer
beginner
Why is Fast Exponentiation faster than the naive method?
Because it uses repeated squaring to halve the exponent at each step, reducing the time complexity from O(n) to O(log n).
Click to reveal answer
intermediate
In Fast Exponentiation, what happens when the exponent is odd?
We multiply the result by the base once and then reduce the exponent by 1 to make it even, continuing with repeated squaring.
Click to reveal answer
beginner
Show the TypeScript function signature for Fast Exponentiation.
function fastPower(base: number, exponent: number): number
Click to reveal answer
beginner
What is the time complexity of Fast Exponentiation and why?
O(log n) because the exponent is divided by 2 at each step, drastically reducing the number of multiplications needed.
Click to reveal answer
What is the time complexity of Fast Exponentiation?
AO(log n)
BO(n)
CO(n^2)
DO(1)
✗ Incorrect
Fast Exponentiation reduces the exponent by half each step, so it runs in O(log n) time.
In Fast Exponentiation, if the exponent is even, what is the next step?
AMultiply result by base and reduce exponent by 1
BAdd 1 to the exponent
CSquare the base and halve the exponent
DReturn 1 immediately
✗ Incorrect
When exponent is even, we square the base and halve the exponent to reduce the problem size.
What does Fast Exponentiation compute?
AProduct of two numbers
BPower of a number (base^exponent)
CSum of numbers
DSquare root of a number
✗ Incorrect
Fast Exponentiation efficiently computes base raised to the power of exponent.
Which of these is a key step in Fast Exponentiation when exponent is odd?
AMultiply result by base once and reduce exponent by 1
BIgnore the odd exponent
CDouble the exponent
DDivide base by 2
✗ Incorrect
For odd exponent, multiply result by base once and then reduce exponent by 1 to make it even.
Which of these best describes the Fast Exponentiation algorithm?
ASorting algorithm
BIterative addition
CLinear search
DRepeated squaring and multiplication
✗ Incorrect
Fast Exponentiation uses repeated squaring and multiplication to compute power efficiently.
Explain how Fast Exponentiation reduces the number of multiplications compared to the naive method.
Think about how the exponent changes each step.
You got /4 concepts.
Describe the steps of the Fast Exponentiation algorithm when given a base and an exponent.
Focus on how the exponent changes and what happens to the result.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using Fast Exponentiation to calculate a^b compared to the simple repeated multiplication?
easy
A. It reduces the time complexity from linear to logarithmic in terms of exponent.
B. It uses less memory by storing all intermediate powers.
Forgetting to multiply by base when exponent is odd
Incorrect base case handling
4. Identify the bug in this fast exponentiation function and select the correct fix:
function fastPower(a: number, b: number): number {
if (b === 0) return 0;
let half = fastPower(a, Math.floor(b / 2));
if (b % 2 === 0) return half * half;
else return half * half * a;
}
medium
A. Change recursive call to fastPower(a, b - 1)
B. Change base case return from 0 to 1
C. Remove the multiplication by 'a' when exponent is odd
D. Change Math.floor to Math.ceil in recursive call
Solution
Step 1: Check base case correctness
When exponent is 0, a^0 should return 1, but code returns 0, which is incorrect.
Step 2: Identify fix for base case
Changing return value in base case from 0 to 1 fixes the function to correctly compute powers.
Final Answer:
Change base case return from 0 to 1 -> Option B
Quick Check:
Base case must return 1 [OK]
Hint: Base case for exponent 0 must return 1 [OK]
Common Mistakes:
Returning 0 for exponent 0
Changing recursive call incorrectly
Removing multiplication for odd exponent
5. You want to compute (3^13) % 7 efficiently using fast exponentiation with modulo to avoid large numbers. Which modification to the fastPower function is correct?
hard
A. Apply modulo after each multiplication: (half * half) % mod and (half * half * a) % mod
B. Apply modulo only once at the end of recursion
C. Apply modulo only to the base a before recursion
D. No modulo needed if exponent is small
Solution
Step 1: Understand modulo property in exponentiation
To avoid large intermediate values, modulo must be applied after every multiplication step.
Step 2: Modify function to apply modulo after multiplications
Applying modulo after half * half and half * half * a keeps numbers small and correct.
Final Answer:
Apply modulo after each multiplication: (half * half) % mod and (half * half * a) % mod -> Option A
Quick Check:
Modulo after each multiply keeps numbers small [OK]
Hint: Modulo after every multiply prevents overflow [OK]
Common Mistakes:
Applying modulo only once at end
Applying modulo only to base
Ignoring modulo for small exponents
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.