Possible Duplicate:
Prime Number Formula
I am trying to write some code in c# that will give me the nth prime number but once my code gets past 121 as a prime number it starts giving me incorrect numbers back.
Now this could be that i have based my code off the wrong algorithm but i wanted to ask here and see if there is something i have done wrong.
the code ask for nth prime 10001 – output: 43751 (which i know is wrong)
Any where here is my code.
int[] p;
int x = 0;
p = new int[10002];
for (int i = 0; i < 1000000; i++)
if (i % 2 != 0)
{
if (i % 3 != 0)
{
if (i % 5 != 0)
{
if (i % 7 != 0)
{
p[x] = i;
x++;
if (x == 10001)
{
Console.WriteLine("{0}", i);
break;
}
}
}
}
}
That’s not how the Sieve of Eratosthenes works, you should read this section from the Wikipedia article :
So basically what you think is the limit (
121, from your comments) is just an example they used in that animated.gif.here’s a C# implementation of this method :
Credits go to Rosetta Code