The Meissel–Lehmer algorithm (after Ernst Meissel and Derrick Henry Lehmer) is an algorithm that computes exact values of the prime-counting function.[1][2]
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
where is the floor function, which denotes the greatest integer less than or equal to x and the
run over all primes
.[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 be the first n primes. For a natural number a ≥ 1, define
which counts natural numbers no greater than x with all prime factors greater than . Also define for a natural number k,
which counts natural numbers no greater than x with exactly k prime factors, all greater than . With these, we have
where the sum only has finitely many nonzero terms because when
. Using the fact that
and
, we get
which proves that one may compute by computing
and
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 :
For k ≥ 3, the identities for can be derived similarly.[1]
Expanding 𝜑(x, a)
With the starting condition
and the recurrence
each value for can be calculated recursively.
Combining the terms
The only thing that remains to be done is evaluating and
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, if
. He used the resulting equation for calculations of
for big values of
. [1]
Meissel calculated for values of x up to
, but he narrowly missed the correct result for the biggest value of x.[1]
Using his method and an IBM 701, Lehmer was able to compute the correct value of and missed the correct value of
by 1.[1]
Extended algorithm
Jeffrey Lagarias, Victor Miller and Andrew Odlyzko published a realisation of the algorithm which computes in time
and space
for any
.[2] Upon setting
, the tree of
has
leaf nodes.[2]
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.[3][4]