Python math.lcm() 関数
数論において、最小公倍数(Least Common Multiple、略称LCM)は非常に重要な概念です。これは、2つ以上の整数の公倍数の中で最小のものを見つけるために使用されます。
math.lcm()は Python 3.9 で導入された関数で、複数の整数の最小公倍数(LCM)。
単語の意味: lcmは「Least Common Multiple」の略で、「最小公倍数」という意味です。
基本構文とパラメータ
構文形式
import math math.lcm(*integers)
パラメータの説明
- パラメータ:
*integers- 可変数の整数(少なくとも1つ) - 説明:最小公倍数を計算する整数。
戻り値
- すべての入力整数の最小公倍数を返します。
- 整数を1つだけ渡した場合は、その整数の絶対値を返します。
- 0 を渡した場合は、0 を返します。
実例
例1:基本用法 - 2つの数の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
# 3つの交通信号機の周期はそれぞれ30、45、60秒
light_cycles = [30, 45, 60]
sync_interval = math.lcm(*light_cycles)
print(f"信号機の周期: {light_cycles}")
print(f"同時に青になる時間間隔: {sync_interval} 秒")
# 3つの交通信号機の周期はそれぞれ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 です。
その他の拡張