Program to find prime numbers

You can do this faster using a nearly optimal trial division sieve in one (long) line like this: Enumerable.Range(0, Math.Floor(2.52*Math.Sqrt(num)/Math.Log(num))).Aggregate( Enumerable.Range(2, num-1).ToList(), (result, index) => { var bp = result[index]; var sqr = bp * bp; result.RemoveAll(i => i >= sqr && i % bp == 0); return result; } ); The approximation formula for … Read more

Sieve of Eratosthenes – Finding Primes Python

You’re not quite implementing the correct algorithm: In your first example, primes_sieve doesn’t maintain a list of primality flags to strike/unset (as in the algorithm), but instead resizes a list of integers continuously, which is very expensive: removing an item from a list requires shifting all subsequent items down by one. In the second example, … Read more