Matrix Exponentiation
Compute M^n in O(log n) using binary exponentiation. Used for linear recurrences like Fibonacci.
Visualization
Interactive visualization for Matrix Exponentiation
Interactive visualization with step-by-step execution
Implementation
1function matrixMultiply(A: number[][], B: number[][]): number[][] {
2 const n = A.length;
3 const C = Array(n).fill(0).map(() => Array(n).fill(0));
4
5 for (let i = 0; i < n; i++) {
6 for (let j = 0; j < n; j++) {
7 for (let k = 0; k < n; k++) {
8 C[i][j] += A[i][k] * B[k][j];
9 }
10 }
11 }
12 return C;
13}
14
15function matrixPower(M: number[][], n: number): number[][] {
16 const size = M.length;
17 let result = Array(size).fill(0).map((_, i) =>
18 Array(size).fill(0).map((_, j) => i === j ? 1 : 0)
19 );
20 let base = M.map(row => [...row]);
21
22 while (n > 0) {
23 if (n & 1) result = matrixMultiply(result, base);
24 base = matrixMultiply(base, base);
25 n >>= 1;
26 }
27 return result;
28}
29
30function fibonacciMatrix(n: number): number {
31 if (n <= 1) return n;
32 const M = [[1, 1], [1, 0]];
33 const result = matrixPower(M, n - 1);
34 return result[0][0];
35}Deep Dive
Theoretical Foundation
Apply binary exponentiation to matrices. Fibonacci: [F(n+1), F(n)] = [[1,1],[1,0]]^n × [1,0]. Generalizes to any linear recurrence.
Complexity
Time
O(d³ log n)
O(d³ log n)
O(d³ log n)
Space
O(d²)
Applications
Industry Use
Fast Fibonacci computation in competitive programming
Linear recurrence solving in mathematics
Dynamic programming optimization
Graph path counting (adjacency matrix powers)
Markov chain state transitions
Population dynamics modeling
Financial modeling (compound growth)
Signal processing (filter design)
Quantum computing (unitary evolution)
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¹.