Meissel–Lehmer Algorithm

The “Meissel–Lehmer algorithm” is an algorithm that counts the primes in 1∼n1\sim n in sublinear time.

Notation

[x]\left[x\right] denotes the floor of xx. \npkp_k denotes the kk-th prime, with p1=2p_1=2. \nπ(x)\pi\left(x\right) denotes the number of primes in 1∼x1\sim x. \nμ(x)\mu\left(x\right) denotes the Möbius function. \nFor a set SS, #S\# S denotes the size of SS. \nδ(x)\delta\left(x\right) denotes the smallest prime factor of xx. \nP+(x)P^+\left(x\right) denotes the largest prime factor of xx.

The Meissel–Lehmer algorithm for computing π(x)

Define ϕ(x,a)\phi\left(x,a\right) as the number of positive integers less than xx whose every prime factor is greater than pap_a, namely:

ϕ(x,a)=#{n≤x∣n mod p=0  ⟹  p>pa}(1)\phi\left(x,a\right)=\#\big\{n\le x\mid n\bmod p=0 \implies p>p_a\big\}\tag{1}

We further define Pk(x,a)P_k\left(x,a\right) as the number of positive integers less than xx that have exactly kk prime factors counted with multiplicity and whose every prime factor is greater than pap_a, namely:

Pk(x,a)=#{n≤x∣n=q1q2⋯qk  ⟹  ∀i,qi>pa}(2)P_k\left(x,a\right)=\#\big\{n\le x\mid n=q_1q_2\cdots q_k \implies \forall i,q_i>p_a\big\}\tag{2}

In particular, define P0(x,a)=1P_0\left(x,a\right)=1, so that:

ϕ(x,a)=P0(x,a)+P1(x,a)+⋯+Pk(x,a)+⋯\phi\left(x,a\right)=P_0\left(x,a\right)+P_1\left(x,a\right)+\cdots+P_k\left(x,a\right)+\cdots

This infinite sum can in fact be written as a finite sum, because when pak>xp_a^k>x, we have Pk(x,a)=0P_k\left(x,a\right)=0.

Let yy be an integer satisfying x1/3≤y≤x1/2x^{1/3}\le y\le x^{1/2}, and write a=π(y)a=\pi\left(y\right).

For k≥3k\ge 3, we have P1(x,a)=π(x)−aP_1\left(x,a\right)=\pi\left(x\right)-a and Pk(x,a)=0P_k\left(x,a\right)=0, from which we obtain:

π(x)=ϕ(x,a)+a−1−P2(x,a)(3)\pi\left(x\right)=\phi\left(x,a\right)+a-1-P_2\left(x,a\right)\tag{3}

Thus, computing π(x)\pi\left(x\right) can be reduced to computing ϕ(x,a)\phi\left(x,a\right) and P2(x,a)P_2\left(x,a\right).

Computing P₂(x,a)

By equation (2)\left(2\right), the number P2(x,a)P_2\left(x,a\right) equals the number of prime pairs satisfying y<p≤qy<p\le q and pq≤xpq\le x, where the pair is (p,q)\left(p,q\right).

First note that p∈[y+1,x]p\in \left[y+1,\sqrt{x}\right]. In addition, for each pp, we have q∈[p,x/p]q\in\left[p,x/p\right]. Therefore:

P2(x,a)=∑y<p≤x(π(xp)−π(p)+1)(4)P_2\left(x,a\right)=\sum_{y<p\le \sqrt{x}}{\left(\pi\left(\dfrac{x}{p}\right)-\pi\left(p\right)+1\right)}\tag{4}

