最大公约数和最小公倍数

最大公约数
def hcf(x, y):
"""该函数返回两个数的最大公约数"""
if x > y:
smaller = y
else:
smaller = x

for i in range(1,smaller + 1):
if((x % i == 0) and (y % i == 0)):
hcf = i

return hcf

num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))
print( num1,"和", num2,"的最大公约数为", hcf(num1, num2))


最小公倍数

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))


posted @ 2021-06-04 16:14  flyacome  阅读(88)  评论(0)    收藏  举报