使用python投影Euler#5
问题陈述如下: 2520是最小的数字,可以被1到10之间的每一个数字除,没有任何余数 能被1到20的所有数整除的最小正数是多少 以下是我的解决方案:使用python投影Euler#5,python,python-2.7,Python,Python 2.7,问题陈述如下: 2520是最小的数字,可以被1到10之间的每一个数字除,没有任何余数 能被1到20的所有数整除的最小正数是多少 以下是我的解决方案: x=2520.0 list=[] true_list=[11.0, 12.0, 13.0, 14.0, 16.0, 17.0, 18.0, 19.0, 20.0] b=1 def test(): for n in true_list: z=x/n if z%2==0: list.app
x=2520.0
list=[]
true_list=[11.0, 12.0, 13.0, 14.0, 16.0, 17.0, 18.0, 19.0, 20.0]
b=1
def test():
for n in true_list:
z=x/n
if z%2==0:
list.append(n)
while b==1:
test()
if list==true_list:
b=2
print x
else:
x=x+20
list=[]
->基本上,我定义了一个空列表,该列表由函数test()填充。test()的作用是检查给定的数字(本例中为x)是否可以被11-20的值均匀整除。如果是,则将该值(介于11-20之间)置于空列表中
当test()运行它的过程时,程序检查列表是否等于预定义的true_列表,该列表包含11-20之间的所有数字。如果是,则打印x。否则,程序将在增加x的值后继续
这意味着,如果list等于true_list,那么11-20之间的所有数字都将平均除以我们的数字(x),这就是问题中的问题
运行一分钟左右后给出的答案是:465585120.0。这恰好是错误的。我不知道这是为什么。我已经试着解决这个问题8个多小时了,现在我已经束手无策了。错误是什么
您无需提前阅读,但如果您对我为什么在解决方案中使用某些东西有疑问,那么我将在这里介绍其中一些:
->我没有使用true_list中的所有20个数字来加速程序,因为任何可以被11-20整除的数字也可以被1-20整除
->我使用x=x+20来加速程序,因为它与x=x+1或x+2一样有效;只是速度更快
->我之所以使用浮点值,是因为我在函数“test()”中使用z=x/n,我不想切掉小数部分,因为这样做会使即使是浮点值也符合后续操作的条件,即z%2
例如:
1) 对于int值:
x=17
n=2
z=x/n=8
在这里,z%2==0是有效的,但不应该是这样,因为它在数学中实际上是无效的
2) 对于浮点值:
x=17.0
n=2.0
z=x/n=8.5
这里,
z%n!=0
应该是这样的。首先,正如我在评论中所说的,你为什么要用暴力的方式来做?您可以在几秒钟内更轻松地计算数字1到20的LCM
其次,你的代码行
if z%2==0:
list.append(n)
这基本上给了你两倍你想要的答案,因为这句话会让你计算LCM*2,因为它必须除以一个额外的因子2
正确答案是232792560,我在中使用一张纸和一个计算器计算,就像其他人提到的一样,只需找到lcm,但这里有一个简单的方法。只要记住lcm(a,b,c)=lcm(a,lcm(b,c))。这就是全部:
from fractions import gcd
print(reduce(lambda a, b: a * b / gcd(a, b), range(1, 21)))
如果您想编写自己的gcd函数,它的工作原理如下():
Tht
%
运算符称为“模数”运算符。在英语中:a%b
读作“a mod b”,意思是“a/b的剩余部分”。所以100%3=1
和12%5=2
检查可除数性的一种常用方法是检查“我的数字模我的除数等于0吗?”。或在代码中:
if a%b == 0:
print("b divides a!")
在代码中,您要检查n
是否除以x
。您检查:
z=x/n
if z%2==0:
print("n divides x?") #No not really
z
是x
和n
的商<如果z%2==0,则代码>可以解释为“如果z可被2整除”。
所以你会问,“x和n的商可以被2整除吗?”当然,这和你想要的并不接近。而是简单地做
if x%n==0:
print("n divides x?") # YES!
我建议您学习一些python教程,以便在尝试解决问题之前掌握基本知识。:)
如果您需要更多帮助,请告诉我。:) 有很多方法可以解决这个问题。由于您正在进行ProjectEuler的早期练习,我想您应该开发一种方法来帮助您理解基本的Python构造(与使用
gcd
等的“包含电池”的方法相反)
这里有一个基本的方法,在运行时或开发人员时间内效率不高:),但这是一个不错的练习:
found = None
n = 0
while not found:
n += 1
for d in xrange(2,21):
if n % d:
break
else:
found = n
print found
是这样的:
- 检查从0开始的分子
。(实际上,你可以从2520开始,因为我们知道答案必须不小于1-10的答案,但这是一个微小的优化。)n
- 永远循环,直到找到解决方案。(在现实世界的程序中,您可能会进行某种安全检查,这样它就不会永远运行,但对于我们正在做的事情来说,这很好。)
- 如果我们还没有找到解决方案,那就在下一轮中把分子增加1
- 将分子除以2-20范围内的分母
。如果d
的任何值产生非零余数,则中断循环-测试剩余分母没有意义。(如果我们想提高效率,我们可以使用d
,因为除以大于分子的值是没有意义的。如果效率是一个极端问题,比如如果范围大得多(2-1000而不是2-20),我们实际上可以使用xrange(2,n)
因为除数不可能没有大于平方根的余数)xrange(2,floor(sqrt(n))
- 如果我们一直通过
循环而不提前中断,for
子句运行,我们记录分子的当前值-这就是解决方案else
这种方法显然是蛮力。作为一种学习练习,它很好。对于同一问题的更大版本,您最好使用欧几里德算法和硬件感知优化。您可以分别找到素数和复合数的LCM,然后找到它们的LCM。在alm中完成工作等一下!这是我的代码:
import time
start_time = time.time()
for i in range(2520,10000000000):
if i % 11 == 0 and\
i % 12 == 0 and\
i % 13 == 0 and\
i % 14 == 0 and\
i % 15 == 0 and\
i % 16 == 0 and\
i % 17 == 0 and\
i % 18 == 0 and\
i % 19 == 0 and\
i % 20 == 0:
print(i)
break
print(time.time() - start_time," seconds")
import time
start_time = time.time()
nums = []
primeNums = []
allNums = []
def findFactors(num):
factors = []
for i in range(1, num + 1):
if num % i == 0:
if i not in factors:
factors.append(i)
x = int(num / i)
factors.append(x)
else:
break
return factors
def isDivisibleByAll(number, numbers):
isDivisbleBy = []
for num in numbers:
if number % num == 0:
isDivisbleBy.append(num)
return isDivisbleBy == numbers
for i in range(11, 21):
nums.append(i)
for num in nums:
if findFactors(num) == [1, num]:
primeNums.append(num)
nums.remove(num)
currentNum = nums[-1]
currentPrimeNum = primeNums[-1]
while isDivisibleByAll(currentNum, nums) == False:
currentNum = currentNum + nums[-1]
print(currentNum)
while isDivisibleByAll(currentPrimeNum, primeNums) == False:
currentPrimeNum = currentPrimeNum + primeNums[-1]
print(currentPrimeNum)
allNums.append(currentNum)
allNums.append(currentPrimeNum)
currentAllNum = allNums[-1]
while isDivisibleByAll(currentAllNum, nums) == False:
currentAllNum = currentAllNum + allNums[-1]
print(currentAllNum)
print(currentNum, currentPrimeNum, currentAllNum)
end_time = time.time()
print("Time taken: ", end_time - start_time)
通过计算素数因子来计算数字的最小公倍数不是更快吗?(2^4*3^2*5*7*11*13)
found = None
n = 0
while not found:
n += 1
for d in xrange(2,21):
if n % d:
break
else:
found = n
print found
import time
start_time = time.time()
for i in range(2520,10000000000):
if i % 11 == 0 and\
i % 12 == 0 and\
i % 13 == 0 and\
i % 14 == 0 and\
i % 15 == 0 and\
i % 16 == 0 and\
i % 17 == 0 and\
i % 18 == 0 and\
i % 19 == 0 and\
i % 20 == 0:
print(i)
break
print(time.time() - start_time," seconds")
import time
start_time = time.time()
nums = []
primeNums = []
allNums = []
def findFactors(num):
factors = []
for i in range(1, num + 1):
if num % i == 0:
if i not in factors:
factors.append(i)
x = int(num / i)
factors.append(x)
else:
break
return factors
def isDivisibleByAll(number, numbers):
isDivisbleBy = []
for num in numbers:
if number % num == 0:
isDivisbleBy.append(num)
return isDivisbleBy == numbers
for i in range(11, 21):
nums.append(i)
for num in nums:
if findFactors(num) == [1, num]:
primeNums.append(num)
nums.remove(num)
currentNum = nums[-1]
currentPrimeNum = primeNums[-1]
while isDivisibleByAll(currentNum, nums) == False:
currentNum = currentNum + nums[-1]
print(currentNum)
while isDivisibleByAll(currentPrimeNum, primeNums) == False:
currentPrimeNum = currentPrimeNum + primeNums[-1]
print(currentPrimeNum)
allNums.append(currentNum)
allNums.append(currentPrimeNum)
currentAllNum = allNums[-1]
while isDivisibleByAll(currentAllNum, nums) == False:
currentAllNum = currentAllNum + allNums[-1]
print(currentAllNum)
print(currentNum, currentPrimeNum, currentAllNum)
end_time = time.time()
print("Time taken: ", end_time - start_time)