Porting optimized Sieve of Eratosthenes from Python to C++

前端 未结 3 962
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 12:33

Some time ago I used the (blazing fast) primesieve in python that I found here: Fastest way to list all primes below N

To be precise, this implementation:

         


        
3条回答
  •  难免孤独
    2021-01-03 13:05

    As an aside, you can "approximate" prime numbers. Call the approximate prime P. Here are a few formulas:

    P = 2*k+1 // not divisible by 2

    P = 6*k + {1, 5} // not divisible 2, 3

    P = 30*k + {1, 7, 11, 13, 17, 19, 23, 29} // not divisble by 2, 3, 5

    The properties of the set of numbers found by these formulas is that P may not be prime, however all primes are in the set P. I.e. if you only test numbers in the set P for prime, you won't miss any.

    You can reformulate these formulas to:

    P = X*k + {-i, -j, -k, k, j, i}

    if that is more convenient for you.

    Here is some code that uses this technique with a formula for P not divisible by 2, 3, 5, 7.

    This link may represent the extent to which this technique can be practically leveraged.

提交回复
热议问题