Dirichlet hyperbola method



In number theory, the Dirichlet hyperbola method is a technique to evaluate the sum


 * $$F(n) = \sum_{k=1}^n f(k),$$

where $$f$$ is a multiplicative function. The first step is to find a pair of multiplicative functions $$g$$ and $$h$$ such that, using Dirichlet convolution, we have $$f = g \ast h$$; the sum then becomes


 * $$F(n) = \sum_{k=1}^n \sum_{xy=k}^{} g(x) h(y),$$

where the inner sum runs over all ordered pairs $$(x,y)$$ of positive integers such that $$xy=k$$. In the Cartesian plane, these pairs lie on a hyperbola, and when the double sum is fully expanded, there is a bijection between the terms of the sum and the lattice points in the first quadrant on the hyperbolas of the form $$xy=k$$, where $$k$$ runs over the integers $$1 \leq k \leq n$$: for each such point $$(x,y)$$, the sum contains a term $$g(x)h(y)$$, and vice versa.

Let $$a$$ be a real number, not necessarily an integer, such that $$1 < a < n$$, and let $$b = n/a$$. Then the lattice points can be split into three overlapping regions: one region is bounded by $$1 \leq x \leq a$$ and $$1 \leq y \leq n/x$$, another region is bounded by $$1 \leq y \leq b$$ and $$ 1 \leq x \leq n/y$$, and the third is bounded by $$1 \leq x \leq a$$ and $$1 \leq y \leq b$$. In the diagram, the first region is the union of the blue and red regions, the second region is the union of the red and green, and the third region is the red. Note that this third region is the intersection of the first two regions. By the principle of inclusion and exclusion, the full sum is therefore the sum over the first region, plus the sum over the second region, minus the sum over the third region. This yields the formula

Examples
Let $$\sigma_0(n)$$ be the divisor-counting function, and let $$D(n)$$ be its summatory function:


 * $$D(n) = \sum_{k=1}^n \sigma_0(k).$$

Computing $$D(n)$$ naïvely requires factoring every integer in the interval $$[1, n]$$; an improvement can be made by using a modified Sieve of Eratosthenes, but this still requires $\widetilde{O}(n)$ time. Since $$\sigma_0$$ admits the Dirichlet convolution $$\sigma_0 = 1 \ast 1$$, taking $$a=b=\sqrt{n}$$ in ($$) yields the formula


 * $$D(n) = \sum_{x=1}^{\sqrt{n}} \sum_{y=1}^{n/x} 1 \cdot 1 + \sum_{y=1}^{\sqrt{n}} \sum_{x=1}^{n/y} 1 \cdot 1 - \sum_{x=1}^{\sqrt{n}} \sum_{y=1}^{\sqrt{n}} 1 \cdot 1,$$

which simplifies to
 * $$D(n) = 2 \cdot \sum_{x=1}^{\sqrt{n}} \left\lfloor \frac{n}{x} \right\rfloor - \left\lfloor \sqrt{n} \right\rfloor^2,$$

which can be evaluated in $$O\left(\sqrt{n}\right)$$ operations.

The method also has theoretical applications: for example, Peter Gustav Lejeune Dirichlet introduced the technique in 1849 to obtain the estimate


 * $$D(n) = n \log n + (2\gamma - 1)n + O(\sqrt{n}),$$

where $$\gamma$$ is the Euler–Mascheroni constant.