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
Current lesson

Fast Exponentiation Lab

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
Open deeper structure lesson
1.0x