What is the main idea behind Fast Exponentiation (Power) algorithm?
It reduces the number of multiplications by repeatedly squaring the base and using the binary form of the exponent, achieving power calculation in logarithmic time.
Click to reveal answer
beginner
Why does Fast Exponentiation run in O(log n) time instead of O(n)?
Because it splits the exponent by half in each step using exponent halving, so the number of steps grows logarithmically with the exponent size.
Click to reveal answer
intermediate
In Fast Exponentiation, what do you do when the exponent is odd?
Multiply the result by the base once, then reduce the exponent by 1 to make it even, and continue squaring.
Click to reveal answer
intermediate
Show the recursive formula for Fast Exponentiation power calculation.
power(base, exp) = power(base * base, exp / 2) if exp is even; else base * power(base, exp - 1) if exp is odd.
Click to reveal answer
beginner
What is the output of fastExponentiation(2, 10)?
1024, because 2^10 = 1024, calculated efficiently using repeated squaring.
Click to reveal answer
What is the time complexity of Fast Exponentiation algorithm?
AO(log n)
BO(n)
CO(n^2)
DO(1)
✗ Incorrect
Fast Exponentiation reduces the exponent by half each step, so it runs in logarithmic time.
In Fast Exponentiation, if the exponent is even, what is the next step?
AMultiply result by base and reduce exponent by 1
BSquare the base and halve the exponent
CReturn 1 immediately
DAdd 1 to the exponent
✗ Incorrect
When exponent is even, square the base and halve the exponent to reduce problem size.
What happens when the exponent reaches zero in Fast Exponentiation?
AReturn 1
BReturn base
CReturn 0
DReturn exponent
✗ Incorrect
Any number to the power 0 is 1, so return 1 as base case.
Which of these is NOT a benefit of Fast Exponentiation?
AFaster calculation for large exponents
BUses less memory than naive method
CWorks only for positive exponents
DReduces multiplications drastically
✗ Incorrect
Fast Exponentiation can be adapted for negative exponents; it is not limited to positive only.
What is the result of fastExponentiation(3, 5)?
A244
B125
C242
D243
✗ Incorrect
3^5 = 243, calculated efficiently by fast exponentiation.
Explain how Fast Exponentiation reduces the number of multiplications compared to the naive method.
Think about how the exponent is split in half each time.
You got /4 concepts.
Describe the base case and recursive steps in the Fast Exponentiation algorithm.
Focus on what happens when exponent is zero, even, or odd.
You got /3 concepts.
Practice
(1/5)
1. What is the main advantage of using Fast Exponentiation to calculate baseexponent compared to simple repeated multiplication?
easy
A. It only works for exponents that are powers of two.
B. It uses less memory by storing all intermediate powers.
C. It avoids multiplication completely by using addition.
D. It reduces the number of multiplications from linear to logarithmic in the exponent.
Solution
Step 1: Understand simple repeated multiplication
Calculating baseexponent by multiplying base by itself exponent times takes exponent multiplications, which is linear time.
Step 2: Understand fast exponentiation approach
Fast exponentiation reduces multiplications by squaring results and halving the exponent, achieving logarithmic time complexity.
Final Answer:
It reduces the number of multiplications from linear to logarithmic in the exponent. -> Option D
Quick Check:
Fast exponentiation = fewer multiplications [OK]
Hint: Fast exponentiation halves exponent steps, speeding up power calculation [OK]
Common Mistakes:
Thinking it avoids multiplication entirely
Believing it only works for powers of two
Confusing memory use with speed
2. Which of the following JavaScript function definitions correctly implements fast exponentiation for positive integers?
easy
A. function power(base, exp) { if (exp === 0) return 1; let half = power(base, Math.floor(exp / 2)); return exp % 2 === 0 ? half * half : half * half * base; }
B. function power(base, exp) { let result = 1; for (let i = 0; i < exp; i++) { result *= base; } return result; }
C. function power(base, exp) { return base ** exp; }
D. function power(base, exp) { if (exp === 1) return base; return power(base, exp - 1) * base; }
Solution
Step 1: Identify fast exponentiation pattern
Fast exponentiation uses recursion with halving the exponent and squaring intermediate results.
Step 2: Check each function
function power(base, exp) { if (exp === 0) return 1; let half = power(base, Math.floor(exp / 2)); return exp % 2 === 0 ? half * half : half * half * base; } uses recursion, halves the exponent, and squares results correctly. The other options use linear multiplication or built-in operator, not fast exponentiation.
Final Answer:
implements fast exponentiation correctly. -> Option A
Quick Check:
Recursion + halving exponent = fast exponentiation [OK]
Hint: Look for recursion with exponent halving and squaring [OK]
Common Mistakes:
Choosing simple loops instead of recursion
Using built-in power operator without algorithm
Confusing base case conditions
3. What is the output of the following JavaScript code?
function fastPower(base, exp) {
if (exp === 0) return 1;
let half = fastPower(base, Math.floor(exp / 2));
if (exp % 2 === 0) return half * half;
else return half * half * base;
}
console.log(fastPower(2, 5));
medium
A. 32
B. 25
C. 16
D. 10
Solution
Step 1: Trace recursive calls for fastPower(2, 5)
fastPower(2,5) calls fastPower(2,2), which calls fastPower(2,1), which calls fastPower(2,0)=1.
Hint: Trace recursion bottom-up, square halves, multiply base if odd [OK]
Common Mistakes:
Calculating 2^5 as 16 by squaring only once
Confusing exponent halving with division
Missing multiplication by base when exponent is odd
4. The following code attempts to implement fast exponentiation but has a bug. What is the bug?
function fastPower(base, exp) {
if (exp === 0) return 1;
let half = fastPower(base, exp / 2);
if (exp % 2 === 0) return half * half;
else return half * half * base;
}
medium
A. The base case should return 0 instead of 1.
B. The exponent is not floored before recursive call, causing infinite recursion for odd exponents.
C. The multiplication order is incorrect for odd exponents.
D. The function does not handle negative exponents.
Solution
Step 1: Analyze recursive call argument
The recursive call uses exp / 2 without flooring, so for odd exponents it passes a decimal, causing infinite recursion.
Step 2: Identify fix
Using Math.floor(exp / 2) ensures the exponent decreases properly and recursion ends.
Final Answer:
The exponent is not floored before recursive call, causing infinite recursion for odd exponents. -> Option B
Quick Check:
Floor exponent before recursion to avoid infinite calls [OK]
Hint: Always floor exponent division in recursion [OK]
Common Mistakes:
Ignoring decimal exponents in recursion
Changing base case return value incorrectly
Assuming multiplication order causes bugs
5. You want to compute 313 modulo 7 efficiently using fast exponentiation. Which of the following code snippets correctly implements this with modulo at each multiplication step to avoid large numbers?
hard
A. function modPower(base, exp, mod) {
if (exp === 0) return 0;
return (base * modPower(base, exp - 1, mod)) % mod;
}
console.log(modPower(3, 13, 7));
B. function modPower(base, exp, mod) {
let result = 1;
for (let i = 0; i < exp; i++) {
result = (result * base) % mod;
}
return result;
}
console.log(modPower(3, 13, 7));
C. function modPower(base, exp, mod) {
if (exp === 0) return 1;
let half = modPower(base, Math.floor(exp / 2), mod);
let halfSquared = (half * half) % mod;
if (exp % 2 === 0) return halfSquared;
else return (halfSquared * base) % mod;
}
console.log(modPower(3, 13, 7));
D. function modPower(base, exp, mod) {
return (base ** exp) % mod;
}
console.log(modPower(3, 13, 7));
Solution
Step 1: Understand modulo in fast exponentiation
Modulo must be applied after each multiplication to keep numbers small and correct.
Step 2: Check each option
function modPower(base, exp, mod) {
if (exp === 0) return 1;
let half = modPower(base, Math.floor(exp / 2), mod);
let halfSquared = (half * half) % mod;
if (exp % 2 === 0) return halfSquared;
else return (halfSquared * base) % mod;
}
console.log(modPower(3, 13, 7)); applies modulo after squaring and multiplication recursively. function modPower(base, exp, mod) {
let result = 1;
for (let i = 0; i < exp; i++) {
result = (result * base) % mod;
}
return result;
}
console.log(modPower(3, 13, 7)); uses linear loop (correct but slower). function modPower(base, exp, mod) {
if (exp === 0) return 0;
return (base * modPower(base, exp - 1, mod)) % mod;
}
console.log(modPower(3, 13, 7)); returns 0 for base case (wrong). function modPower(base, exp, mod) {
return (base ** exp) % mod;
}
console.log(modPower(3, 13, 7)); uses built-in power without modulo at each step (incorrect for large exponents).
Final Answer:
correctly implements fast exponentiation with modulo at each step. -> Option C
Quick Check:
Modulo after each multiply keeps numbers small [OK]
Hint: Apply modulo after every multiplication step [OK]
Common Mistakes:
Not applying modulo at intermediate steps
Wrong base case returning 0
Using built-in power without modulo
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.