Meissel–Lehmer algorithm

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

π ( x ) π ( x 1 / 2 ) + 1 = x i x / p i + i < j x / p i p j {\displaystyle \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 {\displaystyle \lfloor x\rfloor } is the floor function, which denotes the greatest integer less than or equal to x and the p i {\displaystyle p_{i}} run over all primes x 1 / 2 {\displaystyle \leq x^{1/2}} .[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 p 1 , p 2 , , p n {\displaystyle p_{1},p_{2},\ldots ,p_{n}} be the first n primes. For a natural number a ≥ 1, define

φ ( x , a ) := | { n x : p | n p > p a } | , {\displaystyle \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 {\displaystyle p_{a}} . Also define for a natural number k,

P k ( x , a ) := | { n x : n = q 1 q 2 q k ,   with   q 1 , , q k > p a } | , {\displaystyle 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 {\displaystyle p_{a}} . With these, we have

φ ( x , a ) = k = 0 P k ( x , a ) , {\displaystyle \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 {\displaystyle P_{k}(x,a)=0} when p a k > x {\displaystyle p_{a}^{k}>x} . Using the fact that P 0 ( x , a ) = 1 {\displaystyle P_{0}(x,a)=1} and P 1 ( x , a ) = π ( x ) a {\displaystyle P_{1}(x,a)=\pi (x)-a} , we get

π ( x ) = φ ( x , a ) + a 1 k = 2 P k ( x , a ) , {\displaystyle \pi (x)=\varphi (x,a)+a-1-\sum _{k=2}^{\infty }P_{k}(x,a),}

which proves that one may compute π ( x ) {\displaystyle \pi (x)} by computing φ ( x , a ) {\displaystyle \varphi (x,a)} and P k ( x , a ) {\displaystyle P_{k}(x,a)} 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 P k ( x , a ) {\displaystyle P_{k}(x,a)} :

P 2 ( x , a ) = | { n : n x ,   n = p b p c ,   with   a < b c } | = b = a + 1 π ( x 1 / 2 ) | { n : n x ,   n = p b p c ,   with   b c π ( x p b ) } | = b = a + 1 π ( x 1 / 2 ) ( π ( x p b ) ( b 1 ) ) = ( a 2 ) ( π ( x 1 / 2 ) 2 ) + b = a + 1 π ( x 1 / 2 ) π ( x p b ) . {\displaystyle {\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 ) {\displaystyle P_{k}(x,a)} can be derived similarly.[1]

Expanding 𝜑(x, a)

With the starting condition

φ ( x , 0 ) = x , {\displaystyle \varphi (x,0)=\lfloor x\rfloor ,}

and the recurrence

φ ( x , a ) = φ ( x , a 1 ) φ ( x p a , a 1 ) , {\displaystyle \varphi (x,a)=\varphi (x,a-1)-\varphi \left({\frac {x}{p_{a}}},a-1\right),}

each value for φ ( x , a ) {\displaystyle \varphi (x,a)} can be calculated recursively.

Combining the terms

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

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

Extended algorithm

Jeffrey Lagarias, Victor Miller and Andrew Odlyzko published a realisation of the algorithm which computes π ( x ) {\displaystyle \pi (x)} in time O ( x 2 / 3 + ε ) {\displaystyle O(x^{2/3+\varepsilon })} and space O ( x 1 / 3 + ε ) {\displaystyle O(x^{1/3+\varepsilon })} for any ε > 0 {\displaystyle \varepsilon >0} .[2] Upon setting a = π ( x 1 / 3 ) {\displaystyle a=\pi (x^{1/3})} , the tree of φ ( x , a ) {\displaystyle \varphi (x,a)} has O ( x 2 / 3 ) {\displaystyle O(x^{2/3})} 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. ^ a b c d e f Lehmer, Derrick Henry (April 1, 1958). "ON THE EXACT NUMBER OF PRIMES LESS THAN A GIVEN LIMIT". Illinois J. Math. 3 (3): 381–388. Retrieved February 1, 2017.
  2. ^ a b c d Lagarias, Jeffrey; Miller, Victor; Odlyzko, Andrew (April 11, 1985). "Computing π ( x ) {\displaystyle \pi (x)} : The Meissel–Lehmer method" (PDF). Mathematics of Computation. 44 (170): 537–560. doi:10.1090/S0025-5718-1985-0777285-5. Retrieved September 13, 2016.
  3. ^ Deleglise, Marc; Rivat, Joël (January 15, 1996). "Computing π ( x ) {\displaystyle \pi (x)} : The Meissel, Lehmer, Lagarias, Miller, Odlyzko method". Mathematics of Computation. 65 (213): 235–245. doi:10.1090/S0025-5718-96-00674-6.
  4. ^ Oliveira e Silva, Tomas (March 1, 2006). "Computing π ( x ) {\displaystyle \pi (x)} : the combinatorial method" (PDF). Revista do Detua. 4 (6): 759–768. Retrieved March 14, 2023.