The Meissel–Lehmer algorithm (after Ernst Meissel and Derrick Henry Lehmer) is an algorithm that computes the prime-counting function.[1][2]
Description
Key functions
Let
be the first
primes. For natural number a ≥ 1, define

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

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

where the sum only has finitely many nonzero terms, because
when
. Using the fact that
, we get

which prove that one may compute
by computing
and
for k ≥ 2. This is what the Meissel–Lehmer algorithm does.
For
, we get the following formula for
:

For
, the identities for
can be derived similarly.[2]
Expanding 𝜑(x, a)
Using the recurrence

may be expanded. Each summand, in turn, may be expanded in the same way.
Combining the terms
The only thing that remains to be done is evaluating
and
for k ≥ 2, for certain values of
and
. This can be done by direct sieving and using the above formulas.
A modern variant
Jeffrey Lagarias, Victor Miller and Andrew Odlyzko published a realisation of this algorithm which computes
in time
and space
for any
.[1] Upon setting
, the tree of
has
leaf nodes.[1]
References