C# in Depth

Cover of C# in Depth
Order now (3rd edition)

Not quite a Sieve of Eratosthanes

Chapter 3: Parameterized typing with generics: 3.5.1

Created: 2/22/2008
Last updated: 2/22/2008

The method for finding primes in listing 3.13 isn't quite the normal Sieve of Eratosthanes - because it removes multiples of numbers we already know to be non-prime. There's no simple way to change the listing while keeping the point of it (i.e. the use of RemoveAll) which is why I kept it as it is.

We could check the list of candidates in the second for loop, to see if factor is present - but of course this ends up being reasonably expensive too if performed in the simplest manner. A binary search would be quicker, but then we're getting significantly more complicated.