[英]Why doesn't this Python code give me a solution even with a large range?
Below is the problem I'm trying to solve: 以下是我要解决的问题:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. 2520是可以除以1到10的每个数字而没有任何余数的最小数字。 What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
能被1到20的所有数均分的最小正数是多少?
It is the 5th problem from Project Euler ( http://projecteuler.net/problem=5 ). 这是来自Euler项目( http://projecteuler.net/problem=5 )的第五个问题。 I wrote a code in Python to find out the number, but I'm unable to obtain a solution.
我用Python编写了一个代码来找出数字,但无法获得解决方案。
My code is: 我的代码是:
def smallestNumber():
smallest=0 ## Initializing the smallest number
end=False ## Initializing the break condition for lower level For loop
found=False ## Initializing the break condition for upper level For loop
for i in range(21,10000000): ## Upper level for loop
if i==10000000 and found==False: break ## This will break upper loop when range is satisfied
for k in range(1,20): ## Lower level for loop
if end==True: break ## This will break lower loop when range is satisfied
if i%k==0: ## If condition to check whether current i value is divisible evenly by current k value
if k==20: ## If k==20, this will make smallest equal to current i value and make both break conditions True
smallest=i
end=True
found=True
k=k+1
else: ## if not divisible, this will increment upper level loop
break
i=i+1
if found==False: print 'No value exists in this range'
else: return smallest
(I'm new to stackoverflow and was unable to paste the actual code without messing up the formatting. I apologize for any inconvenience due to that). (我是stackoverflow的新手,无法粘贴实际代码而不弄乱格式。因此给您带来的任何不便,我深表歉意)。
I keep getting the output 'No value exists in this range' regardless of how big I make my range. 无论我将范围扩大多大,我都会不断得到输出“此范围内不存在任何值”。 I'm guessing that although my logic is alright, I have messed up the code somewhere since I'm a Python beginner.
我猜想虽然我的逻辑还不错,但是由于我是Python初学者,所以我在某些地方弄乱了代码。
It'd be great if someone can help me. 如果有人可以帮助我,那就太好了。
Thanks 谢谢
Some things that are wrong: 一些错误的地方:
xrange
in Python 2, otherwise you'll have memory errors if you increase the upper limit xrange
,否则,如果增加上限,将会出现内存错误 range(1, 21)
range(1, 21)
range
or xrange
do it for you range
或xrange
First of all, what you need is LCM of the numbers from 1 to 20. You can find better ways to find LCM than the one you've implemented in this code. 首先,您需要的是从1到20的数字的LCM。与在此代码中实现的LCM相比,您可以找到更好的查找LCM的方法。
Next, there are a few mistakes here. 接下来,这里有一些错误。 I've edited your code to illustrate.
我已经编辑了您的代码以进行说明。 Please see my code bellow, this shows the changes I had to make in order to make your code produce the correct result for finding the smallest number divisible by all numbers from 1 to 10:
请看下面的代码,它显示了我为使代码产生正确的结果而必须进行的更改,以便找到最小的数字,该最小的数字可被1到10的所有数字整除:
def smallestNumber():
smallest=0
end=False
found=False
for i in range(21,1000000):
if found==True: break # This will break upper loop when number is found
for k in range(1,11): # the range is up to 11 here because we need to check numbers 1-10
if end==True: break
if i%k==0:
if k==10: ##
smallest=i
end=True
found=True
#k=k+1 # not necessary
else:
break
#i=i+1 # not necessary
if found==False: print 'No value exists in this range'
else: return smallest
As you can see, I had to comment out lines like i=i+1
because that is already being taken care of by using for i in range(21,10000000)
. 如您所见,我不得不注释掉
i=i+1
因为使用for i in range(21,10000000)
已经可以解决for i in range(21,10000000)
。 You would also notice that I had to change if i==10000000 and found==False: break
to if found==True: break
because checking for i=10000000 is not necessary and you should actually stop searching when your found is true. 您还会注意到,
if i==10000000 and found==False: break
, if found==True: break
必须更改if i==10000000 and found==False: break
if found==True: break
, if found==True: break
if i==10000000 and found==False: break
if found==True: break
因为没有必要检查i = 10000000,并且在找到true时实际上应该停止搜索。 And that was the real bug in your code. 那就是代码中的真正错误。 Hope this helps.
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.