Just because I've never written a real Sieve of Eratosthenes, I decided I should probably write one just to make sure I know what it is. I'd like (constructive) criticism on best practices, potential problems, and anything else you see fit:
static int GetNthPrime(int n)
{
List<int> primes = new List<int>() { 2, 3 };
int potentialPrime = 5;
while (primes.Count < n)
{
int squareRootPotentialPrime = (int)Math.Sqrt(potentialPrime);
for (int i = 1; i < primes.Count; i++)
{
if (potentialPrime % primes[i] == 0)
{
break;
}
if (primes[i] > squareRootPotentialPrime)
{
primes.Add(potentialPrime);
break;
}
}
potentialPrime += primes[0];
}
return primes[n - 1];
}
primes[i] > squareRootPotentialPrime
, useprimes[i] * primes[i] > potentialPrime
.Sqrt
is definitely slower than a multiplication. </microoptimisation> \$\endgroup\$