Python-100-Days/Day01-15/code/Day06/function2.py

25 lines
382 B
Python
Raw Normal View History

2018-03-06 00:03:44 +08:00
"""
函数的定义和使用 - 求最大公约数和最小公倍数
Version: 0.1
Author: 骆昊
Date: 2018-03-05
"""
def gcd(x, y):
2019-05-03 21:17:36 +08:00
if x > y:
(x, y) = (y, x)
for factor in range(x, 1, -1):
if x % factor == 0 and y % factor == 0:
return factor
return 1
2018-03-06 00:03:44 +08:00
def lcm(x, y):
2019-05-03 21:17:36 +08:00
return x * y // gcd(x, y)
2018-03-06 00:03:44 +08:00
print(gcd(15, 27))
print(lcm(15, 27))