Algorithm Search⌘K

Proofs: Meissel-Lehmer algorithm

These are the human proofs of Meissel-Lehmer algorithm, at full length.

The Meissel-Lehmer algorithm returns pi(x) full

This proof shows: correctness.

Write p1<p2<⋯p_1 < p_2 < \cdots for the primes, Pb=p1⋯pbP_b = p_1 \cdots p_b, and ϕ(t,b)\phi(t, b) for the number of integers 1≤n≤t1 \le n \le t with no prime factor among p1,…,pbp_1, \ldots, p_b. For m≥2m \ge 2, lpf(m)\mathrm{lpf}(m) is its least prime factor. Two facts are used throughout. First, for real t≥0t \ge 0 and integers u,v≥1u, v \ge 1, ⌊⌊t/u⌋/v⌋=⌊t/(uv)⌋\lfloor \lfloor t/u \rfloor / v \rfloor = \lfloor t/(uv) \rfloor. Second, the inclusion-exclusion form of ϕ\phi: ϕ(t,b)=∑d∣Pbμ(d)⌊t/d⌋\phi(t, b) = \sum_{d \mid P_b} \mu(d) \lfloor t/d \rfloor, because ∑d∣gcd⁡(n,Pb)μ(d)\sum_{d \mid \gcd(n, P_b)} \mu(d) is 1 if gcd⁡(n,Pb)=1\gcd(n, P_b) = 1 and 0 otherwise, and ⌊t/d⌋\lfloor t/d \rfloor counts the n≤tn \le t with d∣nd \mid n.

If x<2x < 2, there is no prime up to xx and line L1 returns 0. Assume x≥2x \ge 2 from now on. Line L2 sets y=⌊x1/3⌋≥1y = \lfloor x^{1/3} \rfloor \ge 1, so y3≤x<(y+1)3y^3 \le x < (y+1)^3, and z=⌊x/y⌋z = \lfloor x/y \rfloor. Line L3 gives the primes up to zz, the table π(t)\pi(t) for t≤zt \le z, lpf\mathrm{lpf} on [2,z][2, z] and μ\mu on [1,y][1, y]; since y≤zy \le z, a=π(y)a = \pi(y) is well defined, and pa≤y<pa+1p_a \le y < p_{a+1}. Since y≤x1/3≤x1/2y \le x^{1/3} \le x^{1/2}, also ⌊x⌋≤x/y\lfloor \sqrt x \rfloor \le x / y, hence ⌊x⌋≤z\lfloor \sqrt x \rfloor \le z.

Step 1: π(x)=ϕ(x,a)+a−1−P2\pi(x) = \phi(x, a) + a - 1 - P_2 with P2P_2 as in line L10. The numbers counted by ϕ(x,a)\phi(x, a) are the 1≤n≤x1 \le n \le x all of whose prime factors are at least pa+1≥y+1>x1/3p_{a+1} \ge y + 1 > x^{1/3}. If such an nn had three prime factors counted with multiplicity, then n>(x1/3)3=xn > (x^{1/3})^3 = x, which is impossible. So nn is 1, a prime pp with y<p≤xy < p \le x, or a product pqpq of primes with y<p≤qy < p \le q and pq≤xpq \le x. The primes number π(x)−a\pi(x) - a. In a product pqpq with p≤qp \le q, p2≤pq≤xp^2 \le pq \le x, so y<p≤⌊x⌋y < p \le \lfloor \sqrt x \rfloor, and for such a pp the possible qq are the primes with p≤q≤⌊x/p⌋p \le q \le \lfloor x/p \rfloor; as p≤⌊x/p⌋p \le \lfloor x/p \rfloor, they number π(⌊x/p⌋)−π(p−1)=π(⌊x/p⌋)−π(p)+1\pi(\lfloor x/p \rfloor) - \pi(p - 1) = \pi(\lfloor x/p \rfloor) - \pi(p) + 1. Each product is counted once, for its smaller factor pp. So ϕ(x,a)=1+(π(x)−a)+P2\phi(x, a) = 1 + (\pi(x) - a) + P_2, which rearranges to the claim. All arguments of π\pi in line L10 are at most zz: ⌊x/p⌋≤⌊x/y⌋=z\lfloor x/p \rfloor \le \lfloor x/y \rfloor = z for p>yp > y, and p≤⌊x⌋≤zp \le \lfloor \sqrt x \rfloor \le z. So line L10 computes P2P_2 from the table.

Step 2: ϕ(x,a)=S0+S\phi(x, a) = S_0 + S. By inclusion-exclusion, ϕ(x,a)=∑d∣Paμ(d)⌊x/d⌋\phi(x, a) = \sum_{d \mid P_a} \mu(d) \lfloor x/d \rfloor. Split the sum by d≤yd \le y and d>yd > y.

The terms with d≤yd \le y. A squarefree m≤ym \le y has all its prime factors at most yy, hence at most pap_a, so m∣Pam \mid P_a; for a non-squarefree mm, μ(m)=0\mu(m) = 0. So these terms are ∑m≤yμ(m)⌊x/m⌋=S0\sum_{m \le y} \mu(m) \lfloor x/m \rfloor = S_0, which is the value set by line L4.

The terms with d>yd > y. Let D\mathcal{D} be the set of d∣Pad \mid P_a with d>yd > y, and T\mathcal{T} the set of triples (m,b,r)(m, b, r) with 0≤b<a0 \le b < a, mm squarefree, m≤y<mpb+1m \le y < m p_{b+1}, every prime factor of mm larger than pb+1p_{b+1}, and r∣Pbr \mid P_b. We show that (m,b,r)↦mpb+1r(m, b, r) \mapsto m p_{b+1} r is a bijection from T\mathcal{T} onto D\mathcal{D}.

