Python Least Common Multiple Algorithm

Document 对象参考手册Python3 Examples

The following code is used to implement the least common multiple algorithm:

Example (Python 3.0+)

# Filename : test.py # author by : www.example.com # Define function def lcm(x, y): # Get the largest 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:")) print( num1,"and", num2,"The least common multiple is", lcm(num1, num2))

Executing the above code produces the following output:

输入第一个数字: 54
输入第二个数字: 24
54 和 24 的最小公倍数为 216

Document 对象参考手册Python3 Examples

Other Extensions