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.
Visualization
Interactive visualization for LCM (Least Common Multiple)
Interactive visualization with step-by-step execution
Implementation
1function lcm(a: number, b: number): number {
2 return Math.abs(a * b) / gcd(a, b);
3}
4
5function gcd(a: number, b: number): number {
6 while (b !== 0) {
7 [a, b] = [b, a % b];
8 }
9 return a;
10}Deep Dive
Theoretical Foundation
The fundamental relationship between LCM and GCD states that for any two positive integers a and b: LCM(a,b) × GCD(a,b) = a × b. This comes from the fact that every common multiple must include all prime factors of both numbers at their highest powers, while the GCD contains only the common factors. By computing GCD first, we can efficiently find LCM without prime factorization.
Complexity
Time
O(1)
O(log min(a,b))
O(log min(a,b))
Space
O(1)
Applications
Industry Use
Scheduling problems (finding when events coincide)
Fraction addition (finding common denominator)
Music theory (finding beat patterns)
Gear synchronization in mechanical systems
Signal processing (sampling rate conversion)
Clock synchronization in distributed systems
Astronomy (planetary orbit calculations)
Project management (task scheduling)
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.
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¹.
Modular Exponentiation (Fast Power with Modulo)
Efficiently compute (base^exponent) mod m without overflow, crucial for cryptographic operations. Computing large powers like 2^100 mod 1000000007 is impossible with naive exponentiation due to integer overflow, but modular exponentiation achieves this in O(log exponent) time using the binary representation of the exponent. This algorithm is the foundation of RSA encryption, Diffie-Hellman key exchange, and many cryptographic protocols.