# 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_{i}p_{j}\rfloor -\ldots$
where ⌊x⌋ is the floor function, which denotes the greatest integer less than or equal to x and the p$_i$ run over all primes ≤ √x.
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$, …, p$_n$ be the first n primes. For a natural number a ≥ 1, define
    $\varphi (x,a):=\left|\left\{n\leq 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\leq 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$^k$ $_a$ > x. Using the fact that P$_0$(x, a) = 1 and P$_1$(x, a) = π(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 π(x) by computing φ(x,a) and P$_k$(x, a) for k ≥ 2. This is what the Meissel–Lehmer algorithm does.

### Formula for P$_k$(x, a)

For k = 2, we get the following formula for P$_k$(x, a):
    ${\begin{aligned}P_{2}(x,a)&=\left|\left\{n:n\leq x,~n=p_{b}p_{c},~{\text{with}}~a<b\leq c\right\}\right|\\&=\sum _{b=a+1}^{\pi (x^{1/2})}\left|\left\{n:n\leq x,~n=p_{b}p_{c},~{\text{with}}~b\leq c\leq \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{aligned}}$
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 φ(x,a) can be calculated recursively.

### Combining the terms

The only thing that remains to be done is evaluating φ(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 = π(x$^{1/3}$). He used the resulting equation for calculations of π(x) for big values of x.
Meissel calculated π(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 π(10$^9$) and missed the correct value of π(10$^{10}$) by 1.

## Extended algorithm

Jeffrey Lagarias, Victor Miller and Andrew Odlyzko published a realisation of the algorithm which computes π(x) in time O(x$^{2/3+ε}$) and space O(x$^{1/3+ε}$) for any ε > 0. Upon setting a = π(x$^{1/3}$), the tree of φ(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.