I know prime number programs have been beaten to death. I am have been programming for eight years (not that long, but I'm not in my 20s yet and I got a programming job straight out of high school so I'm not doing too shabby). Everyone's first instinct is the "Naive" approach (nested loops, check for divisors), and I wrote my fair share of prime number programs. Over the years, those programs have gotten faster and faster, but I now believe I have reached the fastest possible C# implementation of a prime number finder. It uses a prime sieve and has a small memory footprint. I was wondering if there are any optimizations that anybody else can think of?
List<int> Primes = new List<int>();
Primes.Add(2);
int half = count/2 + 1;
bool[] nums = new bool[half];
for (int i = 0;i<half;i++)
{
if (!nums[i])
{
int number = i * 2 + 3;
Primes.Add(number);
for (int j = i + number; j < half; j += number)
{
nums[j] = true;
}
}
}
return Primes;