Legendre sieve

In mathematics, the Legendre sieve, named after Adrien-Marie Legendre, is the simplest method in modern sieve theory. It applies the concept of the Sieve of Eratosthenes to find upper or lower bounds on the number of primes within a given set of integers. Because it is a simple extension of Eratosthenes' idea, it is sometimes called the Legendre–Eratosthenes sieve.

Legendre's identity
The central idea of the method is expressed by the following identity, sometimes called the Legendre identity:


 * $$S(A,P)= \sum_{a\in A}\sum_{d\mid a;\,d\mid P} \mu(d) =\sum_{d\mid P}\mu(d)|A_d|,$$

where A is a set of integers, P is a product of distinct primes, $$\mu$$ is the Möbius function, and $$A_d$$ is the set of integers in A divisible by d, and S(A, P) is defined to be:


 * $$S(A, P) = |\{n: n \in A, \gcd(n, P) = 1\}|$$

i.e. S(A, P) is the count of numbers in A with no factors common with P.

Note that in the most typical case, A is all integers less than or equal to some real number X, P is the product of all primes less than or equal to some integer z &lt; X, and then the Legendre identity becomes:



\begin{align} S(A,P) = {} & \sum_{d\mid P}\mu(d)\left\lfloor\frac{X}{d}\right\rfloor \\[6pt] = {} & \lfloor X \rfloor - \sum_{p_1 \le z} \left\lfloor\frac{X}{p_1}\right\rfloor + \sum_{p_1 < p_2 \le z} \left\lfloor \frac{X}{p_1p_2}\right\rfloor \\[4pt] & {} - \sum_{p_1 < p_2 < p_3 \le z} \left\lfloor\frac{X}{p_1p_2p_3}\right\rfloor + \cdots + \mu(P) \left\lfloor \frac{X}{P} \right\rfloor \end{align} $$

(where $$\lfloor X \rfloor$$ denotes the floor function). In this example the fact that the Legendre identity is derived from the Sieve of Eratosthenes is clear: the first term is the number of integers below X, the second term removes the multiples of all primes, the third term adds back the multiples of two primes (which were miscounted by being "crossed out twice") but also adds back the multiples of three primes once too many, and so on until all $$2^{\pi(z)}$$ (where $$\pi(z)$$ denotes the number of primes below z) combinations of primes have been covered.

Once S(A, P) has been calculated for this special case, it can be used to bound $$\pi(X)$$ using the expression


 * $$S(A,P) \geq \pi(X) - \pi(z) + 1, \, $$

which follows immediately from the definition of S(A, P).

Limitations
The Legendre sieve has a problem with fractional parts of terms accumulating into a large error, which means the sieve only gives very weak bounds in most cases. For this reason it is almost never used in practice, having been superseded by other techniques such as the Brun sieve and Selberg sieve. However, since these more powerful sieves are extensions of the basic ideas of the Legendre sieve, it is useful to first understand how this sieve works.