Fast Exponentiation (Binary Exponentiation / Exponentiation by Squaring)
Compute x^n in O(log n) time using divide-and-conquer strategy, vastly superior to the naive O(n) approach of multiplying x by itself n times. Also known as exponentiation by squaring, this algorithm is fundamental in competitive programming, cryptography, and numerical computing. It exploits the binary representation of the exponent to reduce the number of multiplications from n to log₂(n), enabling computation of massive powers like 2^1000 efficiently.
Visualization
Interactive visualization for Fast Exponentiation (Binary Exponentiation / Exponentiation by Squaring)
Interactive visualization with step-by-step execution
Implementation
1function fastPow(x: number, n: number): number {
2 if (n === 0) return 1;
3 if (n < 0) {
4 x = 1 / x;
5 n = -n;
6 }
7
8 let result = 1;
9 while (n > 0) {
10 if (n % 2 === 1) {
11 result *= x;
12 }
13 x *= x;
14 n = Math.floor(n / 2);
15 }
16
17 return result;
18}Deep Dive
Theoretical Foundation
Fast exponentiation is based on the recursive relation: x^n = (x^(n/2))² if n is even, and x^n = x × (x^((n-1)/2))² if n is odd. By expressing the exponent in binary, we only need to multiply by x when the corresponding bit is 1. For example, x^13 = x^(1101₂) = x^8 × x^4 × x^1. We build these powers by repeatedly squaring: x → x² → x⁴ → x⁸ → x^16..., only using the powers corresponding to set bits in n's binary representation.
Complexity
Time
O(1)
O(log n)
O(log n)
Space
O(1)
Applications
Industry Use
Matrix exponentiation (Fibonacci in O(log n))
Computing large Fibonacci numbers
Solving linear recurrences
Computer graphics transformations
Scientific computing (polynomial evaluation)
Game theory (Nim game variants)
Dynamic programming optimization
Competitive programming problems
Number theory computations
Signal processing (FFT preprocessing)
Use Cases
Related Algorithms
GCD (Euclidean Algorithm)
Compute the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Dating back to around 300 BC and appearing in Euclid's Elements, it's one of the oldest algorithms still in common use. The algorithm is based on the principle that GCD(a,b) = GCD(b, a mod b) and is remarkably efficient with O(log min(a,b)) time complexity. The GCD is the largest positive integer that divides both numbers without remainder.
LCM (Least Common Multiple)
Calculate the Least Common Multiple (LCM) of two integers - the smallest positive integer that is divisible by both numbers. The LCM is intimately related to the GCD through the formula: LCM(a,b) = |a×b| / GCD(a,b). This relationship allows us to compute LCM efficiently using the Euclidean algorithm for GCD, achieving O(log min(a,b)) time complexity instead of naive factorization methods.
Sieve of Eratosthenes
Ancient and highly efficient algorithm to find all prime numbers up to a given limit n. Invented by Greek mathematician Eratosthenes of Cyrene (276-194 BC), this sieve method systematically eliminates multiples of primes, leaving only primes in the array. With O(n log log n) time complexity, it remains one of the most practical algorithms for generating large lists of primes, vastly superior to trial division which runs in O(n² / log n) time.
Prime Factorization
Decompose a positive integer into its unique prime factor representation. Every integer greater than 1 can be expressed as a product of prime numbers in exactly one way (Fundamental Theorem of Arithmetic). This algorithm uses trial division optimized to check only up to √n, as any composite number must have a prime factor ≤ √n. Returns a map of prime factors to their powers, e.g., 360 = 2³ × 3² × 5¹.