© 本文档使用 php.cn手册 发布
以下代码用于实现最小公倍数算法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Filename :test.py
# author by : www.shouce.ren
# 定义函数
def lcm(x, y):
# 获取最大的数
if x > y:
if
x > y:
greater = x
else:
else
:
greater = y
while(True):
while
(True):
if((greater % x == 0) and (greater % y == 0)):
((greater % x == 0)
and
(greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
return
lcm
# 获取用户输入
num1 = int(input("输入第一个数字: "))
num1 = int(input(
"输入第一个数字: "
))
num2 = int(input("输入第二个数字: "))
num2 = int(input(
"输入第二个数字: "
print( num1,"和", num2,"的最小公倍数为", lcm(num1, num2))
print
( num1,
"和"
, num2,
"的最小公倍数为"
, lcm(num1, num2))
执行以上代码输出结果为:
输入第一个数字: 54
输入第二个数字: 24
54 和 24 的最小公倍数为 216