开发小学生

导航

辗转相除法计算最大公约数和最小公倍数

"""
欧几里得 = 辗转相处
最大公约数等于两数辗转相处之后的最终余数
比如10和25,25除以10商2余5,那么10和25的最大公约数,等同于10和5的最大公约数。

二者关系:两个数之积=最小公倍数*最大公约数
"""

a=int(input('please enter 1st num:'))

b=int(input('please enter 2nd num:'))
s=a*b
while a%b!=0:
    a,b=b,(a%b)  
else:
    print(b,'is the maximum common divisor')
    print(s//b,'is the least common multiple')
 
#运行结果
please enter 1st num:10
please enter 2nd num:15
5 is the Maximum common divisor
30 is the Least common multiple

posted on 2021-08-03 16:27  开发小学生  阅读(228)  评论(0编辑  收藏  举报