Python求最小公倍数

最小公倍数:两个或多个整数公有的倍数叫做它们的公倍数,其中除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 = 1while(1):j = x0*iif j % x1==0 and j % x2 ==0 and j % 3 ==0:breaki+=1print(x1,x2,x3,"这三个数的最小公倍数是:",j)def max(x,y,z):if x>y and x>z:return xelif y>x and y>z:return yelse:return z

算法二

# 算法2def least_commo_multiple2():t=1print("请输入3个数")x1 = int(input("请输入x1:"))x = x1x2 = int(input("请输入x2:"))y = x2x3 = int(input("请输入x3:"))z = x3x0 = max(x1,x2,x3)for i in range(2,x0+1):flag = 1while flag:flag = 0if x1 % i == 0:x1 = x1 / iflag = 1if x2 % i == 0:x2 = x2 / iflag = 1if x3 % i == 0:x3 = x3 / iflag = 1if flag == 1:t = t * ix0 = 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 % bwhile c != 0:a = bb = cc = a % breturn 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 = ab1 = bc = a%bwhile c != 0:a = bb = cc = a%breturn a1*b1/b

主函数

# 主函数if __name__ == "__main__":# least_commo_multiple1()# least_commo_multiple2()# least_commo_multiple3()least_commo_multiple4()

效果截图:

以上就是Python语言求解三个数的最小公倍数啦~