简体   繁体   English

Python素数列表生成器错误

[英]Python Prime number list generator errors

This code begins at 5, and lists the following prime numbers up to your choice, which in this case, is the following 17 prime numbers. 此代码从5开始,并列出以下素数供您选择,在本例中为以下17个素数。 When I run it, 25 and 49 are printed. 当我运行它时,将打印25和49。 Why are they not filtered out? 为什么不将它们过滤掉?

start = 5
number = 1
divisor = 3
upper = start - 2
doc = open("hey.txt", "w")
while number <= 17:
    if start % divisor == 0:
        start = start + 2
        divisor = 3
    elif divisor == upper:
        doc.write(str(start))
        doc.write(", ")
        number = number + 1
        start = start + 2
        divisor = 3
    else:
        divisor = divisor + 2


hey.txt: 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53, 

you need to update your upper variable, i will explain: 您需要更新您的upper变量,我将解释:

when you write your start number to the file , it means that you found this number to be a prime number, hence you need to update the upper variable to be the new start -2 value , since you increased start . 当您将start编号写入文件时,这意味着您发现该编号为质数,因此,由于增加了start ,因此需要将upper变量更新为新的start -2值。 so your function should look like: 因此您的函数应如下所示:

start = 5
number = 1
divisor = 3
upper = start - 2
doc = open("hey.txt", "w")
while number <= 17:
    if start % divisor == 0:
        start = start + 2
        divisor = 3
    elif divisor == upper:
        doc.write(str(start))
        doc.write(", ")
        number = number + 1
        start = start + 2
        divisor = 3
        upper = start - 2 # this is the line you forgot.
    else:
        divisor = divisor + 2

Probably because you don't increase upper. 可能是因为您没有增加上限。 So the maximal divisor you test is 3. 因此,您测试的最大除数为3。

You got allready an answer on you question. 您已经准备好回答您的问题。 But take a look at this question. 但是看看这个问题。 This is a way faster approach to do primes Fast primes with python : 这是一种执行素数的更快方法, 使用python快速素数

def primes2(n):
    """ Input n>=6, Returns a list of primes, 2 <= p < n """
    n, correction = n-n%6+6, 2-(n%6>1)
    sieve = [True] * (n//3)
    for i in range(1,int(n**0.5)//3+1):
      if sieve[i]:
        k=3*i+1|1
        sieve[      k*k//3      ::2*k] = [False] * ((n//6-k*k//6-1)//k+1)
        sieve[k*(k-2*(i&1)+4)//3::2*k] = [False] * ((n//6-k*(k-2*(i&1)+4)//6-1)//k+1)
    return [2,3] + [3*i+1|1 for i in range(1,n//3-correction) if sieve[i]]

True, its not really readable. 是的,它不是很可读。 But first olways look if other people did already what you a trying to do. 但是首先,奥尔威特先看看别人是否已经做了您想做的事情。 Python is a language that has a huge community and its benefits are a ton of libraries and a ton of developed algorithms/programs that do probably what you want ot do in a cleaner and faster way than you will do it by yourself. Python是一种具有庞大社区的语言,它的好处是拥有大量的库和大量的开发的算法/程序,它们可能比您自己更干净,更快速地完成您想做的事情。 So enjoy the benefit of the community.) 因此,请享受社区的利益。)

Cleaner and shorter: 更干净,更短:

lower = 5
upper = 50

print("Prime numbers between {} and {} are: ".format(lower, upper))

doc = open("hey.txt", "w")
for num in range(lower,upper + 1):
   # prime numbers are greater than 1
   if num > 1:
       for i in range(2,num):
           if (num % i) == 0:
               break
       else:
           print(num)
           doc.write(str(num))
           doc.write(",")

OUTPUT: OUTPUT:

Prime numbers between 5 and 50 are: 
5,7,11,13,17,19,23,29,31,37,41,43,47,

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

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