First of all - I checked a lot on this forum, and I did not find something fast enough . I am trying to make a function that returns me primes in the specified range. For example, I made this function (in C #) using an Eratosthenes sieve. I also tried the Atkin sieve, but Eratosthenes is faster (in my implementation):
public static void SetPrimesSieve(int Range) { Primes = new List<uint>(); Primes.Add(2); int Half = (Range - 1) >> 1; BitArray Nums = new BitArray(Half, false); int Sqrt = (int)Math.Sqrt(Range); for (int i = 3, j; i <= Sqrt; ) { for (j = ((i * i) >> 1) - 1; j < Half; j += i) Nums[j] = true; do i += 2; while (i <= Sqrt && Nums[(i >> 1) - 1]); } for (int i = 0; i < Half; ++i) if (!Nums[i]) Primes.Add((uint)(i << 1) + 3); }
It works about twice as fast as the codes and algorithms I found ... There must be a faster way to find primes, could you help me?
performance algorithm primes sieve-of-eratosthenes
Ohad Sep 27 '10 at 21:48 2010-09-27 21:48
source share