Skip to content

Instantly share code, notes, and snippets.

@PrashantUnity
Created May 6, 2022 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrashantUnity/74055e3710e3f6c81df12948ea8eae6f to your computer and use it in GitHub Desktop.
Save PrashantUnity/74055e3710e3f6c81df12948ea8eae6f to your computer and use it in GitHub Desktop.
Print Prime Value Upto N Prime Number
internal class Program
{
public static void Main()
{
var n = 53;
var prime = SieveOfEratosthenes(n);
for (int i = 2; i <= n; i++)
{
if(prime[i] == 0)
{
Console.WriteLine(i); ;
}
}
}
public static int[] SieveOfEratosthenes(int num)
{
var isPrime = new int[num + 1];
// Removing multiples.
for (int i = 2; i <= num; i++)
{
if (isPrime[i]==0)
{
for (int j = i * 2; j <= num; j += i)
{
// Eliminate multiples of i.
isPrime[j] = 1;
}
}
}
return isPrime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment