Python math.lcm() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.9+

Definition

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)
CallWhat 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

ParameterDescription
*integersZero or more integers (int only). Passing floats raises TypeError.

Return Value

Return ValueDescription
intNon-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+).

python
import math

print("4m & 6m routes:", math.lcm(4, 6))
print("12m, 15m, 20m :", math.lcm(12, 15, 20))
print("7m & 11m      :", math.lcm(7, 11))

How It Works

  1. Shared factors collapse. Four and six share factor two, so they align every 12 minutes instead of 24.
  2. Three schedules at once. Twelve, fifteen, and twenty converge every 60 minutes.
  3. 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))

How It Works

  1. Runtime feature test. Calls native math.lcm() on Python 3.9+ and falls back smoothly on 3.8.
  2. Divide before multiplying. Computing abs(a) // gcd(a, b) * abs(b) keeps numbers smaller and handles negatives cleanly.
  3. 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")

How It Works

  1. Any zero yields zero. The least common multiple involving zero is defined as 0.
  2. Empty call returns 1. Just as empty product is 1, calling math.lcm() with zero arguments returns 1.
  3. 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).

Next: math.ldexp()

Return x * (2**i) as a float, exact inverse of frexp().

ldexp() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful