76. Parallel primes table

The basic method for building a table of primes loops through odd entries in the table. It calls the IsPrime method for each and saves the result in the table. The IsPrime method simply loops through possible factors that are smaller than the square root of a number and determines whether they divide the number evenly. (Revisit Solution 12. Prime factors, for more details).

The key feature of this method as far as parallelization is concerned is that each step looks at a single entry in the table. This means that each step can proceed without interfering with any of the other steps.

The following code shows how the example solution adapts this method to build a primes table in parallel:

// Use the IsPrime method ...

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.