Meissel–Lehmer algorithm

The Meissel–Lehmer algorithm (after Ernst Meissel and Derrick Henry Lehmer) is an algorithm that computes exact values of the prime-counting function.

Description
The problem of counting the exact number of primes less than or equal to x, without actually listing them all, dates from Legendre. He observed from the Sieve of Eratosthenes that
 * $$\pi (x) - \pi (x^{1/2}) + 1 = \lfloor x \rfloor - \sum_{i} \lfloor x/p_i \rfloor + \sum_{i<j} \lfloor x/p_ip_j \rfloor - \ldots$$

where $$\lfloor x \rfloor$$ is the floor function, which denotes the greatest integer less than or equal to x and the $$p_i$$ run over all primes $$\leq x^{1/2}$$.

Since the evaluation of this sum formula becomes more and more complex and confusing for large x, Meissel tried to simplify the counting of the numbers in the Sieve of Eratosthenes. He and Lehmer therefore introduced certain sieve functions, which are detailed below.

Key functions
Let $$p_1, p_2, \ldots, p_n$$ be the first n primes. For a natural number a ≥ 1, define
 * $$\varphi(x, a) := \left| \left \{ n \le x : p|n \implies p > p_a \right \} \right|,$$

which counts natural numbers no greater than x with all prime factors greater than $$p_a$$. Also define for a natural number k,


 * $$P_k(x, a) := \left| \left \{ n \le x : n = q_1 q_2 \cdots q_k, ~\text{with}~ q_1, \ldots, q_k > p_a \right \} \right|,$$

which counts natural numbers no greater than x with exactly k prime factors, all greater than $$p_a$$. With these, we have


 * $$\varphi(x, a) = \sum_{k=0}^\infty P_k(x, a),$$

where the sum only has finitely many nonzero terms because $$P_k(x,a) = 0$$ when $$p_a^k > x$$. Using the fact that $$P_0(x,a) = 1$$ and $$P_1(x,a) = \pi(x) - a$$, we get


 * $$\pi(x) = \varphi(x,a) + a - 1 - \sum_{k=2}^\infty P_k(x,a),$$

which proves that one may compute $$\pi(x)$$ by computing $$\varphi(x, a)$$ and $$P_k(x, a)$$ for k ≥ 2. This is what the Meissel–Lehmer algorithm does.

Formula for Pk(x, a)
For k = 2, we get the following formula for $$P_k(x,a)$$:
 * $$\begin{align}

P_2(x, a) & =\left| \left\{ n : n \le x,~ n = p_b p_c, ~\text{with}~ a < b \le c \right\} \right| \\ & = \sum_{b = a+1}^{\pi(x^{1/2})} \left| \left\{ n : n \le x,~ n = p_b p_c, ~\text{with}~ b \le c \le \pi \left( \frac{x}{p_b} \right) \right\} \right| \\ & = \sum_{b = a+1}^{\pi(x^{1/2})} \left( \pi\left( \frac{x}{p_b} \right) - (b-1) \right) \\ & = \binom{a}{2} - \binom{\pi(x^{1/2})}{2} + \sum_{b = a+1}^{\pi(x^{1/2})} \pi\left( \frac{x}{p_b} \right). \end{align}$$ For k ≥ 3, the identities for $$P_k(x,a)$$ can be derived similarly.

Expanding 𝜑(x, a)
With the starting condition
 * $$\varphi(x, 0) = \lfloor x \rfloor,$$

and the recurrence
 * $$\varphi(x, a) = \varphi(x, a-1) - \varphi\left(\frac x {p_a}, a-1 \right),$$

each value for $$\varphi(x,a)$$ can be calculated recursively.

Combining the terms
The only thing that remains to be done is evaluating $$\varphi(x, a)$$ and $$P_k(x, a)$$ for k ≥ 2, for certain values of x and a. This can be done by direct sieving and using the above formulas.

History
Meissel already found that for k ≥ 3, $$P_k(x,a) = 0$$ if $$a = \pi(x^{1/3})$$. He used the resulting equation for calculations of $$\pi(x)$$ for big values of $$x$$.

Meissel calculated $$\pi(x)$$ for values of x up to $$10^{9}$$, but he narrowly missed the correct result for the biggest value of x.

Using his method and an IBM 701, Lehmer was able to compute the correct value of $$\pi\left(10^{9}\right)$$ and missed the correct value of $$\pi\left(10^{10}\right)$$ by 1.

Extended algorithm
Jeffrey Lagarias, Victor Miller and Andrew Odlyzko published a realisation of the algorithm which computes $$\pi(x)$$ in time $$O(x^{2/3+\varepsilon})$$ and space $$O(x^{1/3 + \varepsilon})$$ for any $$\varepsilon > 0$$. Upon setting $$a = \pi(x^{1/3})$$, the tree of $$\varphi(x, a)$$ has $$O(x^{2/3})$$ leaf nodes.

This extended Meissel-Lehmer algorithm needs less computing time than the algorithm developed by Meissel and Lehmer, especially for big values of x.

Further improvements of the algorithm are given by M. Deleglise and J. Rivat in 1996.