Python math.lcm() Function

Python math 模块Python math Module


In number theory,the least common multiple(Least Common Multiple, abbreviated as LCM) is a very important concept. It is used to find the smallest among the common multiples of two or more integers.

math.lcm()is a function introduced in Python 3.9, specifically used to calculate theLeast Common Multiple (LCM)。

Term Explanation: lcmis the abbreviation of "Least Common Multiple", meaning "least common multiple".


Basic Syntax and Parameters

Syntax Format

import math

math.lcm(*integers)

Parameter Description

  • Parameter: *integers- Variable number of integers (at least one)
  • Description: The integers for which to calculate the least common multiple.

Return Value

  • Returns the least common multiple of all input integers
  • If only one integer is passed, returns the absolute value of that integer
  • If 0 is passed, returns 0

Examples

Example 1: Basic Usage - Calculating LCM of Two Numbers

Example

import math

print("LCM of 4 and 6:", math.lcm(4, 6))     # 12
print("LCM of 5 and 7:", math.lcm(5, 7))     # 35
print("LCM of 12 and 18:", math.lcm(12, 18)) # 36

# Verify the formula
a, b = 4, 6
print(f"\n"Verify: LCM({a}, {b}) = {a} * {b} / GCD({a}, {b}) = {a*b//math.gcd(a,b)}")

Run result:

4 和 6 的 LCM: 12
5 和 7 的 LCM: 35
12 和 18 的 LCM: 36

验证: LCM(4, 6) = 4 * 6 / GCD(4, 6) = 12

Example 2: Calculating LCM of Multiple Numbers

Example

import math

print("LCM of 3, 4, 5:", math.lcm(3, 4, 5))    # 60
print("LCM of 6, 8, 12:", math.lcm(6, 8, 12))  # 24
print("LCM of 2, 3, 4, 5:", math.lcm(2, 3, 4, 5)) # 60

Run result:

3, 4, 5 的 LCM: 60
6, 8, 12 的 LCM: 24
2, 3, 4, 5 的 LCM: 60

Example 3: Handling Special Values

Example

import math

print("Single integer 7:", math.lcm(7))
print("LCM of 0 and 5:", math.lcm(0, 5))
print("LCM of -4 and 6:", math.lcm(-4, 6))

Run result:

单个整数 7: 7
0 和 5 的 LCM: 0
-4 和 6 的 LCM: 12

Example 4: Practical Application - Fraction Reduction to a Common Denominator

Example

import math

# Fraction addition: 1/4 + 1/6
denom1, denom2 = 4, 6
common_denom = math.lcm(denom1, denom2)
numer1 = 1 * (common_denom // denom1)
numer2 = 1 * (common_denom // denom2)
result = numer1 + numer2
print(f"1/{denom1} + 1/{denom2} = {numer1}/{common_denom} + {numer2}/{common_denom} = {result}/{common_denom}")

Run result:

1/4 + 1/6 = 3/12 + 2/12 = 5/12

Example 5: Practical Application - Cycle Synchronization

Example

import math

# The periods of three traffic lights are 30, 45, and 60 seconds
light_cycles = [30, 45, 60]
sync_interval = math.lcm(*light_cycles)
print(f"Traffic light periods: {light_cycles}")
print(f"Time interval for turning green simultaneously: {sync_interval} seconds")

Run result:

信号灯周期: [30, 45, 60]
同时变绿的时间间隔: 180 秒

Relationship with GCD

Formula: LCM(a, b) = a × b / GCD(a, b)

Example

import math

for a, b in [(4, 6), (8, 12), (15, 20)]:
    lcm_val = math.lcm(a, b)
    gcd_val = math.gcd(a, b)
    print(f"LCM({a}, {b}) = {lcm_val}, GCD({a}, {b}) = {gcd_val}, Verify: {lcm_val * gcd_val} == {a*b} -> {lcm_val*gcd_val == a*b}")

Run result:

LCM(4, 6) = 12, GCD(4, 6) = 2, 验证: 24 == 24 -> True
LCM(8, 12) = 24, GCD(8, 12) = 4, 验证: 96 == 96 -> True
LCM(15, 20) = 60, GCD(15, 20) = 5, 验证: 300 == 300 -> True

Notes

  • math.lcm()Only available in Python 3.9+
  • Negative numbers are calculated using their absolute values
  • The LCM of 0 and any number is 0

Python math 模块Python math Module

Other Extensions