Euclidean Algorithm (GCD)
Efficient algorithm for computing greatest common divisor. One of oldest algorithms, described by Euclid around 300 BC. Basis for many number theory algorithms.
Visualization
Interactive visualization for Euclidean Algorithm (GCD)
Interactive visualization with step-by-step execution
Implementation
1function gcd(a: number, b: number): number {
2 while (b !== 0) {
3 [a, b] = [b, a % b];
4 }
5 return Math.abs(a);
6}
7
8// Recursive
9function gcdRecursive(a: number, b: number): number {
10 return b === 0 ? Math.abs(a) : gcdRecursive(b, a % b);
11}
12
13// Extended Euclidean: returns [gcd, x, y] where ax + by = gcd
14function extendedGCD(a: number, b: number): [number, number, number] {
15 if (b === 0) return [a, 1, 0];
16
17 const [gcd, x1, y1] = extendedGCD(b, a % b);
18 const x = y1;
19 const y = x1 - Math.floor(a / b) * y1;
20
21 return [gcd, x, y];
22}
23
24// LCM using GCD
25function lcm(a: number, b: number): number {
26 return Math.abs(a * b) / gcd(a, b);
27}Deep Dive
Theoretical Foundation
Based on principle: gcd(a,b) = gcd(b, a mod b). Repeatedly replace larger number with remainder until one becomes 0. The other number is GCD. Extended Euclidean finds coefficients x,y where ax + by = gcd(a,b).
Complexity
Time
O(1)
O(log min(a,b))
O(log min(a,b))
Space
O(1) iterative, O(log n) recursive
Applications
Industry Use
Fraction simplification in calculators
RSA cryptography (key generation and operations)
Modular arithmetic in computer algebra
Solving linear Diophantine equations
Computer graphics (Bresenham's line algorithm)
Music theory (rhythm and harmony analysis)
Calendar calculations and date arithmetic
Error correction codes in telecommunications
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¹.