Print Prime Value Upto N Prime Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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