Porting optimized Sieve of Eratosthenes from Python to C++

前端 未结 3 959
佛祖请我去吃肉
佛祖请我去吃肉 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:16

    Piggy-Backing onto Howard Hinnant's response, Howard, you don't have to test numbers in the set of all natural numbers not divisible by 2, 3 or 5 for primality, per se. You need simply multiply each number in the array (except 1, which self-eliminates) times itself and every subsequent number in the array. These overlapping products will give you all the non-primes in the array up to whatever point you extend the deterministic-multiplicative process. Thus the first non-prime in the array will be 7 squared, or 49. The 2nd, 7 times 11, or 77, etc. A full explanation here: http://www.primesdemystified.com

提交回复
热议问题