📖 Scenario: Imagine you are building a calculator app that needs to quickly find the power of a number. Calculating powers by multiplying the base repeatedly can be slow for big numbers. Using fast exponentiation, you can find the power much faster, like a shortcut in math.
🎯 Goal: You will write a program in TypeScript that calculates base raised to the power exponent using fast exponentiation. This method works in O(log n) time, which means it is very fast even for large exponents.
📋 What You'll Learn
Create two variables: base and exponent with exact values 3 and 13 respectively.
Create a variable result initialized to 1 to hold the final answer.
Use a while loop to implement fast exponentiation logic using base, exponent, and result.
Print the final value of result after the loop ends.
💡 Why This Matters
🌍 Real World
Fast exponentiation is used in calculators, computer graphics, cryptography, and anywhere powers of numbers are needed quickly.
💼 Career
Understanding fast exponentiation helps in algorithm design, optimizing code, and working with encryption algorithms in software development.
Progress0 / 4 steps
1
Create base and exponent variables
Create a variable called base and set it to 3. Create another variable called exponent and set it to 13.
DSA Typescript
Hint
Use let to create variables and assign the exact numbers.
2
Initialize result variable
Create a variable called result and set it to 1. This will hold the final power value.
DSA Typescript
Hint
Start with 1 because multiplying by 1 does not change the value.
3
Implement fast exponentiation loop
Use a while loop that runs while exponent is greater than 0. Inside the loop, if exponent % 2 === 1, multiply result by base. Then square base by multiplying it by itself. Finally, divide exponent by 2 using integer division (use Math.floor).
DSA Typescript
Hint
Remember to update result only when the exponent is odd, then square the base and halve the exponent.
4
Print the final result
Write console.log(result) to print the final power value after the loop.
DSA Typescript
Hint
The output should be the number 3 raised to the power 13.
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.