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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.