Python 最小公倍数算法

Python 3 教程 · 2019-02-12 13:43:21

以下代码用于实现最小公倍数算法:

实例(Python 3.0+)

# Filename : test.py # author by : www.codercto.com # 定义函数 def lcm(x, y): # 获取最大的数 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 # 获取用户输入 num1 = int(input("输入第一个数字: ")) num2 = int(input("输入第二个数字: ")) print( num1,"", num2,"的最小公倍数为", lcm(num1, num2))

执行以上代码输出结果为:

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

点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html

查看所有标签

Agile Web Development with Rails 4

Agile Web Development with Rails 4

Sam Ruby、Dave Thomas、David Heinemeier Hansson / Pragmatic Bookshelf / 2013-10-11 / USD 43.95

Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details. Tens of thousands of deve......一起来看看 《Agile Web Development with Rails 4》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具