Python Least Common Multiple Algorithm
The following code implements the least common multiple algorithm:
Example (Python 3.0+)
Filename : test.py
author by : www.runoob.com
Defining a function
def
lcm
(
x
,
y
)
:
Get the larger number
if
x
y
:
greater
=
x
else
:
greater
=
y
while
(
True
)
:
if
(
(
greater
%
x
==
0
)
and
(
greater
%
y
==
0
)
)
:
lcm
=
greater
break
greater
+=
1
return
lcm
Get user input
num1
=
int
(
input
(
"
Enter the first number:
"
)
)
num2
=
int
(
input
(
"
Enter the second number:
"
)
)
(
num1
,
"
and
"
,
num2
,
"
the least common multiple of
"
,
lcm
(
num1
,
num2
)
)
The output of the code above is:
输入第一个数字: 54
输入第二个数字: 24
54 和 24 的最小公倍数为 216
Python3 Examples