Skip to content
StackDecode
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

  1. The LCM is the smallest number that both inputs divide.
  2. Use the link with the GCD: lcm(a, b) = a / gcd(a, b) × b.
  3. Find the GCD with Euclid’s algorithm, then apply the formula (divide first to avoid overflow).

Watch it run

Loading animation…