Definition
Smallest multiple
LCM is the first shared multiple of a and b.
The least common multiple (LCM) is the smallest positive integer divisible by both inputs. This tutorial covers the gcd-lcm identity, Euclid’s helper, a brute scan, a live preview, worked Python examples, edge cases, and complexity.
Smallest multiple
LCM is the first shared multiple of a and b.
gcd × lcm
gcd(a,b) * lcm(a,b) = a * b (nonnegative).
Then formula
Compute g, then abs(a // g * b).
lcm = 36
gcd=6, so 12//6 * 18 = 36.
Try pairs
See gcd and lcm for any safe pair.
Via Euclid
GCD dominates; formula is O(1) after that.
The least common multiple of two positive integers is the smallest positive integer that is divisible by both. Example: for 12 and 18, the first shared multiple is 36.
Interviews almost always expect the gcd-based formula first. A brute multiple-scan is fine for teaching intuition, but it can be slow when the LCM is large.
LCM appears in scheduling, fraction arithmetic, and any problem where you need a shared period or common cycle length.
Compute LCM from Euclid’s gcd.
Use a // g * b, not a * b // g first.
lcm(a, 0) = 0 in most APIs.
Take absolute values so LCM stays ≥ 0.
In short: find gcd with Euclid, return 0 if either input is 0, otherwise return abs(a // g * b).
Given integers a and b, compute their least common multiple (nonnegative).
# 12, 18 → gcd=6 → 12//6 * 18 = 36
# 4, 6 → gcd=2 → 4//2 * 6 = 12
# 0, 9 → lcm = 0 | Item | Type | Description |
|---|---|---|
a, b | int | Integers (absolute values used for gcd). |
| Return / print | int / text | Nonnegative LCM (0 if either input is 0). |
function gcd(a, b):
while b != 0:
(a, b) = (b, a mod b)
return a
function lcm(a, b):
if a == 0 or b == 0:
return 0
g = gcd(abs(a), abs(b))
return abs((a / g) * b) | Method | Idea | Notes |
|---|---|---|
| gcd + formula | abs(a // g * b) | Interview default — fast |
| Brute scan | Walk multiples of max(a,b) | Clear intuition; can be slow |
| Prime factors | Max exponent per prime | Good math story; more code |
| Goal | Pattern |
|---|---|
| Euclid step | a, b = b, a % b |
| Safe LCM | abs(a // g * b) |
| Zero case | if a == 0 or b == 0: return 0 |
| Identity check | gcd * lcm == abs(a * b) |
| Classic pair | (12, 18) → 36 |
| Three numbers | lcm(lcm(a, b), c) |
Three ways to get LCM — pick by speed and clarity.
a // g * bO(log) via Euclid — interview default
m += max(a,b)Easy to explain; slow when LCM is huge
max exponentsMatches textbook definition
gcd firstMention brute only as intuition
Reach for LCM when you need a shared multiple or period.
Pair with GCD to show the identity.
Find when two repeating events next coincide.
Common denominators reuse LCM thinking.
Natural next number-theory warm-up in this chain.
Reduce a list with pairwise LCM.
Key benefit: one short function that locks in Euclid, modular arithmetic, and a classic identity.
Nonnegative integers only, within JavaScript safe range.
Three complete Python programs — gcd formula for 12 and 18, brute multiple-scan, and fold for three numbers. Click View Output to reveal sample console results.
Euclid gcd plus the safe LCM formula.
12, 18)Uses Euclid gcd, then computes LCM by dividing before multiplying.
def find_gcd(num1: int, num2: int) -> int:
num1, num2 = abs(num1), abs(num2)
while num2 != 0:
num1, num2 = num2, num1 % num2
return num1
def find_lcm(num1: int, num2: int) -> int:
if num1 == 0 or num2 == 0:
return 0
g = find_gcd(num1, num2)
return abs((num1 // g) * num2)
number1 = 12
number2 = 18
lcm = find_lcm(number1, number2)
print(f"LCM of {number1} and {number2} is: {lcm}") Euclid finds gcd(12, 18) = 6. Then 12 // 6 * 18 = 36. Dividing first keeps the intermediate product smaller.
Walk multiples until both divide evenly.
Start at max(a, b) and step by that value until both divide.
def lcm_scan_positive(a: int, b: int) -> int:
if a <= 0 or b <= 0:
return 0
step = max(a, b)
m = step
while m % a != 0 or m % b != 0:
m += step
return m
number1 = 12
number2 = 18
print(f"LCM of {number1} and {number2} is: {lcm_scan_positive(number1, number2)}") Start at 18, then 36. 36 is the first multiple divisible by both 12 and 18. Prefer the gcd formula in real interviews.
Fold pairwise LCM across a list.
Reuse the same helper: lcm(lcm(a, b), c).
def find_gcd(a: int, b: int) -> int:
a, b = abs(a), abs(b)
while b != 0:
a, b = b, a % b
return a
def find_lcm(a: int, b: int) -> int:
if a == 0 or b == 0:
return 0
g = find_gcd(a, b)
return abs((a // g) * b)
def lcm_many(*nums: int) -> int:
result = 1
for n in nums:
result = find_lcm(result, n)
return result
print(lcm_many(4, 6, 8))
print(lcm_many(12, 18, 9)) Pairwise folding works because LCM is associative on nonnegative integers (with the zero convention). Start from 1 so the first value becomes the running LCM.
If a or b is 0, return 0 immediately.
Run Euclid on absolute values.
Return abs(a // g * b).
Smallest shared multiple (or 0).
(12, 18)Trace Euclid for gcd, then the safe LCM formula.
| Step | a | b | Action |
|---|---|---|---|
| 1 | 12 | 18 | 12, 18 = 18, 12 % 18 → (18, 12) |
| 2 | 18 | 12 | 18, 12 = 12, 18 % 12 → (12, 6) |
| 3 | 12 | 6 | 12, 6 = 6, 12 % 6 → (6, 0) |
| 4 | 6 | 0 | gcd = 6; LCM = 12 // 6 * 18 = 36 |
Check: 6 * 36 = 216 = 12 * 18.
Where LCM shows up beyond the interview prompt.
Pair with GCD and the product identity.
Example: write find_lcm(a, b).
Find the next time two cycles meet.
Example: buses every 12 and 18 minutes.
Same idea as aligning fractions.
Example: 1/4 + 1/6 needs denom 12.
Reduce many numbers with pairwise LCM.
Example: Example 3 above.
LCM is a natural follow-up after GCD.
Example: reuse the same helper.
Verify gcd * lcm equals |a * b|.
Example: 6 * 36 = 216.
Pro Tip: say the identity out loud, then code Euclid + a // g * b.
Why the gcd-based approach earns interview points.
Euclid is logarithmic; formula is constant after that.
One equation connects GCD and LCM.
Same gcd function powers many problems.
Divide by g before multiplying b.
Pro Tip: lead with the formula; mention the brute scan only if asked how you would discover LCM without gcd.
Small habits that keep LCM solutions interview-ready.
A pure Euclid helper makes LCM almost trivial.
Return 0 when either argument is 0.
Prefer a // g * b over multiplying first.
Expect 36 — fast sanity check.
Use abs so the returned LCM is nonnegative.
Pro Tip: verify with gcd * lcm == abs(a * b) on a few pairs before moving on.
Mistakes that commonly break LCM solutions.
Skipping the zero guard before the formula.
→ Return 0 when a or b is 0.
a * b // g can overflow in fixed-width languages.
→ Prefer a // g * b (still exact when g divides a).
Large LCMs make repeated addition slow.
→ Use the gcd formula for production and interviews.
Negative inputs can yield a negative product.
→ Wrap the result (and gcd inputs) with abs.
Returning the gcd by mistake after Euclid.
→ Apply the formula after you have g.
Handle zero and sign consistently. Brute scans can be slow for large numbers.
a == 0 or b == 0Return 0 by standard programming convention.
Repeated addition can take many steps if LCM is large.
Fold with lcm(lcm(a,b), c).
Use absolute values so the result stays nonnegative.
LCM becomes |a * b| (e.g. 17 and 13 → 221).
LCM equals |a| (and gcd equals |a|).
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
gcd(a,b) * lcm(a,b) = a * b (nonnegative).abs(a // g * b).Quick Takeaway: find gcd, then LCM is abs(a // g * b) — or 0 if either input is 0.
| Method | Time | Extra space |
|---|---|---|
| gcd + formula | O(log min(a,b)) | O(1) |
| Brute scan | O(lcm / max(a,b)) worst case | O(1) |
| Fold k numbers | O(k log M) class | O(1) |
The gcd-based method is preferred in real programs and interviews.
LCM is the smallest shared multiple of two integers. Compute it with Euclid’s gcd and abs(a // g * b), handle zeros, and keep the result nonnegative.
Practice the three examples above, then continue to leap year for a calendar-rules warm-up.
Remember: gcd × lcm = |a × b|, and lcm(a, 0) = 0.
a // g * b after finding gCompute LCM the interview-friendly way.
Least multiple
Definitiongcd × lcm
MathThen formula
Codelcm(a,0)=0
EdgeO(log min)
AnalysisFor nonnegative integers a and b, gcd(a,b) * lcm(a,b) = a * b, with lcm(a,0)=0.
Learn how to check leap years with the standard 4 / 100 / 400 rules.
8 people found this page helpful