
:max_bytes(150000):strip_icc()/GettyImages-478186903-57af3a2b3df78cd39cec5ca1-5c464c1bc9e77c0001b962ee.jpg)
Sieve k | k f (g y)) x = f (g log(log(10000)) is ~2įrom the wikipedia article (which you cited) Sieve of Atkin: _Y g = g (_Y g) - non-sharing fixpoint combinator Time complexity is much improved (to just about a log factor above optimal) by folding in a tree-like structure, and space complexity is drastically improved by multistage primes production, in primes = 2 : _Y ( (3:). The above code is easily tweaked into working on odds only, primes = 2 : 3 : minus (foldr (\p r -> p*p : union r) (tail primes)). Primes = 2 : minus (foldr (\p r -> p*p : union r) In Haskell, we can write down almost word for word the mathematical definition of the sieve of Eratosthenes, " primes are natural numbers above 1 without any composite numbers, where composites are found by enumeration of each prime's multiples": import (minus, union) But I do know that the algorithm I linked takes 8 seconds to figure out all the primes up to 1000000000 on an ancient Pentium II-350 The sieve of Atkin uses a similar approach, but unfortunately I don't know enough about it to explain it to you. Obviously there are quite a few optimizations that can be done to make this algorithm work faster, but this is the basic idea. All the numbers that aren't crossed off are prime. Repeat step 2 until you reach the end of the list.Take the first number that isn't crossed off (for the first iteration this is 2) and cross off all multiples of that number from the list.

Write down a list of numbers from 2 to whatever limit you want, let's say 1000.The sieve or Eratosthenes is probably the most intuitive method of finding a list of primes. I recommend a sieve, either the Sieve of Eratosthenes or the Sieve of Atkin.
