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

时间:2013-03-10 06:33:59

标签: 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]
        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`

我发现如果我将a = [0,1,2]移到前面的空格而0 == 0,它会累积程序运行时之前使用的所有数字。我想要改变的是,作为累积素数,它使用那些来赶上下一个未知数。例如,假设我想要所有素数达到100.然后,我希望所有素数高达200.而不是重新计算高达100的素数,我想编程跳过这些并继续100后的第一个素数。

任何建议都会非常感激,我使用的是2.7 Python。

a = [2,3,5,7,11]
while 1:
    b = input('Enter the number for which all previous shall be tested for primality: ')
    c = len(a)
    d = 0
    isprime = True
    while b<=a[c-1] and not d==c:
            if b==a[d]:
            print a[0:d]
        if d==(c-1) and not b==a[d]:
            break
        d = d + 1
    while b>a[c-1]:
        d = 0
        print a[c-1]
        if b%a[d]==0:
            isprime = False
            break
        while a[d]==a[c-1]:
            f = a[c-1] + 2
            for g in range(f,b,2):
                if b%g==0:
                    isprime = False
                    break
            if isprime:
                a.append(b)
                print a

好吧,我让这个程序工作,以便找到素数时,它们被存储并用于下一组素数。有了这个,让我们说我想找到高达1000的素数。该程序计算素数。然后,我想知道2000年之前的素数。好吧,因为程序已经发现质数高达1000,所以不需要重现它们,所以它需要所有的素数小于或等于最高数量是输入,然后通过将新数除以已知素数来找到剩下的数。然后它将新的素数添加到a,并继续。

唯一的问题是,存在问题。它不想按照我的计划工作,我正在努力修复它。也许你们可以投入并看看有什么问题?

好吧,我编辑了代码以便它运行得更快:

While 1:
    i=input('Enter the number for which all previous shall be tested for primality: ')
    n=0
    while n<=i:
        n=n+1
        a=int(n**.5)
        for b in range(2,n):
            if n%b==0:
                break
             if b==a:
                print n
                break

到目前为止,这个程序的运行时间只有原来的一小部分和我尝试过的程序。在我进行的测试中,我有它,我的第一个算法找到所有素数到100000.我的第一个算法花了4分多一点,不像我的新程序,花了大约1分40秒。如果我可以自己这么说的话,那就是升级。

2 个答案:

答案 0 :(得分:1)

various prime number algorithms,其中筛子最快。如果您熟悉extending python with c,则可以换primesieve。下面是sieve of Eratosthenes的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

答案 1 :(得分:-1)

这应该更快:

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

但我不认为你能比O更快(见塞巴斯蒂安的评论),除非你使用更高级的算法和数字非常巨大的10 ** 100。

数字越大,总是越慢。