Python 最小公倍数算法
以下代码用于实现最小公倍数算法:
实例(Python 3.0+)
执行以上代码输出结果为:
输入第一个数字: 54 输入第二个数字: 24 54 和 24 的最小公倍数为 216
以下代码用于实现最小公倍数算法:
执行以上代码输出结果为:
输入第一个数字: 54 输入第二个数字: 24 54 和 24 的最小公倍数为 216
HaydnLiao
Hay***[email protected]
按以下思路可减少循环次数:
1.当最大值为最小公倍数时,返回最大值;
2.当最大值不为最小公倍数时,最小公倍数为最大值的倍数。
实例:
# 最小公倍数
def lcm(a, b):
if b > a:
a, b = b, a # a为最大值
if a % b == 0:
return a # 判断a是否为最小公倍数
mul = 2 # 最小公倍数为最大值的倍数
while a*mul % b != 0:
mul += 1
return a*mul
while(True):
a = int(input("Input 'a':"))
b = int(input("Input 'b':"))
print(lcm(a, b))HaydnLiao
Hay***[email protected]
Joshua
cos***[email protected]
参考方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- def lcm(x, y): # very fast s = x*y while y: x, y = y, x%y return s//x print(lcm(120, 123)) #result: 4920
Joshua
cos***[email protected]
旅行的意义
101***[email protected]
利用最大公约数,最小公倍数等于两个数的乘积除以最大公约数:
def my_lcm():
n1 = int(input('请输入一个正整数:'))
n2 = int(input('请输入另一个正整数:'))
temp1 = n1
temp2 = n2
if n1 == 0:
n1, n2 = n2, n1
while n2 != 0:
n1, n2 = n2, n1 % n2
return int(temp1 * temp2 / n1)
print(my_lcm())旅行的意义
101***[email protected]
黄桃果捞
zhe***[email protected]
可以借用前一个实例中求取最小公约数的代码来实现求解两个整数的最大公倍数,代码如下:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# find the Least Common Multiple of two integer
def find_GCD(x, y):
find_GCD = y
while (x % y) != 0:
find_GCD = x % y
x, y = y, find_GCD
return find_GCD
def find_LCM(x,y):
return int(x * y / find_GCD(x, y))
int1 = int(input('Input 1st integer: '))
int2 = int(input('Input 2nd integer: '))
print('The least common multiple of {} and {} is {}'.format(int1, int2, find_LCM(int1, int2)))
代码运行结果如下:
Input 1st integer: 54 Input 2nd integer: 24 The least common multiple of 54 and 24 is 216
黄桃果捞
zhe***[email protected]
星空中的鱼
357***[email protected]
你这个实例的效率太慢了,这样写效率高些。
def lcm(X,Y): count=1; if X > Y: sample=X else: sample=Y while True: lcm=sample*count if (lcm % X ==0)and( lcm % Y ==0): return lcm else: count+=1
星空中的鱼
357***[email protected]
gj
123***[email protected]
使用 for 的方法:
def lcm(x,y):
if x>y:
greater = x
else:
greater = y
for i in range(greater,x*y+1,1):
if(i%x==0) and (i%y==0):
lcm = i
break
return lcm
num1 = int(input('输入第一个数字:'))
num2 = int(input('输入第二个数字:'))
print(num1,'和',num2,'的最小公倍数为',lcm(num1,num2))gj
123***[email protected]
桃之夭夭
956***[email protected]
求两个数之间的最小公倍数相对简单,用两个数的乘积对两个之间的最大公约数求商即可:
def gcd(a,b):
while (a!=0) & (b!=0):
a,b = b,a%b
return a
c = int(input('请输入第一个正整数: '))
d = int(input('请输入第一个正整数: '))
print('{}和{}的最小公倍数为: {}'.format(c,d,(c*d)//gcd(c,d)))
桃之夭夭
956***[email protected]
Alex
934***[email protected]
使用 math 模块的 gcd():
import math a = 54 b = 24 print(a*b/math.gcd(a,b))
输出结果为:
216.0
Alex
934***[email protected]
慕容君少
shu***[email protected]
参考方法:
# 继续用我最爱的 filter 刷起来
while True:
try:
n1=int(input('请输入第一个正整数: '))
n2=int(input('请输入第一个正整数: '))
L1=list(filter(lambda x:x%n1==0,range(n1,n1*n2+1)))
L2=list(filter(lambda x:x%n2==0,range(n2,n1*n2+1)))
L=list(filter(lambda x:x in L2,L1))
print('{0}最小公倍数为{1}'.format((n1,n2),min(L)))
break
except ValueError:
print('请输入正整数')慕容君少
shu***[email protected]
面向天依编程
145***[email protected]
乘法解决:两个数分别取自己的乘积,直到相等。
lis = [int(input('输入数字1:')), int(input('输入数字2:'))]
a = b = 1
c, d = 0, 1
while True:
if c < d:
a += 1
c = min(lis) * a
elif c > d:
b += 1
d = max(lis) * b
elif c == d:
print('最小公倍数: %d'%c)
break
除法解决:较大数取乘积,直到可被较小数整除时(反之亦可)
lis = [int(input('输入数字1:')), int(input('输入数字2:'))]
a = b = 1
while True:
if a % min(lis) == 0:
print('最小公倍数: %d' % a)
break
b += 1
a = max(lis) * b面向天依编程
145***[email protected]
不是TheShy是Rookie
501***[email protected]
可以直接用math.lcm()方法:
import math
def T1(a,b):
print(f"{a}与{b}的最小公倍数为:")
a,b =max(a,b),min(a,b)
c = 1
while True:
if (a*c) % b == 0:
print(a*c)
break
c += 1
def T2(a,b):
c = math.lcm(a,b)
print(f"{a}与{b}的最小公倍数为:{c}")
if __name__ == "__main__":
T1(100,42)
T2(100,42)
输出结果为:
100与42的最小公倍数为: 2100 100与42的最小公倍数为:2100
不是TheShy是Rookie
501***[email protected]