简体   繁体   English

为什么即使在较大范围内,此Python代码也不能为我提供解决方案?

[英]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: 一些错误的地方:

  1. The answer is greater than your upper limit of 10000000 答案大于您的上限10000000
  2. Your should use xrange in Python 2, otherwise you'll have memory errors if you increase the upper limit 您应该在Python 2中使用xrange ,否则,如果增加上限,将会出现内存错误
  3. If you want all of the numbers from 1 to 20, you should use range(1, 21) 如果要使用从1到20的所有数字,则应使用range(1, 21)
  4. You should not manually increase loop counters, range or xrange do it for you 您不应该手动为您增加循环计数器, rangexrange

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: breakif found==True: break必须更改if i==10000000 and found==False: break if found==True: breakif 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.

相关问题 为什么python不给我关闭openCV窗口的选项? - why doesn't python give me the option to close openCV window? 为什么我的程序不给出 output 甚至错误 python - Why doesn't my program give output or even error in python 我的二次方程求解器程序(Python 3)没有给我正确的方程解 - My quadratic equation solver program (Python 3) doesn't give me right solution to the equation 为什么以下代码不会给出“IndexError”? - Why following code doesn't give “IndexError”? pynput 代码没有给我一个简单的字符串作为键 - pynput code doesn't give me a simple string as a key 为什么numpy.where不能给我正确答案? - Why doesn't numpy.where give me the correct answer? 为什么不首先和最后一组给我第一个也是最后一个 - Why doesn't first and last in a groupby give me first and last 为什么str(反向(...))不能给我反转的字符串? - Why str(reversed(…)) doesn't give me the reversed string? 为什么我的 IF 语句和 OR 运算符没有给我预期的输出? - Why my IF statement and OR operator doesn't give me the expected output? 为什么 range 只打印出 python 中的(开始,结束)索引项(对我来说),而 quit() 对我也不起作用 - Why range only prints out (start, end) indexed item in python (for me), and quit() doesn't work for me either
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM