An improved version Sieve32FastV2Sieve32FastV2 is available.
I don’t consider the code to be complicated but is quite a bit longer than a simple sieve. If a simple sieve is more of your speed, stick with Sieve31Sieve31 or Sieve32Sieve32.
Sieve31Sieve31 a simple sieve for 31 bit primes, or int. Uses BitArray
. Likely candidate for ToList()
.
Sieve32Sieve32 a simple sieve for 32 bit primes, or uint
. Uses BitArray
. Due to memory needs, a bool[]
version is not possible.
EBrown’s answerEBrown’s answer to Sieve31
. Uses a bool[]
. Over 40% faster than Sieve31
but uses 600% the memory. Due to memory constraints, cannot use ToList()
.
The version below is as fast if not faster than EBrown’s answerEBrown’s answer (depends on number of cores) but requires only 4% more memory than Sieve31
.
EBrown’s answerEBrown’s answer took 22.18 seconds and used 1 GB memory.
Sieve32Fast
above took 20.38 seconds and used 149 MB.
- Sieve32Sieve32 took 77.10 seconds and used 283 MB memory.
Sieve32Fast
above took 41.23 seconds and used 293 MB memory.