小学的时候咱们就学过如何求两个数的最小公倍数了,如求8,12的最小公倍数,用质数反复除的方法, python
2| 8 24 app
2| 4 12 spa
2| 2 6 code
1 3 rem
最后的结果就是2*2*2*3=24,可是如何求3个或3个数以上的最小公倍数呢?其实原理和求两个数的最小公倍数是同样的,下面是用Python实现的求多个数的最小公倍数的程序段,附上了比较详细的注释代码,求各位的指教: get
import math """ 得到多个数的最小公倍数 isPrime(n) getMutiPrime(n) getLeastCommonMutible(numList) """ def isPrime(n): """ 判断一个数是不是质数 """ if n==2: return True if n==1 or n%2==0: return False p = int(math.sqrt(n))+1 i = 3 while i<p: if n%i==0: return False i+=2 return True def getMutiPrime(n): """get muti num of n n=num1*num2...*numx for num1...numx are all prime numbers 将数n进行质因数分解 prime factorization """ if n==2: resultlist = [2] else: list1 = [i for i in range(2,int(math.sqrt(n))+1) if isPrime(i)] #print(list1) resultlist=[] tmp = n while not isPrime(tmp) and tmp!=1: for i in list1: if tmp%i==0: resultlist.append(i) tmp=tmp//i #print(tmp) #print(resultlist) if tmp!=1: resultlist.append(tmp) return resultlist def isMutillistFill(mutilLsit): """ 判断一个二维列表中的每个列表是否都为空 mutilLsit=[[],[],[]] 返回False mutilLsit=[[1],[],[]] 返回True """ for row in mutilLsit: if len(row)>0: return True return False def getLeastCommonMutible(numList): """ numList 待求数的列表, 返回它们的最小公倍数的质因数列表 """ primelists=[] set1=set({}) mutilResult = [] for i in numList: primelists.append(getMutiPrime(i)) set1.update(set(i1 for i1 in getMutiPrime(i))) #print(primelists) flag = True flag1 = False while flag: for i in set1: for row in primelists: if i in row: row.remove(i) flag1 = True if flag1: mutilResult.append(i) flag1 = False flag = isMutillistFill(primelists) #print(mutilResult) return mutilResult def getMutil(numList): result = 1 for i in numList: result*=i return result if __name__=='__main__': print(getMutil(getLeastCommonMutible([8,12,20])))#获取8,12,20的最小公倍数 print(getMutil(getLeastCommonMutible([i for i in range(2,21)])))#获取1到20的全部数的最小公倍数