Sieve of Atkin - Pseudocode

Pseudocode

The following is pseudocode for a straightforward version of the algorithm:

// arbitrary search limit limit ← 1000000 // initialize the sieve is_prime(i) ← false, ∀ i ∈ // put in candidate primes: // integers which have an odd number of // representations by certain quadratic forms for (x, y) in × : n ← 4x²+y² if (n ≤ limit) and (n mod 12 = 1 or n mod 12 = 5): is_prime(n) ← ¬is_prime(n) n ← 3x²+y² if (n ≤ limit) and (n mod 12 = 7): is_prime(n) ← ¬is_prime(n) n ← 3x²-y² if (x > y) and (n ≤ limit) and (n mod 12 = 11): is_prime(n) ← ¬is_prime(n) // eliminate composites by sieving for n in : if is_prime(n): // n is prime, omit multiples of its square; this is // sufficient because composites which managed to get // on the list cannot be square-free is_prime(k) ← false, k ∈ {n², 2n², 3n², ..., limit} print 2, 3 for n in : if is_prime(n): print n

This pseudocode is written for clarity. Repeated and wasteful calculations mean that it would run slower than the sieve of Eratosthenes. To improve its efficiency, faster methods must be used to find solutions to the three quadratics. At the least, separate loops could have tighter limits than .

Read more about this topic:  Sieve Of Atkin