Fastest way to calculate primes in C#?

后端 未结 8 811
孤独总比滥情好
孤独总比滥情好 2021-02-03 09:56

I actually have an answer to my question but it is not parallelized so I am interested in ways to improve the algorithm. Anyway it might be useful as-is for some people.

8条回答
  •  庸人自扰
    2021-02-03 10:35

    There's a very good article about the Sieve of Eratosthenes: The Genuine Sieve of Eratosthenes

    It's in a functional setting, but most of the opimization do also apply to a procedural implementation in C#.

    The two most important optimizations are to start crossing out at P^2 instead of 2*P and to use a wheel for the next prime numbers.

    For concurrency, you can process all numbers till P^2 in parallel to P without doing any unnecessary work.

提交回复
热议问题