Least common multiple for 3 or more numbers
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The least common multiple of several numbers is the smallest positive integer divisible by all of them. For three or more numbers, you do not need a brand-new formula. The standard approach is to compute the LCM pairwise, folding the list from left to right.
Build Multi-Number LCM from the Two-Number Case
For two integers a and b, the usual formula is:
lcm(a, b) = abs(a * b) // gcd(a, b)
That extends naturally:
lcm(a, b, c) = lcm(lcm(a, b), c)
and the same idea keeps working for any number of inputs.
This works because divisibility is associative in the way LCM combines. Once you know the least common multiple of the first two numbers, you can combine that result with the next number, and so on.
Prime Factorization Explains Why It Works
Prime factorization is still useful conceptually. If:
- '
12 = 2^2 * 3' - '
15 = 3 * 5' - '
20 = 2^2 * 5'
then the LCM must include the highest power of every prime appearing in any input:
- '
2^2' - '
3' - '
5'
So the LCM is 2^2 * 3 * 5 = 60.
That is why the pairwise method works. The gcd removes shared factors so you do not multiply common divisors twice.
Handle Zero and Negative Numbers Correctly
In code, the annoying part is not the math. It is edge cases.
If any input is zero, the LCM is conventionally taken as zero because zero is divisible by every integer in the arithmetic sense used by the formula above. Negative signs do not matter, so it is standard to use absolute values.
That is why the implementation uses abs(a * b) and checks for zero explicitly.
Use Reduction for Cleaner Code
If you like functional style, a reduction expresses the same idea neatly.
This is still the same algorithm. It just packages the repeated pairwise combination more compactly.
When Prime Factorization Is Better
For hand calculations, prime factorization is often easier to reason about because you can see exactly which prime powers must appear in the answer. For code, the gcd-based method is usually simpler and faster to implement because an efficient gcd routine is already available in standard libraries.
So a practical rule is:
- use prime factorization for explanation or paper math
- use pairwise
lcmwithgcdin programs
Real Uses of Multi-Number LCM
This comes up whenever several cycles need to align:
- periodic jobs that repeat on different intervals
- gear or rotation problems
- finding a common denominator across many fractions
- synchronization of repeating events in simulations
If one machine repeats every 12 seconds, another every 15 seconds, and another every 20 seconds, the first time they all line up again is after 60 seconds.
Common Pitfalls
- Trying to list multiples manually once the numbers get large.
- Forgetting that
lcm(a, b, c)can be computed as nested two-number LCM operations. - Ignoring zero and negative inputs in code.
- Multiplying all numbers together and assuming that is the LCM.
- Confusing greatest common divisor with least common multiple when applying the formula.
Summary
- For three or more numbers, compute LCM by repeatedly applying the two-number LCM formula.
- The standard formula is
abs(a * b) // gcd(a, b)for non-zeroaandb. - Prime factorization explains the result but is not the only way to compute it.
- Handle zero and negative values deliberately in code.
- In practice, a fold or reduction over the input list is the cleanest implementation.

