Number theory & mathAnimated
LCM
lcm(a, b) = a / gcd(a, b) · b.
Time
O(log min(a, b))
Extra space
O(1)
Use it for
Synchronising cycles.
How it works
- The LCM is the smallest number that both inputs divide.
- Use the link with the GCD: lcm(a, b) = a / gcd(a, b) × b.
- Find the GCD with Euclid’s algorithm, then apply the formula (divide first to avoid overflow).
Watch it run
Loading animation…