Optimize Sieve of Eratosthenes Further

后端 未结 1 1920
小鲜肉
小鲜肉 2020-11-29 13:11

I have written a Sieve of Eratosthenes--I think--but it seems like it\'s not as optimized as it could be. It works, and it gets all the primes up to N, but not as quickly as

相关标签:
1条回答
  • 2020-11-29 14:11

    a slightly different approach: use a bitarray to represent the odd numbers 3,5,7,... saving some space compared to a list of booleans.

    this may save some space only and not help speedup...

    from bitarray import bitarray
    
    def index_to_number(i): return 2*i+3
    def number_to_index(n): return (n-3)//2
    
    LIMIT_NUMBER = 50
    LIMIT_INDEX = number_to_index(LIMIT_NUMBER)+1
    
    odd_primes = bitarray(LIMIT_INDEX)
    # index  0 1 2 3
    # number 3 5 7 9
    
    odd_primes.setall(True)
    
    for i in range(LIMIT_INDEX):
        if odd_primes[i] is False:
            continue
        n = index_to_number(i)
        for m in range(n**2, LIMIT_NUMBER, 2*n):
            odd_primes[number_to_index(m)] = False
    
    primes = [index_to_number(i) for i in range(LIMIT_INDEX)
              if odd_primes[i] is True]
    primes.insert(0,2)
    
    print('primes: ', primes)
    

    the same idea again; but this time let bitarray handle the inner loop using slice assignment. this may be faster.

    for i in range(LIMIT_INDEX):
        if odd_primes[i] is False:
            continue
        odd_primes[2*i**2 + 6*i + 3:LIMIT_INDEX:2*i+3] = False
    

    (none of this code has been seriously checked! use with care)


    in case you are looking for a primes generator based on a different method (wheel factorizaition) have a look at this excellent answer.

    0 讨论(0)
提交回复
热议问题