小编Ana*_*and的帖子

对的连续质数的对数,相差6,例如(23,29)从1至20亿

如何在考虑时间复杂度的情况下,找到从10到20亿之间有6个类似(23,29)的连续质数对的数量(使用任何编程语言,而无需使用任何外部库)?

  1. 尝试过eratosthenes的筛子,但要获得连续的素数是挑战

  2. 使用过的发电机,但时间复杂度很高

代码是:

def gen_numbers(n):
    for ele in range(1,n+1):
        for i in range(2,ele//2):
            if ele%i==0:
                break
        else:
            yield ele
    prev=0
    count=0
    for i in gen_numbers(2000000000):
        if i-prev==6:
            count+=1
        prev = i
Run Code Online (Sandbox Code Playgroud)

python algorithm math primes sieve-of-eratosthenes

7
推荐指数
1
解决办法
652
查看次数

标签 统计

algorithm ×1

math ×1

primes ×1

python ×1

sieve-of-eratosthenes ×1