Algorithm Search⌘K

Proofs: Dirichlet hyperbola method

These are the human proofs of Dirichlet hyperbola method, at full length.

The hyperbola method returns D(n) after exactly floor(sqrt n) divisions full

This proof shows: correctness, termination, time.worst, time.best.

We use the pseudocode HYPERBOLA-D of the page. For an integer nn, D(n)=∑k=1nd(k)D(n) = \sum_{k=1}^{n} d(k), where d(k)d(k) is the number of positive divisors of kk, and D(n)=0D(n) = 0 for n≤0n \le 0.

Lemma 1 (pairs). For n≥1n \ge 1 let P={(a,b)∈Z≥12:ab≤n}P = \{ (a, b) \in \mathbb{Z}_{\ge 1}^2 : ab \le n \}. Then ∣P∣=D(n)|P| = D(n).

Proof. The map (a,b)↦ab(a, b) \mapsto ab sends PP to {1,…,n}\{1, \ldots, n\}. The pairs with ab=kab = k are the (a,k/a)(a, k/a) with aa a positive divisor of kk, so there are d(k)d(k) of them. Summing over k≤nk \le n gives ∣P∣=D(n)|P| = D(n). □\square

Lemma 2 (columns). For a≥1a \ge 1, the number of b≥1b \ge 1 with ab≤nab \le n is ⌊n/a⌋\lfloor n/a \rfloor.

Proof. ab≤nab \le n if and only if b≤n/ab \le n/a, and the positive integers b≤n/ab \le n/a are 1,…,⌊n/a⌋1, \ldots, \lfloor n/a \rfloor. □\square

Theorem 1 (correctness). HYPERBOLA-D(n)(n) returns D(n)D(n).

Proof. If n≤0n \le 0, line L1 returns 0=D(n)0 = D(n). Let n≥1n \ge 1. Line L2 sets ss with s2≤n<(s+1)2s^2 \le n < (s + 1)^2, so s≥1s \ge 1.

Loop invariant. At the start of the iteration with value aa of line L4, t=∑a′=1a−1⌊n/a′⌋t = \sum_{a' = 1}^{a - 1} \lfloor n/a' \rfloor. It holds for a=1a = 1 with t=0t = 0 (line L3), and line L5 adds the term for aa, so it holds for a+1a + 1. When the loop ends, t=∑a=1s⌊n/a⌋t = \sum_{a=1}^{s} \lfloor n/a \rfloor.

Let A={(a,b)∈P:a≤s}A = \{ (a, b) \in P : a \le s \} and B={(a,b)∈P:b≤s}B = \{ (a, b) \in P : b \le s \}.

  1. By Lemma 2, ∣A∣=∑a≤s⌊n/a⌋=t|A| = \sum_{a \le s} \lfloor n/a \rfloor = t.
  2. The swap (a,b)↦(b,a)(a, b) \mapsto (b, a) is a bijection of PP onto itself, since ab=baab = ba, and it maps AA onto BB. So ∣B∣=t|B| = t.
  3. A∪B=PA \cup B = P: if (a,b)∈P(a, b) \in P with a≥s+1a \ge s + 1 and b≥s+1b \ge s + 1, then ab≥(s+1)2>nab \ge (s + 1)^2 > n, which is false.
  4. A∩B={1,…,s}2A \cap B = \{1, \ldots, s\}^2: each pair with a,b≤sa, b \le s has ab≤s2≤nab \le s^2 \le n, so it is in PP, and it is in AA and in BB; the converse is the definition. So ∣A∩B∣=s2|A \cap B| = s^2.

By inclusion and exclusion and Lemma 1, D(n)=∣P∣=∣A∣+∣B∣−∣A∩B∣=2t−s2D(n) = |P| = |A| + |B| - |A \cap B| = 2t - s^2, which line L6 returns. □\square

Theorem 2 (general form). Let f,gf, g be functions on the positive integers, F(x)=∑a≤xf(a)F(x) = \sum_{a \le x} f(a) and G(x)=∑b≤xg(b)G(x) = \sum_{b \le x} g(b). For n≥1n \ge 1 and s=⌊n⌋s = \lfloor \sqrt n \rfloor,

∑(a,b)∈Pf(a)g(b)=∑a≤sf(a)G(⌊n/a⌋)+∑b≤sg(b)F(⌊n/b⌋)−F(s)G(s).\sum_{(a, b) \in P} f(a) g(b) = \sum_{a \le s} f(a) G(\lfloor n/a \rfloor) + \sum_{b \le s} g(b) F(\lfloor n/b \rfloor) - F(s) G(s).

Proof. Give the point (a,b)(a, b) the weight f(a)g(b)f(a) g(b). By Lemma 2, the weight of the column aa is f(a)G(⌊n/a⌋)f(a) G(\lfloor n/a \rfloor), so the weight of AA is the first sum. In the same way the weight of the row bb is g(b)F(⌊n/b⌋)g(b) F(\lfloor n/b \rfloor), and the weight of BB is the second sum. The weight of A∩B={1,…,s}2A \cap B = \{1, \ldots, s\}^2 is ∑a≤s∑b≤sf(a)g(b)=F(s)G(s)\sum_{a \le s} \sum_{b \le s} f(a) g(b) = F(s) G(s). Facts 3 and 4 above hold as before, so inclusion and exclusion for weights gives the formula. With f=g=1f = g = 1 the left side is D(n)D(n) and the formula reduces to Theorem 1. □\square

Theorem 3 (cost). For n≥1n \ge 1 the loop of lines L4 and L5 runs exactly ⌊n⌋\lfloor \sqrt n \rfloor times, with one division and one addition per iteration, and the algorithm uses O(1)O(1) words besides the input.

Proof. The loop runs over a=1,…,sa = 1, \ldots, s with no early exit, so it terminates after exactly ss iterations, and n−1<s≤n\sqrt n - 1 < s \le \sqrt n. The variables are ss, tt and aa. So the time is Θ(n)\Theta(\sqrt n) word operations in every case, best and worst alike, plus the cost of the integer square root of line L2, which Newton's method computes in O(log⁡n)O(\log n) steps. For n≤0n \le 0 the time is O(1)O(1). ■\blacksquare

Back to Dirichlet hyperbola method