繁体   English   中英

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

[英]prime number generator in python: accumulation of numbers

我的生成器效果很好:我已经对其进行了多次测试。 只是有一个问题:正如人们可能相信的那样,随着数字的增加,程序变得越来越慢。 我已经想出了一种方法来做,但是不知道怎么做,因为不久前我才刚开始使用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的素数,所以不需要重现它们,因此输入的所有素数小于或等于最高数,然后通过将新数字除以已知素数来找到剩余的内容。 然后,将新素数添加到中,并继续。

唯一的是,有一个问题。 它不想按照我计划的方式工作,而我正在努力对其进行修复。 也许你们可以插手看看有什么问题吗?

好的,我已经编辑了代码,使其运行得更快:

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秒。 如果我自己说的话,相当不错的升级。

有多种质数算法 ,其中筛子是最快的。 如果您熟悉使用c扩展python ,则可以包装primesieve 以下是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

这应该工作得更快:

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快得多(请参阅sebastian的评论),除非您使用的是更高级的算法,并且数字非常大10 ** 100。

数字越大,速度总是越慢。

暂无
暂无

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

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