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
p
1
,
p
2
,
…
,
p
n
{\displaystyle p_{1},p_{2},\ldots ,p_{n}}
be the first n primes. For 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
p
a
{\displaystyle p_{a}}
. Also define for 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 larger 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
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 prove 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.
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]
Meissel already found that for
k
≥
3
,
P
k
(
x
,
a
)
=
0
{\displaystyle k\geq 3,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)}
up to
x
=
1
e
9
{\displaystyle x=1e9}
, but he narrowly missed the correct result for the biggest value of x.[ 1]
Expanding 𝜑 (x , a )
Using 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),}
φ
(
x
,
a
)
{\displaystyle \varphi (x,a)}
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
φ
(
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.
Lehmer analyzed especially the calculation of
φ
(
x
,
a
)
{\displaystyle \varphi (x,a)}
and used already computers to calculate the correct value of
a
=
π
(
1
e
9
)
{\displaystyle a=\pi (1e9)}
.[ 1]
Modern variants
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 are given by M. Deleglise and J. Rivat in 1996.[ 3]
References
^ a b c d e 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 .
^ a b c 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 .
^ 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 .