1、函数实现最小公倍数:
方法使用穷举法,较简单
def gong_bei(a, b):
c = max(a, b)
while True:
if c % a == 0 and c % b == 0:
return c
c += 1
2、 函数实现最大公约数:
用Ture保持循环的发生,而后判断c的状况,为零则为a(较小的),否则为c
def gong_yue(a, b):
if a > b:
a, b = b, a # b大 6, 5 1
c = b % a
while True:
if c == 0:
return a
else:
a = b
b = c
return c
3、循环实现同时实现两个:
这里有数学定理:最大公约数 = 两者乘积 / 最小公倍数
if __name__ == '__main__':
x = int(input("输入第一个数:"))
y = int(input("输入第二个数:"))
m = max(x, y)
while True:
if m % x == 0 and m % y == 0:
print('最小公倍数为:', m)
break
m += 1
t = x * y / m
print('最大公因数为:', int(t))
print('最小公倍数为:', gong_bei(x, y))
print('最大公因数为:', gong_yue(x, y))