Write a prime number generator using the Sieve of Eratosthenes. Your program should require exactly one command-line argument, an integer (no smaller than 2) specifying the largest prime number to print. To find the prime numbers up to some maximum n, an array p[i] with indices 2 ≤ i ≤ n is used. Each element p[i] is true if i is prime and false if it is not. Initially every element in p is set to true. For each i between 2 and n, the element p[i] is tested. If p[i] is false then the number is not prime and can be ignored. If p[i] is true then then i is prime and all multiples of it p[n×i] (for n ≥ 1) in the array can be set to false, since they are multiples of a prime factor i. When the process completes, the elements p[i] that are true identify prime numbers i. let P be an array of boolean values indexed by integers 2 to N set all elements of P to true for i in 2 . . . N do if P[i] is true then for j in 2i . . . N do set P[ j] to false for i in 2 . . . N do if P[i] is true then print i
# using System; namespace PrimeNumber { class Program { static void Main(string[] args) { Console.WriteLine("Enter the maximum number to generate primes to:"); int max = int.Parse(Console.ReadLine()); bool[] primes = new bool[max + 1]; // assume all numbers are prime for (int i = 0; i < primes.Length; i++) { primes[i] = true; } // first prime number int p = 2; // loop through array and determine primes for (int i = p; i < primes.Length; i++) { // index is prime so remove multiples if (primes[i]) { for (int j = i + i; j < primes.Length; j = j + i) { // index is not prime primes[j] = false; } } } // output primes