#头条创作挑战赛#
从Python3.9版本,Math模块新增加了计算最小公倍数的函数lcm函数。本文简单介绍此函数的应用。

封面
2个整数的最小公倍数
在3.9版本中,通过lcm函数可以非常简单地快速得到2个整数的最小公倍数,如下图例1.

例1
而在3.8版本以及之前版本中,需要使用gcd函数,如下图例2.

例2
在例2,定义了一个函数,函数返回值为变量X,Y的最小公倍数。将6,4代入自定义函数后,得到最小公倍数为12。需要注意的是,如果自定义函数中,使用"/"则会产生浮点小数(flot),所以使用"//"(整除)运算符。
3个以上整数的最小公倍数
在3.9版本中,lcm函数同样支持3个以上整数的计算。如下图例3。

例3
在3.8版本,需要用到gcd函数,并自定义函数。如下例4。

例4
List对象要素的最小公倍数
在对list对象中的要素求取最小公倍数时,要在参数前加"*"。如下图例5。

例5