python中的素数生成器:数字的累加

python中的素数生成器:数字的累加,python,python-2.7,primes,Python,Python 2.7,Primes,我的发电机工作得很好:我已经测试过很多次了。只是,有一个问题:正如人们可能相信的那样,随着数字的增加,程序变得越来越慢。我已经想出了一种方法来实现这一点,但不知道如何实现,因为不久前我才开始使用python 我的生成器如下所示: while 0==0: i=input('Enter the number for which all previous shall be tested for primality: ') n=0 a=[0,1,2

我的发电机工作得很好:我已经测试过很多次了。只是,有一个问题:正如人们可能相信的那样,随着数字的增加,程序变得越来越慢。我已经想出了一种方法来实现这一点,但不知道如何实现,因为不久前我才开始使用python

我的生成器如下所示:

    while 0==0:
        i=input('Enter the number for which all previous shall be tested for primality: ')
        n=0
        a=[0,1,2]
        while n<=i:
            n=n+1
            for b in range(2,n):
                if n%b==0:
                    break
                if b==(n-1):
                    a.append(n)
                    print a`
当0==0时:
i=输入('输入所有先前测试的素数:')
n=0
a=[0,1,2]

当n时,这应该工作得更快:

while 1:
    i=input('Enter the number for which all previous shall be tested for primality: ')
    n=5
    a=[2,3]
    while n<=i:
        n=n+1
        isPrime = True
        for b in a:
            if n%b==0:
                isPrime = False
                break
        if isPrime:
            a.append(n)
            print a
而1:
i=输入('输入所有先前测试的素数:')
n=5
a=[2,3]
而n有,其中筛最快。如果你熟悉,你可以包装。下面是的python实现,如果您还有其他问题,请告诉我:

from __future__ import generators
def eratosthenes():
    '''Yields the sequence of prime numbers via the Sieve of Eratosthenes.'''
    D = {}  # map composite integers to primes witnessing their compositeness
    q = 2   # first integer to test for primality
    while 1:
        if q not in D:
            yield q        # not marked composite, must be prime
            D[q*q] = [q]   # first multiple of q not already marked
        else:
            for p in D[q]: # move each witness to its next multiple
                D.setdefault(p+q,[]).append(p)
            del D[q]       # no longer need D[q], free memory
        q += 1

对不起,我不是想把2.3版python。。。这是2.7为您编辑的问题。您只需要测试高达sqrt(n)的因子,因为较大的因子将有一个小于sqrt(n)的辅因子。使用素数的乐趣:相关:
n%1==0
始终是正确的,对于任何数字
n
。我尝试了这个方法,它比我的速度快一些,因此这是一个改进。我正在做正确的工作,不想让它快一点。使用试除法枚举素数不是
O(n*log(n))
更糟。将其与埃拉托斯坦筛的时间复杂度进行比较
O(n*log(log(n)))
(更好)。埃拉托斯坦筛就是我试图制作的。
while 1:
    i=input('Enter the number for which all previous shall be tested for primality: ')
    n=5
    a=[2,3]
    while n<=i:
        n=n+1
        isPrime = True
        for b in a:
            if n%b==0:
                isPrime = False
                break
        if isPrime:
            a.append(n)
            print a
from __future__ import generators
def eratosthenes():
    '''Yields the sequence of prime numbers via the Sieve of Eratosthenes.'''
    D = {}  # map composite integers to primes witnessing their compositeness
    q = 2   # first integer to test for primality
    while 1:
        if q not in D:
            yield q        # not marked composite, must be prime
            D[q*q] = [q]   # first multiple of q not already marked
        else:
            for p in D[q]: # move each witness to its next multiple
                D.setdefault(p+q,[]).append(p)
            del D[q]       # no longer need D[q], free memory
        q += 1