Python math.lcm() Function
In number theory,the least common multiple(Least Common Multiple, LCM for short) is a very important concept. It is used to find the smallest of the common multiples of two or more integers.
math.lcm() is a function introduced in Python 3.9, designed specifically to compute theleast common multiple (LCM)。
Word meaning: lcm is an abbreviation of "Least Common Multiple", meaning "least common multiple".
Basic syntax and parameters
Syntax format
import math
math.lcm(*integers)
Parameter Description
- Parameters:
*integers- A variable number of integers (at least one) - Description: the integers whose least common multiple is to be computed.
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
Example
Example 1: basic usage - computing the LCM of two numbers
Example
import
math
(
"LCM of 4 and 6:"
,
math
.
lcm
(
4
,
6
)
)
12
(
"LCM of 5 and 7:"
,
math
.
lcm
(
5
,
7
)
)
35
(
"LCM of 12 and 18:"
,
math
.
lcm
(
12
,
18
)
)
36
Verify the formula
a
,
b
=
4
,
6
(
f
"Verification: LCM({a}, {b}) = {a} * {b} / GCD({a}, {b}) = {a*b//math.gcd(a,b)}"
)
Result:
4 和 6 的 LCM: 12
5 和 7 的 LCM: 35
12 和 18 的 LCM: 36
验证: LCM(4, 6) = 4 * 6 / GCD(4, 6) = 12
Example 2: computing the LCM of multiple numbers
Example
import
math
(
"LCM of 3, 4, 5:"
,
math
.
lcm
(
3
,
4
,
5
)
)
60
(
"LCM of 6, 8, 12:"
,
math
.
lcm
(
6
,
8
,
12
)
)
24
(
"LCM of 2, 3, 4, 5:"
,
math
.
lcm
(
2
,
3
,
4
,
5
)
)
60
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
(
"A single integer 7:"
,
math
.
lcm
(
7
)
)
(
"LCM of 0 and 5:"
,
math
.
lcm
(
0
,
5
)
)
(
"LCM of -4 and 6:"
,
math
.
lcm
(
4
,
6
)
)
Result:
单个整数 7: 7
0 和 5 的 LCM: 0
-4 和 6 的 LCM: 12
Example 4: practical application - finding a common denominator for fractions
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
(
f
"1/{denom1} + 1/{denom2} = {numer1}/{common_denom} + {numer2}/{common_denom} = {result}/{common_denom}"
)
Result:
1/4 + 1/6 = 3/12 + 2/12 = 5/12
Example 5: practical application - period synchronization
Example
import
math
Three traffic lights have cycles of 30, 45 and 60 seconds
light_cycles
=
[
30
,
45
,
60
]
sync_interval
=
math
.
lcm
(
*light_cycles
)
(
f
"Light cycles: {light_cycles}"
)
(
f
"Interval until they all turn green together: {sync_interval} seconds"
)
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
)
(
f
"LCM({a}, {b}) = {lcm_val}, GCD({a}, {b}) = {gcd_val}, verification: {lcm_val * gcd_val} == {ab} -> {lcm_valgcd_val == a*b}"
)
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()is only available in Python 3.9+- Negative numbers are computed using their absolute values
- The LCM of 0 and any number is 0
Python math Module