When p∈[y+1,x]p\in \left[y+1,\sqrt{x}\right], we have xp∈[1,xy]\dfrac{x}{p}\in \left[1,\dfrac{x}{y}\right]. Thus, we can sieve the interval [1,xy]\left[1,\dfrac{x}{y}\right], and then, for every prime p∈[y+1,x]p\in \left[y+1,\sqrt{x}\right], compute π(xp)−π(p)+1\pi\left(\dfrac{x}{p}\right)-\pi\left(p\right)+1. To reduce the space complexity of this algorithm, we can use blocking with block length LL. If L=yL=y, then O(xylog⁡log⁡x)O\left(\dfrac{x}{y}\log{\log{x}}\right) can be computed in O(y)O\left(y\right) time and P2(x,a)P_2\left(x,a\right) space.

Computing ϕ(x,a)

For b≤ab\le a, consider all positive integers not exceeding xx whose every prime factor is greater than pb−1p_{b-1}. These numbers fall into two classes:

  1. Those divisible by pbp_b;
  2. Those not divisible by pbp_b.

Class 11 contains ϕ(xpb,b−1)\phi\left(\dfrac{x}{p_b},b-1\right) numbers, while class two contains ϕ(x,b)\phi\left(x,b\right) numbers.

Therefore, we conclude:

Theorem 5.15.1: The function ϕ\phi satisfies the following properties

ϕ(u,0)=[u](5)\phi\left(u,0\right)=\left[u\right]\tag{5} ϕ(x,b)=ϕ(x,b−1)−ϕ(xpb,b−1)(6)\phi\left(x,b\right)=\phi\left(x,b-1\right)-\phi\left(\dfrac{x}{p_b},b-1\right)\tag{6}

A simple method for computing ϕ(x,a)\phi\left(x,a\right) can be derived from this theorem: repeatedly apply equation (7)\left(7\right) until finally obtaining ϕ(u,0)\phi\left(u,0\right). This process can be viewed as creating a rooted binary tree beginning with the root node ϕ(x,a)\phi\left(x,a\right); Figure 11 illustrates this process. In this way, we obtain the following formula:

ϕ(x,a)=∑1≤n≤xP+(n)≤yμ(n)[x/n]\phi\left(x,a\right)=\sum_{\substack{1\le n\le x\\ P^+\left(n\right)\le y}}{\mu\left(n\right)\left[x/n\right]} ϕ(x,a)↙↘ϕ(x,a−1)−ϕ(xpa,a−1)↙↓↓↘ϕ(x,a−2)ϕ(xpa−1,a−2)−ϕ(xpa,a−2)ϕ(xpapa−1,a−2)⋮\begin{gathered} \begin{matrix}&&\phi\left(x,a\right)&&\\ &\swarrow&&\searrow&\\ &\phi\left(x,a-1\right)&&-\phi\left(\frac{x}{p_a},a-1\right)&\\ \swarrow&\downarrow&&\downarrow&\searrow\\ \phi\left(x,a-2\right)&\phi\left(\frac{x}{p_{a-1}},a-2\right)&&-\phi\left(\frac{x}{p_a},a-2\right)&\phi\left(\frac{x}{p_ap_{a-1}},a-2\right)\end{matrix}\\ \vdots\\ \end{gathered}

The figure above represents the binary tree for computing ϕ(x,a)\phi\left(x,a\right): the sum of the leaf weights is ϕ(x,a)\phi\left(x,a\right).

However, this requires computing too many things. Since y≥x1/3y\geq x^{1/3}, merely counting numbers that are products of 33 primes not exceeding yy would produce at least xlog⁡3x\dfrac{x}{\log^3 x} terms under this method, which does not meet our complexity requirements.

To limit the “growth” of this binary tree, we need to change the original stopping condition. This is the original stopping condition.

Stopping condition 11: If b=0b=0, do not apply the rule to the node μ(n)ϕ(xn,b)\mu\left(n\right)\phi\left(\dfrac xn,b\right) using equation (6)\left(6\right) again.

We replace it with a stronger stopping condition:

Stopping condition 22: If either of the following 22 conditions holds, do not apply the rule to the node μ(n)ϕ(xn,b)\mu\left(n\right)\phi\left(\dfrac xn,b\right) using equation (6)\left(6\right) again:

  1. b=0b=0 and n≤yn\le y;
  2. n>yn>y.

According to Stopping condition 22, we divide the leaves of the original binary tree into two types:

  1. If the leaf node μ(n)ϕ(xn,b)\mu\left(n\right)\phi\left(\dfrac xn,b\right) satisfies n≤yn\le y, it is called an ordinary leaf;
  2. If the leaf node μ(n)ϕ(xn,b)\mu\left(n\right)\phi\left(\dfrac xn,b\right) satisfies n>yn>y and n=mpb(m≤y)n=mp_b\left(m\le y\right), it is called a special leaf.

Therefore, we obtain:

Theorem 5.25.2: We have:

ϕ(x,a)=S0+S(7)\phi\left(x,a\right)=S_0+S\tag{7}

The contribution of ordinary leaves, S0S_0, is:

S0=∑n≤yμ(n)[xn](8)S_0=\sum_{n\le y}{\mu\left(n\right)\left[\dfrac xn\right]}\tag{8}

The contribution of special leaves, SS, is:

S=∑n/δ(n)≤y≤nμ(n)ϕ(xn,π(δ(n))−1)(9)S=\sum_{n/\delta\left(n\right)\le y\le n}{\mu\left(n\right)\phi\left(\dfrac{x}{n},\pi\left(\delta\left(n\right)\right)-1 \right)}\tag{9}

Computing S0S_0 can clearly be done in O(ylog⁡log⁡x)O\left(y\log{\log x}\right) time; we now consider how to compute SS.

Computing S

We have:

S=−∑p≤y ∑δ(m)>pm≤y<mpμ(m)ϕ(xmp,π(p)−1)(10)S=-\sum_{p\le y}{\ \sum_{\substack{\delta\left(m\right)>p\\ m\le y<mp}}{\mu\left(m\right)\phi\left(\dfrac{x}{mp},\pi\left(p\right)-1\right)}}\tag{10}

We rewrite this equation as:

S=S1+S2+S3S=S_1+S_2+S_3

where:

S1=−∑x1/3<p≤y ∑δ(m)>pm≤y<mpμ(m)ϕ(xmp,π(p)−1)S_1=-\sum_{x^{1/3}<p\le y}{\ \sum_{\substack{\delta\left(m\right)>p\\ m\le y<mp}}{\mu\left(m\right)\phi\left(\dfrac{x}{mp},\pi\left(p\right)-1\right)}} S2=−∑x1/4<p≤x1/3 ∑δ(m)>pm≤y<mpμ(m)ϕ(xmp,π(p)−1)S_2=-\sum_{x^{1/4}<p\le x^{1/3}}{\ \sum_{\substack{\delta\left(m\right)>p\\ m\le y<mp}}{\mu\left(m\right)\phi\left(\dfrac{x}{mp},\pi\left(p\right)-1\right)}} S3=−∑p≤x1/4 ∑δ(m)>pm≤y<mpμ(m)ϕ(xmp,π(p)−1)S_3=-\sum_{p\le x^{1/4}}{\ \sum_{\substack{\delta\left(m\right)>p\\ m\le y<mp}}{\mu\left(m\right)\phi\left(\dfrac{x}{mp},\pi\left(p\right)-1\right)}}

Notice that, in the sums for computing S1,S2S_1,S_2, all occurring mm are prime numbers, as follows:

Otherwise, since δ(m)>p>x1/4\delta\left(m\right)>p>x^{1/4}, we would have m>p2>xm>p^2>\sqrt{x}, contradicting m≤ym\le y; hence the claim follows.

Moreover, when mp>x1/2≥ymp>x^{1/2}\ge y, we have y≤mpy\le mp. Therefore:

S1=∑x1/3<p≤y ∑p<q≤yϕ(xpq,π(p)−1)S_1=\sum_{x^{1/3}<p\le y}{\ \sum_{p<q\le y}{\phi\left(\dfrac{x}{pq},\pi\left(p\right)-1\right)}} S2=∑x1/4<p≤x1/3 ∑p<q≤yϕ(xpq,π(p)−1)S_2=\sum_{x^{1/4}<p\le x^{1/3}}{\ \sum_{p<q\le y}{\phi\left(\dfrac{x}{pq},\pi\left(p\right)-1\right)}}

Computing S₁

Because:

xpq<x1/3<p\dfrac{x}{pq}<x^{1/3}<p

Therefore:

ϕ(xpq,π(p)−1)=1\phi\left(\dfrac{x}{pq},\pi\left(p\right)-1\right)=1

Thus every term in the sum for S1S_1 is 11. We therefore only need to count prime pairs (p,q)\left(p,q\right) satisfying x1/3<p<q≤yx^{1/3}<p<q\le y.

Therefore:

S1=(π(y)−π(x1/3))(π(y)−π(x1/3)−1)2S_1=\dfrac{\left(\pi\left(y\right)-\pi\left(x^{1/3}\right)\right)\left(\pi\left(y\right)-\pi\left(x^{1/3}\right)-1\right)}{2}

This equation permits an O(1)O\left(1\right) computation of S1S_1.

Computing S₂

We have:

S2=∑x1/4<p≤x1/3 ∑p<q≤yϕ(xpq,π(p)−1)S_2=\sum_{x^{1/4}<p\le x^{1/3}}{\ \sum_{p<q\le y}{\phi\left(\dfrac{x}{pq},\pi\left(p\right)-1\right)}}

We split S2S_2 into the two parts q>xp2q>\dfrac x{p^2} and q≤xp2q\le \dfrac x{p^2}:

S2=U+VS_2=U+V

where:

U=∑x1/4<p≤x1/3 ∑p<q<yq>x/p2ϕ(xpq,π(p)−1)U=\sum_{x^{1/4}<p\le x^{1/3}}{\ \sum_{\substack{p<q<y\\q>x/p^2}}{\phi\left(\dfrac{x}{pq},\pi\left(p\right)-1 \right)}} V=∑x1/4<p≤x1/3 ∑p<q<yq≤x/p2ϕ(xpq,π(p)−1)V=\sum_{x^{1/4}<p\le x^{1/3}}{\ \sum_{\substack{p<q<y\\q\le x/p^2}}{\phi\left(\dfrac{x}{pq},\pi\left(p\right)-1 \right)}}

Computing U

Since q>xp2q>\dfrac x{p^2}, we have p2>xq≤xy,p>xyp^2>\dfrac xq\le \dfrac xy,p>\sqrt{\dfrac xy}; therefore:

U=∑x/y<p≤x1/3 ∑p<q≤yq>x/p2ϕ(xpq,π(p)−1)U=\sum_{\sqrt{x/y}<p\le x^{1/3}}{\ \sum_{\substack{p<q\le y\\q>x/p^2}}{\phi\left(\dfrac{x}{pq},\pi\left(p\right)-1 \right)}}

Therefore:

U=∑x/y<p≤x1/3#{q∣xp2<q≤y}U=\sum_{\sqrt{x/y}<p\le x^{1/3}}{\#\left\{q\mid \dfrac x{p^2}<q\le y \right\}}

Therefore:

U=∑x/y<p≤x1/3(π(y)−π(xp2))U=\sum_{\sqrt{x/y}<p\le x^{1/3}}{\left(\pi\left(y\right)-\pi\left(\dfrac{x}{p^2} \right) \right)}

Since xp2<y\dfrac x{p^2}<y, precomputing the table π(t)(t≤y)\pi\left(t\right)\left(t\le y\right) means that, in O(y)O\left(y\right) time, we can compute UU.

Computing V

For every term in the sum for VV, we have p≤xpq<x1/2<p2p\le \dfrac{x}{pq}<x^{1/2}<p^2. Therefore:

ϕ(xpq,π(p)−1)=1+π(xpq)−(π(p)−1)=2−π(p)+π(xpq)\phi\left(\dfrac{x}{pq},\pi\left(p\right)-1 \right)=1+\pi\left(\dfrac{x}{pq} \right)-\left(\pi\left(p\right)-1\right)=2-\pi\left(p\right)+\pi\left(\dfrac{x}{pq} \right)

Thus VV can be written as:

V=V1+V2V=V_1+V_2

where:

V1=∑x1/4<p≤x1/3 ∑p<q≤min⁡(x/p2,y)(2−π(p))V_1=\sum_{x^{1/4}<p\le x^{1/3}}{\ \sum_{p<q\le \min\left(x/p^2,y\right)}{\left(2-\pi\left(p\right)\right)}} V2=∑x1/4<p≤x1/3 ∑p<q≤min⁡(x/p2,y)π(xpq)V_2=\sum_{x^{1/4}<p\le x^{1/3}}{\ \sum_{p<q\le \min\left(x/p^2,y\right)}{\pi\left(\dfrac{x}{pq} \right)}}

After precomputing π(t)(t≤y)\pi\left(t\right)\left(t\le y\right), an O(x1/3)O\left(x^{1/3}\right) computation gives V1V_1.

Consider how to accelerate the computation of V2V_2. We can split the contribution of qq into several intervals on each of which π(xpq)\pi\left(\dfrac{x}{pq} \right) is constant. Then we only need to compute the length of each interval and the change in π(xpq)\pi\left(\dfrac{x}{pq} \right) from one interval to the next.

More precisely, we first split V2V_2 into two parts to simplify the complex condition q≤min⁡(xp2,y)q\le \min\left(\dfrac x{p^2},y\right):

V2=∑x1/4<p≤x/y ∑p<q≤yπ(xpq)+∑x/y<p≤x1/3 ∑p<q≤x/p2π(xpq)V_2=\sum_{x^{1/4}<p\le \sqrt{x/y}}{\ \sum_{p<q\le y}{\pi\left(\dfrac{x}{pq} \right)}}+\sum_{\sqrt{x/y}<p\le x^{1/3}}{\ \sum_{p<q\le x/p^2}{\pi\left(\dfrac{x}{pq} \right)}}

Next, rewrite this expression as:

V2=W1+W2+W3+W4+W5V_2=W_1+W_2+W_3+W_4+W_5

where:

W1=∑x1/4<p≤x/y2 ∑p<q≤yπ(xpq)W_1=\sum_{x^{1/4}<p\le x/y^2}{\ \sum_{p<q\le y}{\pi\left(\dfrac{x}{pq} \right)}} W2=∑x/y2<p≤x/y ∑p<q≤x/pπ(xpq)W_2=\sum_{x/y^2<p\le \sqrt{x/y}}{\ \sum_{p<q\le \sqrt{x/p}}{\pi\left(\dfrac{x}{pq} \right)}} W3=∑x/y2<p≤x/y ∑x/p<q≤yπ(xpq)W_3=\sum_{x/y^2<p\le \sqrt{x/y}}{\ \sum_{\sqrt{x/p}<q\le y}{\pi\left(\dfrac{x}{pq} \right)}} W4=∑x/y<p≤x1/3 ∑p<q≤x/pπ(xpq)W_4=\sum_{\sqrt{x/y}<p\le x^{1/3}}{\ \sum_{p<q\le \sqrt{x/p}}{\pi\left(\dfrac{x}{pq} \right)}} W5=∑x/y<p≤x1/3 ∑x/p<q≤x/p2π(xpq)W_5=\sum_{\sqrt{x/y}<p\le x^{1/3}}{\ \sum_{\sqrt{x/p}<q\le x/p^2}{\pi\left(\dfrac{x}{pq} \right)}}

Computing W₁ and W₂

First consider y<xpq<x1/2y<\dfrac{x}{pq}<x^{1/2}; the required value is π(xpq)\pi\left(\dfrac{x}{pq} \right). It can be obtained by a block sieve on [1,x][1,\sqrt x]. In each block, for every (p,q)(p,q) satisfying the conditions, add π(xpq)\pi\left(\dfrac x{pq}\right).

Computing W₃

For each pp, divide qq into several intervals on each of which π(xpq)\pi\left(\dfrac x{pq}\right) is constant; the contribution of each interval can be computed in O(1)O(1). When a new qq is reached, use the table of π(t)\pi(t) for t≤yt\leq y to compute π(xpq)\pi\left(\dfrac x{pq}\right). The table of primes up to yy gives the π(t)<π(t+1)=π(xpq)\pi(t)<\pi(t+1)=\pi\left(\dfrac x{pq}\right) satisfying tt. Continuing in this way gives the next π(xpq)\pi\left(\dfrac x{pq}\right) at which qq changes.

Computing W₄

Compared with W3W_3, W4W_4 is smaller in qq, so π(xpq)\pi\left(\dfrac x{pq}\right) changes faster. At this point, using the method for computing W3W_3 to compute W4W_4 offers no advantage, so we directly enumerate the pairs (p,q)(p,q) to compute W4W_4.

Computing W₅

As for W3W_3, we compute W5W_5 in the same way.

Computing S₃

Using all primes less than x1/4x^{1/4}, we sieve the interval [1,xy]\left[1,\dfrac xy\right] once. When the sieve reaches pkp_k, we have computed, for all squarefree mm with δ(m)>pk\delta(m)>p_k, the values −μ(m)ϕ(xmpk,k−1)-\mu(m)\phi\left(\dfrac{x}{mp_k},k-1 \right). The sieve is performed in blocks; within each block, we maintain a binary tree to update in real time the intermediate results after sieving by all primes up to a given prime. Thus, we can find the number of values not yet sieved when the sieve reaches a given point in only O(log⁡x)O(\log x) time.

Time and space complexity of the algorithm

The time and space complexity are determined by the following 33 processes:

  1. Computing P2(x,a)P_2\left(x,a\right);
  2. Computing W1,W2,W3,W4,W5W_1,W_2,W_3,W_4,W_5;
  3. Computing S3S_3.

Complexity of computing P₂(x,y)

We already know that this process takes O(xylog⁡log⁡x)O\left(\dfrac{x}{y}\log{\log x}\right) time and O(y)O\left(y\right) space.

Complexity of computing W₁,W₂,W₃,W₄,W₅

The block sieve used to compute W1,W2W_1,W_2 has length yy and takes O(xlog⁡log⁡x)O\left(\sqrt{x}\log{\log x}\right) time and O(y)O\left(y\right) space.

The time complexity required to compute W1W_1 is:

π(xy2)π(y)=O(xylog⁡2x)\pi\left(\dfrac{x}{y^2} \right)\pi\left(y\right)=O\left(\dfrac{x}{y\log^2 x} \right)

The time complexity of computing W2W_2 is:

O(∑x/y2<p≤x/yπ(xp))=O(x3/4y1/4log⁡2x)O\left(\sum_{x/y^2<p\le \sqrt{x/y}}{\pi\left(\sqrt{\dfrac xp}\right)} \right)=O\left(\dfrac{x^{3/4}}{y^{1/4}\log^2 x} \right)

Therefore, the time complexity of computing W3W_3 is:

O(∑x/y2<p≤x/yπ(xp))=O(x3/4y1/4log⁡2x)O\left(\sum_{x/y^2<p\le \sqrt{x/y}}{\pi\left(\sqrt{\dfrac xp}\right)} \right)=O\left(\dfrac{x^{3/4}}{y^{1/4}\log^2 x} \right)

The time complexity of computing W4W_4 is:

O(∑x/y<p≤x1/3π(xp))=O(x2/3log⁡2x)O\left(\sum_{\sqrt{x/y}<p\le x^{1/3}}{\pi\left(\sqrt{\dfrac xp}\right)} \right)=O\left(\dfrac{x^{2/3}}{\log^2 x} \right)

The time complexity of computing W5W_5 is:

O(∑x/y<p≤x1/3π(xp))=O(x2/3log⁡2x)O\left(\sum_{\sqrt{x/y}<p\le x^{1/3}}{\pi\left(\sqrt{\dfrac xp}\right)} \right)=O\left(\dfrac{x^{2/3}}{\log^2 x} \right)

Complexity of computing S₃

For preprocessing: because we need to query ϕ(u,b)\phi(u,b) quickly, the ordinary sieve cannot compute it in O(1)O(1) time. We must maintain a data structure so that each query takes O(log⁡x)O(\log x) time, giving a total time complexity of O(xylog⁡xlog⁡log⁡x)O\left(\dfrac{x}{y}\log x\log\log x\right).

For the summation: for each term in the sum for S3S_3, we query the data structure, for a total of O(log⁡x)O\left(\log x\right) queries. We also need to count the number of terms in the sum, namely the number of leaves in the binary tree. All leaves have the form ±ϕ(xmpb,b−1)\pm\phi\left(\dfrac{x}{mp_b},b-1\right), where m≤y,b<π(x1/4)m\le y,b<\pi(x^{1/4}). Therefore, the number of leaves is on the order of O(yπ(x1/4))O\left(y\pi\left(x^{1/4}\right)\right). Hence the total time complexity for computing S3S_3 is:

O(xylog⁡xlog⁡log⁡x+yx1/4)O\left(\dfrac{x}{y}\log x\log\log x+yx^{1/4}\right)

Overall complexity

The space complexity of this algorithm is O(y)O\left(y\right), and its time complexity is:

O(xylog⁡log⁡x+xylog⁡xlog⁡log⁡x+x1/4y+x2/3log⁡2x)O\left(\dfrac{x}{y}\log{\log x}+\dfrac{x}{y}\log x\log{\log x}+x^{1/4}y+\dfrac{x^{2/3}}{\log^2{x}} \right)

Taking y=x1/3log⁡3xlog⁡log⁡xy=x^{1/3}\log^3{x}\log{\log x} gives the optimal time complexity O(x2/3log⁡2x)O\left(\dfrac{x^{2/3}}{\log^2 x}\right) and space complexity O(x1/3log⁡3xlog⁡log⁡x)O\left(x^{1/3}\log^3{x}\log{\log x}\right).

Some improvements

Here we give improvements that reduce the constant factors of the algorithm and increase its practical efficiency.

  • In Stopping condition 22, we can use zz in place of yy, where zz satisfies z>yz>y. It can be proved that the time complexity of computing S3S_3 can then be optimized to:

    O(xzlog⁡xlog⁡log⁡x+yx1/4log⁡x+z3/2)O\left(\dfrac{x}{z}\log x\log{\log x}+\dfrac{yx^{1/4}}{\log x}+z^{3/2} \right)

    This also provides a good way to check the computation by changing the value of zz.

  • For clarity, when describing the algorithm we choose to split at x1/4x^{1/4} when computing the total sum SS, but in fact it suffices to have p≤xpq<p2p\le \dfrac{x}{pq}<p^2 to perform the computation. This can be used without changing the asymptotic complexity.

  • Preprocessing with the first few primes 2,3,52,3,5 can save additional time.

References and further reading

This article is translated from: Computing π(x)\pi(x): the Meissel, Lehmer, Lagarias, Miller, Odlyzko method

This content comes from OI Wiki: Meissel–Lehmer Algorithm. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.