Foundations
Linear Structures
Hashing
Array and String Patterns
Recursion, Search, and Sorting
Problem Solving Patterns
Algorithm Searching
Sorting Algorithms
Array Algorithms
Grid Algorithms
Linear Structure Algorithms
Hashing Algorithms
Recursion and Backtracking
Greedy Algorithms
Tree and Heap Algorithms
Graph Algorithms
Dynamic Programming Algorithms
String Algorithms
Math Algorithms
Keyboard ready
Space plays. Arrow keys step. R restarts.
LAB Fast Exponentiation Lab
Math
Problem shape
Intermediate
4
8
15
16
23
42
Core idea
Square the base and consume exponent bits from right to left.
Step 1 / 5
Problem shape
Math Intermediate
Fast Exponentiation
Welcome to the Fast Exponentiation algorithm lab. Square the base and consume exponent bits from right to left. Use it when: Use for modular exponentiation and large powers. The time complexity is O(log exponent), and the space complexity is O(1). The pseudocode is: result = 1 while exp > 0 if exp is odd result *= base base *= base exp = exp / 2 As you practice, track the state that changes and the condition that makes the algorithm stop.
Time
O(log exponent)
Space
O(1)
Pseudocode
result = 1
while exp > 0
if exp is odd result *= base
base *= base
exp = exp / 2 1.0x