What is the bottleneck in this primes related predicate?

后端 未结 4 1006
旧时难觅i
旧时难觅i 2021-01-19 21:05

So here it is : I\'m trying to calculate the sum of all primes below two millions (for this problem), but my program is very slow. I do know that the algorithm in itself is

4条回答
  •  无人共我
    2021-01-19 21:54

    Consider using for example a sieve method ("Sieve of Eratosthenes"): First create a list [2,3,4,5,6,....N], using for example numlist/3. The first number in the list is a prime, keep it. Eliminate its multiples from the rest of the list. The next number in the remaining list is again a prime. Again eliminate its multiples. And so on. The list will shrink quite rapidly, and you end up with only primes remaining.

提交回复
热议问题