math.lcm(*integers) returns the least common multiple of the given integers as a non-negative int.
Added in Python 3.9, it accepts zero, one, or multiple integers (math.lcm() == 1), and returns 0 if any argument is 0.
math.lcm() is a function in Python's standard math module: import it with import math and call math.lcm(...), or import the name directly with from math import lcm. Added in Python 3.9.
Syntax
Import math and pass integers (Python 3.9+).
python
import math
m = math.lcm(a, b)
Call
What it does
math.lcm(4, 6)
Returns 12.
math.lcm(12, 15, 20)
Returns 60.
math.lcm(0, 15)
Returns 0.
math.lcm(4.0, 6)
Raises TypeError: floats are not allowed.
Requires Python 3.9 or newer. Dividing by math.gcd() first (`abs(a) // math.gcd(a, b) * abs(b)`) avoids oversized intermediate products in older-version fallbacks.
Function signatures
python
math.lcm(*integers)
# added in Python 3.9; returns non-negative int (1 if empty)
Parameters
Parameter
Description
*integers
Zero or more integers (int only). Passing floats raises TypeError.
Return Value
Return Value
Description
int
Non-negative least common multiple; 0 if any input is 0; 1 if no inputs.
Python Examples
Example 1 — Find When Repeating Bus Routes Meet Again
Calculate alignment intervals for 2-route and 3-route transit cycles (Python 3.9+).
Shared factors collapse. Four and six share factor two, so they align every 12 minutes instead of 24.
Three schedules at once. Twelve, fifteen, and twenty converge every 60 minutes.
Coprime cycles multiply. Seven and eleven share no factors, giving product 77.
Example 2 — Compute LCM Safely on Python 3.8 and Newer
Use hasattr(math, "lcm") with a gcd()-based fallback across a list of denominators.
python
import functools
import math
def safe_lcm(*nums):
if hasattr(math, "lcm"):
return math.lcm(*nums)
return functools.reduce(
lambda a, b: (
0 if a == 0 or b == 0 else abs(a) // math.gcd(a, b) * abs(b)
),
nums,
1,
)
print(safe_lcm(3, 4, 6, 8))
print(safe_lcm(-4, 6))
Output
24
12
How It Works
Runtime feature test. Calls native math.lcm() on Python 3.9+ and falls back smoothly on 3.8.
Divide before multiplying. Computing abs(a) // gcd(a, b) * abs(b) keeps numbers smaller and handles negatives cleanly.
Common denominator found. Denominators 3, 4, 6, 8 share common multiple 24.
Example 3 — Check Zero Inputs, Empty Calls, and Float Rejection
Verify how zero collapses LCM to 0 and catch TypeError on floats.
python
import math
def fallback_lcm(a, b):
return 0 if a == 0 or b == 0 else abs(a * b) // math.gcd(a, b)
lcm_fn = getattr(math, "lcm", fallback_lcm)
print(lcm_fn(0, 15))
if hasattr(math, "lcm"):
print(math.lcm())
else:
print(1)
try:
math.gcd(4.0, 6)
except TypeError:
print("lcm and gcd reject floats with TypeError")
Output
0
1
lcm and gcd reject floats with TypeError
How It Works
Any zero yields zero. The least common multiple involving zero is defined as 0.
Empty call returns 1. Just as empty product is 1, calling math.lcm() with zero arguments returns 1.
Integers only. Passing a float like 4.0 raises TypeError.
Frequently Asked Questions
math.lcm() was added in Python 3.9.
It returns 0. For example math.lcm(0, 10) is 0.
1, the multiplicative identity (analogous to math.prod([]) == 1).
Yes. Like math.gcd(), signs are ignored so math.lcm(-4, 6) returns positive 12.
Divide by math.gcd(a, b) before multiplying: 0 if a == 0 or b == 0 else abs(a) // math.gcd(a, b) * abs(b).