It maps into D\mathcal{D}: mm, pb+1p_{b+1} and rr are squarefree, with prime factors in the disjoint ranges (pb+1,pa](p_{b+1}, p_a] (as m≤ym \le y), {pb+1}\{p_{b+1}\} and [p1,pb][p_1, p_b]; so their product is squarefree, divides PaP_a, and is at least mpb+1>ym p_{b+1} > y.

It is onto and one to one. Let d∈Dd \in \mathcal{D} have prime factors q1>q2>⋯>qkq_1 > q_2 > \cdots > q_k. The prefix products 1,q1,q1q2,…,d1, q_1, q_1 q_2, \ldots, d increase, the first is 1≤y1 \le y and the last is d>yd > y, so there is a unique j≥1j \ge 1 with q1⋯qj−1≤y<q1⋯qjq_1 \cdots q_{j-1} \le y < q_1 \cdots q_j. Put m=q1⋯qj−1m = q_1 \cdots q_{j-1}, pb+1=qjp_{b+1} = q_j and r=qj+1⋯qkr = q_{j+1} \cdots q_k. Then (m,b,r)∈T(m, b, r) \in \mathcal{T}: b<ab < a since qj≤paq_j \le p_a, the prime factors of mm exceed qjq_j, and r∣Pbr \mid P_b since its factors are below qjq_j. Conversely, for any (m,b,r)∈T(m, b, r) \in \mathcal{T} with product dd, the decreasing list of the prime factors of dd is that of mm, then pb+1p_{b+1}, then that of rr, so the construction recovers exactly mm, bb and rr: the prefix products through the factors of mm end at m≤ym \le y, and the next one is mpb+1>ym p_{b+1} > y.

Therefore, using μ(mpb+1r)=μ(m)⋅(−1)⋅μ(r)\mu(m p_{b+1} r) = \mu(m) \cdot (-1) \cdot \mu(r) for these coprime squarefree factors and the floor identity,

∑d∈Dμ(d)⌊xd⌋=−∑b=0a−1∑mμ(m)∑r∣Pbμ(r)⌊⌊x/(mpb+1)⌋r⌋=−∑b=0a−1∑mμ(m) ϕ ⁣(⌊xmpb+1⌋,b)=S,\sum_{d \in \mathcal{D}} \mu(d) \left\lfloor \frac{x}{d} \right\rfloor = -\sum_{b=0}^{a-1} \sum_{m} \mu(m) \sum_{r \mid P_b} \mu(r) \left\lfloor \frac{\lfloor x / (m p_{b+1}) \rfloor}{r} \right\rfloor = -\sum_{b=0}^{a-1} \sum_{m} \mu(m)\, \phi\!\left( \left\lfloor \frac{x}{m p_{b+1}} \right\rfloor, b \right) = S,

where the sum over mm runs over the squarefree mm with y/pb+1<m≤yy / p_{b+1} < m \le y whose prime factors exceed pb+1p_{b+1}. For an integer mm, y/pb+1<my / p_{b+1} < m holds if and only if ⌊y/pb+1⌋<m\lfloor y / p_{b+1} \rfloor < m. The condition "every prime factor exceeds pb+1p_{b+1}" is lpf(m)>pb+1\mathrm{lpf}(m) > p_{b+1} for m≥2m \ge 2; and m=1m = 1 never occurs, because pb+1≤pa≤yp_{b+1} \le p_a \le y gives ⌊y/pb+1⌋≥1\lfloor y / p_{b+1} \rfloor \ge 1. Squarefree is μ(m)≠0\mu(m) \ne 0. So the pairs (b,m)(b, m) visited by line L7 are exactly the terms of SS.

Step 3: the Fenwick tree. Let AbA_b be the set of n∈[1,z]n \in [1, z] with no prime factor among p1,…,pbp_1, \ldots, p_b. We claim that at the start of round bb of line L6, the array behind FF holds 1 at the positions in AbA_b and 0 elsewhere. For b=0b = 0, line L5 sets all of [1,z][1, z], which is A0A_0. If the claim holds at the start of round bb, line L9 sets to 0 every multiple of pb+1p_{b+1} in [1,z][1, z] and changes nothing else, so afterwards the 1 positions are AbA_b minus the multiples of pb+1p_{b+1}, which is Ab+1A_{b+1}. The test F[k]=1F[k] = 1 only skips multiples that are already 0. During round bb, lines L7 and L8 run before line L9 and do not change FF, so each query of round bb sees AbA_b, and F.prefix(n)=∣Ab∩[1,n]∣=ϕ(n,b)F.\mathrm{prefix}(n) = |A_b \cap [1, n]| = \phi(n, b) for 0≤n≤z0 \le n \le z. The query index is n=⌊x/(mpb+1)⌋n = \lfloor x / (m p_{b+1}) \rfloor with mpb+1≥y+1m p_{b+1} \ge y + 1, so n≤⌊x/y⌋=zn \le \lfloor x/y \rfloor = z, and the query is in range. Hence line L8 adds exactly the term −μ(m)ϕ(⌊x/(mpb+1)⌋,b)-\mu(m) \phi(\lfloor x / (m p_{b+1}) \rfloor, b) of SS, once for each term.

Conclusion. After the loop of line L6, the variable ϕ\phi equals S0+S=ϕ(x,a)S_0 + S = \phi(x, a) by Step 2. Line L10 computes P2P_2, and line L11 returns ϕ(x,a)+a−1−P2=π(x)\phi(x, a) + a - 1 - P_2 = \pi(x) by Step 1. ■\blacksquare

Back to Meissel-Lehmer algorithm