python实现求最大公约数与最小公倍数

您所在的位置:网站首页 用python求两个整数的最大公约数 python实现求最大公约数与最小公倍数

python实现求最大公约数与最小公倍数

2023-05-22 00:12| 来源: 网络整理| 查看: 265

 

  记录python实现最大公约数&最小公位数两种算法

 

概念

最大公约数:指两个或多个整数共有约数中最大的一个

最小公倍数:两个或多个整数公有的倍数叫做它们的公倍数,其中除0以外最小的一个公倍数就叫做这几个整数的最小公倍数

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

 

实例

辗转相除法

 

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

 

 

更相减损法

a=int(input('please enter 1st num:')) b=int(input('please enter 2nd num:')) s=a*b while a!=b: if a>b: a-=b elif a


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3