Skip to content

Instantly share code, notes, and snippets.

@Joehoel
Created September 8, 2022 12:49
Show Gist options
  • Save Joehoel/c5553b2b80bb29c50b488642120d5fa6 to your computer and use it in GitHub Desktop.
Save Joehoel/c5553b2b80bb29c50b488642120d5fa6 to your computer and use it in GitHub Desktop.
void Erosthenes(int n)
{
bool[] primes = new bool[n + 1];
for (int i = 0; i < primes.Length; i++)
{
primes[i] = true;
}
for (int i = 2; i < Math.Sqrt(n) + 1; i++)
{
if (primes[i - 1])
{
for (int j = (int)Math.Pow(i, 2); j <= n; j += i)
{
primes[j - 1] = false;
}
}
}
int count = 0;
for (int i = 2; i < primes.Length; i++)
{
if (primes[i - 1])
{
Console.WriteLine(i);
count++;
}
}
Console.WriteLine($"There are {count} primes up to {n}");
}
Erosthenes(100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment