Python math.lcm() 函数
在数论中,最小公倍数(Least Common Multiple,简称 LCM)是一个非常重要的概念。它用于找到两个或多个整数的共同倍数中最小的那一个。
math.lcm() 是 Python 3.9 引入的函数,专门用于计算多个整数的最小公倍数(LCM)。
单词释义: lcm 是 "Least Common Multiple" 的缩写,意为"最小公倍数"。
基本语法与参数
语法格式
import math math.lcm(*integers)
参数说明
- 参数:
*integers- 可变数量的整数(至少一个) - 描述: 要计算最小公倍数的整数。
返回值
- 返回所有输入整数的最小公倍数
- 如果只传入一个整数,返回该整数的绝对值
- 如果传入 0,则返回 0
实例
示例 1:基础用法 - 计算两个数的 LCM
实例
import math
print("4 和 6 的 LCM:", math.lcm(4, 6)) # 12
print("5 和 7 的 LCM:", math.lcm(5, 7)) # 35
print("12 和 18 的 LCM:", math.lcm(12, 18)) # 36
# 验证公式
a, b = 4, 6
print(f"\n验证: LCM({a}, {b}) = {a} * {b} / GCD({a}, {b}) = {a*b//math.gcd(a,b)}")
print("4 和 6 的 LCM:", math.lcm(4, 6)) # 12
print("5 和 7 的 LCM:", math.lcm(5, 7)) # 35
print("12 和 18 的 LCM:", math.lcm(12, 18)) # 36
# 验证公式
a, b = 4, 6
print(f"\n验证: LCM({a}, {b}) = {a} * {b} / GCD({a}, {b}) = {a*b//math.gcd(a,b)}")
运行结果:
4 和 6 的 LCM: 12 5 和 7 的 LCM: 35 12 和 18 的 LCM: 36 验证: LCM(4, 6) = 4 * 6 / GCD(4, 6) = 12
示例 2:计算多个数的 LCM
实例
import math
print("3, 4, 5 的 LCM:", math.lcm(3, 4, 5)) # 60
print("6, 8, 12 的 LCM:", math.lcm(6, 8, 12)) # 24
print("2, 3, 4, 5 的 LCM:", math.lcm(2, 3, 4, 5)) # 60
print("3, 4, 5 的 LCM:", math.lcm(3, 4, 5)) # 60
print("6, 8, 12 的 LCM:", math.lcm(6, 8, 12)) # 24
print("2, 3, 4, 5 的 LCM:", math.lcm(2, 3, 4, 5)) # 60
运行结果:
3, 4, 5 的 LCM: 60 6, 8, 12 的 LCM: 24 2, 3, 4, 5 的 LCM: 60
示例 3:处理特殊值
实例
import math
print("单个整数 7:", math.lcm(7))
print("0 和 5 的 LCM:", math.lcm(0, 5))
print("-4 和 6 的 LCM:", math.lcm(-4, 6))
print("单个整数 7:", math.lcm(7))
print("0 和 5 的 LCM:", math.lcm(0, 5))
print("-4 和 6 的 LCM:", math.lcm(-4, 6))
运行结果:
单个整数 7: 7 0 和 5 的 LCM: 0 -4 和 6 的 LCM: 12
示例 4:实际应用 - 分数通分
实例
import math
# 分数加法: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}")
# 分数加法: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}")
运行结果:
1/4 + 1/6 = 3/12 + 2/12 = 5/12
示例 5:实际应用 - 周期同步
实例
import math
# 三个交通信号灯周期分别是 30、45、60 秒
light_cycles = [30, 45, 60]
sync_interval = math.lcm(*light_cycles)
print(f"信号灯周期: {light_cycles}")
print(f"同时变绿的时间间隔: {sync_interval} 秒")
# 三个交通信号灯周期分别是 30、45、60 秒
light_cycles = [30, 45, 60]
sync_interval = math.lcm(*light_cycles)
print(f"信号灯周期: {light_cycles}")
print(f"同时变绿的时间间隔: {sync_interval} 秒")
运行结果:
信号灯周期: [30, 45, 60] 同时变绿的时间间隔: 180 秒
与 GCD 的关系
公式: LCM(a, b) = a × b / GCD(a, b)
实例
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}, 验证: {lcm_val * gcd_val} == {a*b} -> {lcm_val*gcd_val == a*b}")
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}, 验证: {lcm_val * gcd_val} == {a*b} -> {lcm_val*gcd_val == a*b}")
运行结果:
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
注意事项
math.lcm()是 Python 3.9+ 才有- 负数会取绝对值计算
- 0 与任何数的 LCM 都为 0
Python math 模块
点我分享笔记