Challenge - 5 Problems
Fast Exponentiation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fast Exponentiation Function
What is the output of the following Go code that calculates 3 to the power 5 using fast exponentiation?
DSA Go
package main import "fmt" func fastPow(base, exp int) int { result := 1 b := base e := exp for e > 0 { if e&1 == 1 { result *= b } b *= b e >>= 1 } return result } func main() { fmt.Println(fastPow(3, 5)) }
Attempts:
2 left
💡 Hint
Remember that 3^5 means 3 multiplied by itself 5 times.
✗ Incorrect
3^5 = 3 * 3 * 3 * 3 * 3 = 243. The fast exponentiation method efficiently calculates this by squaring and multiplying selectively.
🧠 Conceptual
intermediateNumber of Multiplications in Fast Exponentiation
For calculating base^exp using fast exponentiation, approximately how many multiplications are performed when exp = 1024?
Attempts:
2 left
💡 Hint
Think about how many times you can divide 1024 by 2 until you reach 1.
✗ Incorrect
Fast exponentiation uses repeated squaring, reducing multiplications to roughly log2(exp). Since 2^10 = 1024, about 10 multiplications occur.
🔧 Debug
advancedIdentify the Runtime Error in Fast Exponentiation Code
What error will the following Go code produce when calculating 2^10?
DSA Go
package main import "fmt" func fastPow(base, exp int) int { result := 1 b := base e := exp for e > 0 { if e%2 == 1 { result *= b } b *= b e /= 2 } return result } func main() { fmt.Println(fastPow(2, 10)) }
Attempts:
2 left
💡 Hint
Check the condition inside the if statement carefully.
✗ Incorrect
The code uses '=' instead of '==' in the if condition, causing a syntax error because '=' is assignment, not comparison.
❓ Predict Output
advancedOutput of Fast Exponentiation with Negative Exponent
What will be the output of this Go code that attempts to compute 2^-3 using fast exponentiation?
DSA Go
package main import "fmt" func fastPow(base, exp int) int { result := 1 b := base e := exp for e > 0 { if e&1 == 1 { result *= b } b *= b e >>= 1 } return result } func main() { fmt.Println(fastPow(2, -3)) }
Attempts:
2 left
💡 Hint
Consider how the loop condition handles negative exponents.
✗ Incorrect
Since exp is negative, e > 0 is false initially, so the loop never runs and result remains 1.
🚀 Application
expertFast Exponentiation Modulo Large Prime
Which option correctly computes (5^117) mod 1000000007 using fast exponentiation in Go?
Attempts:
2 left
💡 Hint
Remember to initialize result as 1 and multiply only when the current bit is 1.
✗ Incorrect
Option A correctly initializes result to 1, uses bitwise AND to check odd exponent bits, and applies modulo after each multiplication and squaring.
