Python求最小公倍数4种方法总结
作者:旦旦崽
这篇文章主要给大家介绍了关于Python求最小公倍数4种方法的相关资料,最小公倍数不可以像最大公约数那样直接利用辗转相除法求出,但可以借助辗转相除法求得的最大公约数来求最小公倍数,需要的朋友可以参考下
最小公倍数:
两个或多个整数公有的倍数叫做它们的公倍数,其中除0以外最小的一个公倍数就叫做这几个整数的最小公倍数。整数a,b的最小公倍数记为[a,b],同样的,a,b,c的最小公倍数记为[a,b,c],多个整数的最小公倍数也有同样的记号。
利用Python求最小公倍数(4种方法)
算法一
# 算法1
def 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
算法二
# 算法2
def 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)
算法三
# 算法3
def 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
算法四
# 算法4
def 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语言求解三个数的最小公倍数啦~🤗
总结
到此这篇关于Python求最小公倍数的文章就介绍到这了,更多相关Python求最小公倍数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
