The Meissel–Lehmer algorithm (after Ernst Meissel and Derrick Henry Lehmer) is an algorithm that computes exact values of the prime-counting function.
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(x1/2)+1=\lfloorx\rfloor-\sumi\lfloorx/pi\rfloor+\sumi<j\lfloorx/pipj\rfloor-\ldots
where is the floor function, which denotes the greatest integer less than or equal to and the run over all primes .
Since the evaluation of this sum formula becomes more and more complex and confusing for large , 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.
Let be the first primes. For a natural number, define
\varphi(x,a):=\left|\left\{n\lex:p|n\impliesp>pa\right\}\right|,
Pk(x,a):=\left|\left\{n\lex:n=q1q2 … qk,~with~q1,\ldots,qk>pa\right\}\right|,
which counts natural numbers no greater than with exactly prime factors, all greater than . With these, we have
\varphi(x,a)=
infty | |
\sum | |
k=0 |
Pk(x,a),
where the sum only has finitely many nonzero terms because when . Using the fact that and, we get
\pi(x)=\varphi(x,a)+a-1-
infty | |
\sum | |
k=2 |
Pk(x,a),
which proves that one may compute by computing and for . This is what the Meissel–Lehmer algorithm does.
For, we get the following formula for :
\begin{align} P2(x,a)&=\left|\left\{n:n\lex,~n=pbpc,~with~a<b\lec\right\}\right|\\ &=
\pi(x1/2) | |
\sum | |
b=a+1 |
\left|\left\{n:n\lex,~n=pbpc,~with~b\lec\le\pi\left(
x | |
pb |
\right)\right\}\right|\\ &=
\pi(x1/2) | |
\sum | |
b=a+1 |
\left(\pi\left(
x | |
pb |
\right)-(b-1)\right)\\ &=\binom{a}{2}-\binom{\pi(x1/2)}{2}+
\pi(x1/2) | |
\sum | |
b=a+1 |
\pi\left(
x | |
pb |
\right). \end{align}
With the starting condition
\varphi(x,0)=\lfloorx\rfloor,
\varphi(x,a)=\varphi(x,a-1)-\varphi\left(
x | |
pa |
,a-1\right),
The only thing that remains to be done is evaluating and for, for certain values of and . This can be done by direct sieving and using the above formulas.
Meissel already found that for, if . He used the resulting equation for calculations of for big values of .
Meissel calculated for values of up to, but he narrowly missed the correct result for the biggest value of .
Using his method and an IBM 701, Lehmer was able to compute the correct value of and missed the correct value of by 1.
Jeffrey Lagarias, Victor Miller and Andrew Odlyzko published a realisation of the algorithm which computes in time and space for any . Upon setting, the tree of has leaf nodes.
This extended Meissel-Lehmer algorithm needs less computing time than the algorithm developed by Meissel and Lehmer, especially for big values of .
Further improvements of the algorithm are given by M. Deleglise and J. Rivat in 1996.
\pi(x)
\pi(x)
\pi(x)