Skip to main content
deleted 3 characters in body; edited tags
Source Link
Heslacher
  • 50.4k
  • 5
  • 81
  • 175

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;

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;

I know prime number programs have been beaten to death. I 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;
rm tag from title
Link
svick
  • 24.3k
  • 4
  • 52
  • 89

C# Prime Number Speed

Source Link

C# Prime Number Speed

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;