Sub topic 9
From GCD to Least Common Multiple
This chapter assumes you can compute gcd(a, b) with Euclid's algorithm and that you know how it treats zero and negative inputs. You should also be comfortable with prime factorization and Python's // operator.
What the LCM Is
The least common multiple of two integers a and b, written lcm(a, b), is the smallest positive integer that both a and b divide. For example, the multiples of 4 are 4, 8, 12, 16, and so on, and the multiples of 6 are 6, 12, 18, and so on. The first value on both lists is 12, so lcm(4, 6) = 12.
Listing multiples is too slow for large numbers. Factoring both numbers also works, but factoring is expensive. The GCD gives a fast route that needs no factoring.
The Identity
For any integers a and b that are not both zero:
lcm(a, b) * gcd(a, b) = |a * b|
Prime exponents show why this holds. Write each number as a product of primes. For each prime p, let a contain p to the power e and let b contain it to the power f. The GCD takes the smaller exponent, min(e, f). The LCM takes the larger one, max(e, f). Because min(e, f) + max(e, f) = e + f for every prime, the product gcd * lcm has the same prime exponents as a * b.
| Prime | Exponent in 12 | Exponent in 18 | In gcd = 6 (min) | In lcm = 36 (max) |
|---|---|---|---|---|
| 2 | 2 | 1 | 1 | 2 |
| 3 | 1 | 2 | 1 | 2 |
Check: 6 * 36 = 216 = 12 * 18.
The proof uses factorization, but the computation does not. You get the GCD from Euclid's algorithm in a logarithmic number of steps, and then one division and one multiplication give the LCM.
Implementation
from functools import reduce
def gcd(a, b):
a, b = abs(a), abs(b)
while b != 0:
a, b = b, a % b
return a
def lcm(a, b):
if a == 0 or b == 0:
return 0
return abs(a // gcd(a, b) * b)
def lcm_many(numbers):
return reduce(lcm, numbers, 1)
print(lcm(4, 6)) # 12
print(lcm(-4, 6)) # 12
print(lcm(0, 5)) # 0
print(lcm_many([4, 6, 10])) # 60lcm_many works because the LCM is associative: lcm(a, b, c) = lcm(lcm(a, b), c). The starting value 1 is the identity, since lcm(1, x) = |x|.
Common Errors
Multiplying before dividing
Writing a * b // gcd(a, b) gives the right answer, but the intermediate value a * b can be far larger than the result. In Python, integers never overflow, so the only cost is speed on very large numbers. In C, Java, or Rust with fixed-width integers, a * b can overflow even when the final LCM fits. Dividing first, a // g * b, keeps the intermediate value no larger than the result. The division is exact because g divides a.
Forgetting the zero case
If both inputs are zero, gcd(0, 0) returns 0, and a // 0 raises ZeroDivisionError. By convention, lcm(a, 0) = 0: zero is the only common multiple of a and 0. The early return handles this case before any division happens.
Returning a negative result
-4 // 2 * 6 is -12. The LCM is defined as non-negative, so take the absolute value of the final product. Taking abs of only one input is not enough, because the sign of either input can make the product negative.
A Useful Consequence
When gcd(a, b) = 1, the identity reduces to lcm(a, b) = |a * b|. Two gears with 8 and 15 teeth return to their starting alignment after lcm(8, 15) = 120 teeth have passed, because 8 and 15 share no factor. Gears with 8 and 12 teeth realign after only lcm(8, 12) = 24, because their GCD of 4 cancels from the product.
Exercise
Compute lcm(21, 6) by first finding the GCD with Euclid's algorithm, then applying the divide-first formula. Show each step.
Show answer
Euclid: (21, 6) -> (6, 3) -> (3, 0), so gcd = 3. Then 21 // 3 = 7, and 7 6 = 42. So lcm(21, 6) = 42. Check: 42 3 = 126 = 21 * 6.
Exercise
Prove that lcm(a, b) = |a * b| exactly when gcd(a, b) = 1, for nonzero a and b.
Show answer
From lcm(a, b) gcd(a, b) = |a b|, with all quantities positive, lcm(a, b) = |a b| holds if and only if gcd(a, b) = 1. If the GCD is larger than 1, the LCM is strictly smaller than |a b|.
Exercise
A programmer writes lcm(a, b) = a * b // gcd(a, b) in Java using 32-bit int, and calls it with a = 100000 and b = 50000. What goes wrong, and how do you fix it?
Show answer
The product 100000 50000 = 5,000,000,000 is larger than the 32-bit int maximum of about 2.147 billion, so it overflows before the division happens. The correct LCM is 100000, which fits easily. To fix it, divide first: a / gcd(a, b) b = 100000 / 50000 * 50000 = 100000.