简体   繁体   English

我的代码在哪里破[Python入门]

[英]Where is my code breaking [Beginner Python]

I'm trying to return a list of all the primes up to a certain number (this if for Project Euler problem 7). 我正在尝试返回所有素数到一定数量的列表(如果是针对Euler问题7的话)。 I'm extremely new to Python, but my issue here doesn't seem to be the language, but a logic error. 我是Python的新手,但我的问题似乎不是语言,而是逻辑错误。

import math
import sys

def is_prime(n):
    if n%2 == 0:
        return False
    for i in range(3, int(math.sqrt(n))+1, 2):
        if n%i == 0:
            return False
    return True

def prime_counter(n):
    result = [2]
    while len(result) < n:
        for i in range(3, sys.maxsize, 2):
            if is_prime(i):
                result.append(i)
                break
    return result

print (prime_counter(6))

This prints [2, 3, 3, 3, 3, 3]. 打印[2、3、3、3、3、3]。 Why is not incrementing i and evaluating new numbers to the list? 为什么不增加i并评估列表中的新数字? Why does it keep adding 3? 为什么它总是加3?

Your break statement exits your for loop in the function prime_counter every time it finds the first prime (3). 每次找到第一个质数(3)时, break语句都会在函数prime_counter退出for循环。 Then it restarts the for loop from the beginning, starting from the number 3, which it again discovers is a prime. 然后,它从头开始重新启动for循环,从数字3开始,它再次发现是质数。

You need to, for example, use a single while loop and increment the candidate variable yourself, and then break the loop when you have found enough primes. 例如,您需要使用一个while循环并自己增加候选变量,然后在找到足够的素数时中断循环。

Get rid of the while loop and, instead, test the length of result which each newly discovered prime: 摆脱while循环,而是测试每个新发现的素数的result长度:

def prime_counter(n):
    result = [2]
    for i in range(3, sys.maxsize, 2):
        if is_prime(i):
            result.append(i)
            if len(result) == n: return result

Your break statement appears to be the issue. 您的中断声明似乎是问题所在。 In the first cycle of the for loop, you set i = 3, confirm that i is less than sys.maxsize, and then check if i is prime. 在for循环的第一个周期中,设置i = 3,确认i小于sys.maxsize,然后检查i是否为素数。 It is, so we do the body of the if statement. 是的,所以我们做if语句的主体。 It appends i to your result set, and then breaks out of the for loop. 它将i附加到您的结果集中,然后脱离for循环。 Notice we never got back to the top of the for loop, where i would be incremented by 2. We left i at 3. After breaking out of the for loop, we check the condition of the while statement, and confirm that the length of the result set is still less than n, so we start the for loop all over again, starting i at 3, checking that 3 is less than sys.maxsize, etc. You're never getting around to incrementing i. 注意,我们永远不会回到for循环的顶部,在这里我将i递增2。我们将i保留在3。在退出for循环之后,我们检查while语句的条件,并确认结果集仍然小于n,因此我们重新开始for循环,从3开始i,检查3是否小于sys.maxsize,依此类推。您永远不会绕过递增i。

Instead of the double loop, you should instead just use the for loop, and after each successful appending, then check to see if the new length of the result set is equal to n, and if so, break out of the loop. 代替双循环,您应该只使用for循环,并在每次成功追加后,检查结果集的新长度是否等于n,如果是,则退出循环。 Get rid of the while loop entirely. 完全摆脱while循环。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM