我正在解决欧拉计划第5题,使用以下代码:
def findLCM(k):
start=time.time()
primes=[2,3,5,7,11,13,17,19,23]
factors=[]
for factor in range(2,k):
if factor in primes:
factors.append(factor)
else:
factorization=[]
while factor!=1:
for prime in primes:
lastFactor=prime
if factor%prime==0:
factor/=prime
factorization.append(lastFactor)
break
tmpFactors=[]
for tmpFactor in factorization:
if tmpFactor not in factors:
factors.append(tmpFactor)
else:
tmpFactors.append(tmpFactor)
factors.remove(tmpFactor)
for tmpFactor in tmpFactors:
factors.append(tmpFactor)
print factors
product=1
for factor in factors:
product*=factor
factors.sort()
end=time.time()
fnTime=end-start
return product, fnTime, factors
有没有一种Python函数可以像这个函数一样将因子和分解组合起来?例如,如果factors=[2, 3, 5]和factorization=[2, 2, 3],则组合后的列表应该是[2, 2, 3, 5]。