Meissel–Lehmer algorithm

From HandWiki
Short description: Prime-counting function algorithm

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

[math]\displaystyle{ \pi (x) - \pi (x^{1/2}) + 1 = \lfloor x \rfloor - \sum_{i} \lfloor x/p_i \rfloor + \sum_{i\lt j} \lfloor x/p_ip_j \rfloor - \ldots }[/math]

where [math]\displaystyle{ \lfloor x \rfloor }[/math] is the floor function, which denotes the greatest integer less than or equal to x and the [math]\displaystyle{ p_i }[/math] run over all primes [math]\displaystyle{ \leq x^{1/2} }[/math].[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 [math]\displaystyle{ p_1, p_2, \ldots, p_n }[/math] be the first n primes. For a natural number a ≥ 1, define

[math]\displaystyle{ \varphi(x, a) := \left| \left \{ n \le x : p|n \implies p \gt p_a \right \} \right|, }[/math]

which counts natural numbers no greater than x with all prime factors greater than [math]\displaystyle{ p_a }[/math]. Also define for a natural number k,

[math]\displaystyle{ P_k(x, a) := \left| \left \{ n \le x : n = q_1 q_2 \cdots q_k, ~\text{with}~ q_1, \ldots, q_k \gt p_a \right \} \right|, }[/math]

which counts natural numbers no greater than x with exactly k prime factors, all greater than [math]\displaystyle{ p_a }[/math]. With these, we have

[math]\displaystyle{ \varphi(x, a) = \sum_{k=0}^\infty P_k(x, a), }[/math]

where the sum only has finitely many nonzero terms because [math]\displaystyle{ P_k(x,a) = 0 }[/math] when [math]\displaystyle{ p_a^k \gt x }[/math]. Using the fact that [math]\displaystyle{ P_0(x,a) = 1 }[/math] and [math]\displaystyle{ P_1(x,a) = \pi(x) - a }[/math], we get

[math]\displaystyle{ \pi(x) = \varphi(x,a) + a - 1 - \sum_{k=2}^\infty P_k(x,a), }[/math]

which proves that one may compute [math]\displaystyle{ \pi(x) }[/math] by computing [math]\displaystyle{ \varphi(x, a) }[/math] and [math]\displaystyle{ P_k(x, a) }[/math] 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 [math]\displaystyle{ P_k(x,a) }[/math]:

[math]\displaystyle{ \begin{align} P_2(x, a) & =\left| \left\{ n : n \le x,~ n = p_b p_c, ~\text{with}~ a \lt 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} }[/math]

For k ≥ 3, the identities for [math]\displaystyle{ P_k(x,a) }[/math] can be derived similarly.[1]

Expanding 𝜑(x, a)

With the starting condition

[math]\displaystyle{ \varphi(x, 0) = \lfloor x \rfloor, }[/math]

and the recurrence

[math]\displaystyle{ \varphi(x, a) = \varphi(x, a-1) - \varphi\left(\frac x {p_a}, a-1 \right), }[/math]

each value for [math]\displaystyle{ \varphi(x,a) }[/math] can be calculated recursively.

Combining the terms

The only thing that remains to be done is evaluating [math]\displaystyle{ \varphi(x, a) }[/math] and [math]\displaystyle{ P_k(x, a) }[/math] 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, [math]\displaystyle{ P_k(x,a) = 0 }[/math] if [math]\displaystyle{ a = \pi(x^{1/3}) }[/math]. He used the resulting equation for calculations of [math]\displaystyle{ \pi(x) }[/math] for big values of [math]\displaystyle{ x }[/math]. [1]

Meissel calculated [math]\displaystyle{ \pi(x) }[/math] for values of x up to [math]\displaystyle{ 10^{9} }[/math], 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 [math]\displaystyle{ \pi\left(10^{9}\right) }[/math] and missed the correct value of [math]\displaystyle{ \pi\left(10^{10}\right) }[/math] by 1.[1]

Extended algorithm

Jeffrey Lagarias, Victor Miller and Andrew Odlyzko published a realisation of the algorithm which computes [math]\displaystyle{ \pi(x) }[/math] in time [math]\displaystyle{ O(x^{2/3+\varepsilon}) }[/math] and space [math]\displaystyle{ O(x^{1/3 + \varepsilon}) }[/math] for any [math]\displaystyle{ \varepsilon \gt 0 }[/math].[2] Upon setting [math]\displaystyle{ a = \pi(x^{1/3}) }[/math], the tree of [math]\displaystyle{ \varphi(x, a) }[/math] has [math]\displaystyle{ O(x^{2/3}) }[/math] 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]

References

  1. 1.0 1.1 1.2 1.3 1.4 1.5 Lehmer, Derrick Henry (April 1, 1958). "ON THE EXACT NUMBER OF PRIMES LESS THAN A GIVEN LIMIT". Illinois J. Math. 3 (3): 381–388. https://projecteuclid.org/download/pdf_1/euclid.ijm/1255455259. Retrieved February 1, 2017. 
  2. 2.0 2.1 2.2 2.3 Lagarias, Jeffrey; Miller, Victor; Odlyzko, Andrew (April 11, 1985). "Computing [math]\displaystyle{ \pi(x) }[/math]: The Meissel–Lehmer method". Mathematics of Computation 44 (170): 537–560. doi:10.1090/S0025-5718-1985-0777285-5. https://www.ams.org/mcom/1985-44-170/S0025-5718-1985-0777285-5/S0025-5718-1985-0777285-5.pdf. Retrieved September 13, 2016. 
  3. Deleglise, Marc; Rivat, Joël (January 15, 1996). "Computing [math]\displaystyle{ \pi(x) }[/math]: The Meissel, Lehmer, Lagarias, Miller, Odlyzko method". Mathematics of Computation 65 (213): 235–245. doi:10.1090/S0025-5718-96-00674-6. https://www.researchgate.net/publication/220577098. 
  4. Oliveira e Silva, Tomas (March 1, 2006). "Computing [math]\displaystyle{ \pi(x) }[/math]: the combinatorial method". Revista do Detua 4 (6): 759–768. https://sweet.ua.pt/tos/bib/5.4.pdf. Retrieved March 14, 2023.