最小公倍数:两个或多个整数公有的倍数叫做它们的公倍数,其中除0以外最小的一个公倍数就叫做这几个整数的最小公倍数。整数a,b的最小公倍数记为[a,b],同样的,a,b,c的最小公倍数记为[a,b,c],多个整数的最小公倍数也有同样的记号。
利用Python求最小公倍数(4种方法)
算法一
# 算法1def least_commo_multiple1(): print("请输入3个数") x1 = int(input("请输入x1:")) x2 = int(input("请输入x2:")) x3 = int(input("请输入x3:")) x0 = max(x1,x2,x3) i = 1 while(1): j = x0*i if j % x1==0 and j % x2 ==0 and j % 3 ==0: break i+=1 print(x1,x2,x3,"这三个数的最小公倍数是:",j)def max(x,y,z): if x>y and x>z: return x elif y>x and y>z: return y else: return z
算法二
# 算法2def least_commo_multiple2(): t=1 print("请输入3个数") x1 = int(input("请输入x1:")) x = x1 x2 = int(input("请输入x2:")) y = x2 x3 = int(input("请输入x3:")) z = x3 x0 = max(x1,x2,x3) for i in range(2,x0+1): flag = 1 while flag: flag = 0 if x1 % i == 0: x1 = x1 / i flag = 1 if x2 % i == 0: x2 = x2 / i flag = 1 if x3 % i == 0: x3 = x3 / i flag = 1 if flag == 1: t = t * i x0 = max(x1,x2,x3) print(x, y, z, "这三个数的最小公倍数是:", t)
算法三
# 算法3def least_commo_multiple3(): print("请输入3个数") x1 = int(input("请输入x1:")) x2 = int(input("请输入x2:")) x3 = int(input("请输入x3:")) x0 = x1*x2/most_common_divisor(x1,x2) x0 = x0 * x3 / most_common_divisor(x0, x3) print(x1,x2,x3,"这三个数的最小公倍数是:",x0)def most_common_divisor(a, b): c = a % b while c != 0: a = b b = c c = a % b return b
算法四
# 算法4def least_commo_multiple4(): print("请输入3个数") x1 = int(input("请输入x1:")) x2 = int(input("请输入x2:")) x3 = int(input("请输入x3:")) x0 = ff(ff(x1,x2),x3) print(x1, x2, x3, "这三个数的最小公倍数是:", x0)def ff(a,b): a1 = a b1 = b c = a%b while c != 0: a = b b = c c = a%b return a1*b1/b
主函数
# 主函数if __name__ == "__main__": # least_commo_multiple1() # least_commo_multiple2() # least_commo_multiple3() least_commo_multiple4()
效果截图:
以上就是Python语言求解三个数的最小公倍数啦~🤗
来源地址:https://blog.csdn.net/weixin_51938823/article/details/131539389