12. Prime table

The obvious approach is to loop through all of the values in the array and call the IsPrime method, described in the previous solution, to see which values are prime. The following method uses this approach:

// Use the IsPrime method to make a table of prime numbers.private bool[] MakePrimesTable(int max){    // Make the array and mark 2 and odd numbers greater than 1 as     // prime.    bool[] isPrime = new bool[max + 1];    isPrime[2] = true;    for (int i = 3; i <= max; i += 2) isPrime[i] = IsPrime(i);    return isPrime;}

This method allocates an array big enough to hold all of the desired values. It then sets isPrime[2] = true because 2 is prime. It leaves the other even values alone because they are set to false when the array is allocated. ...

Get The Modern C# Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.