Computing a Number Theoretic Sum

Published Jan 4, 2025

#math #cs #algorithms

While I was attempting this problem, I misread it after writing it down on paper and instead solved

S=xynxygcd(x,y) S = \sum_{xy \leq n} xy \gcd(x, y)

Here the sum is over positive integers (x,y)(x, y) satisfying xynxy \leq n, rather than x,ynx, y \leq n. In my opinion it turned out more interesting than the original one, so I wanted to share.

Compute S=xynxygcd(x,y)S = \sum_{xy \leq n} xy \gcd(x, y) in O(nlogn)\mathcal{O}(\sqrt{n} \log n).

—Source: Accidental misread

Solution

Setting up notation first, let MM be set of multiplicative functions, and * be the Dirichlet product, then (M,)(M, *) is an abelian group. Let II, τ\tau, ϕ\phi be the identity, divisor count, divisor sum, Mobius, and totient functions respectively. Note that they are all multiplicative.

Write k=xyk = xy so that we can iterate over kk as follows:

S=knkdkgcd(d,k/d)S = \sum_{k \leq n} k \sum_{d \mid k} \gcd(d, k/d)
Key Lemma
d2kτ(k/d2)ϕ(d)=dkgcd(d,k/d)\sum_{d^2 \mid k} \tau(k/d^2) \phi(d) = \sum_{d \mid k} \gcd(d, k/d)

Note here that dd does not represent the same iterator, but I kept the variable name anyways.

Proof of Lemma. A cool fact is that any sum of a product of multiplicative functions with the form of the left side is multiplicative. We can define here

f(d2)={0d is not a squareϕ(d)otherwisef(d^2) = \begin{cases} 0 & \text{$d$ is not a square} \\ \phi(d) & \text{otherwise} \end{cases}

Note that ff is multiplicative, and the left side is actually fτf * \tau, making it multiplicative.

We can verify the right side is multiplicative too, with just the textbook approach. One can notice that if gcd(p,q)=1\gcd(p, q) = 1, dpq    d=rsd \mid pq \implies d = rs, where rpr \mid p and sqs \mid q. Also, gcd(rs,pq/(rs))=gcd(r,p/r)gcd(s,q/s)\gcd(rs, pq/(rs)) = \gcd(r, p/r) \gcd(s, q/s). Therefore,

dpqgcd(d,pq/d)=rpgcd(r,p/r)sqgcd(s,q/s)\sum_{d \mid pq} \gcd(d, pq/d) = \sum_{r \mid p} \gcd(r, p/r) \sum_{s \mid q} \gcd(s, q/s)

It remains to simply check both sides coincide at prime powers:

ia/2τ(pa2i)ϕ(pi)=τ(pa)+1ia/2(a2i+1)pi1(p1)\sum_{i \leq \lfloor a/2\rfloor} \tau(p^{a - 2i}) \phi(p^i) = \tau(p^a) + \sum_{1 \leq i \leq \lfloor a/2\rfloor} (a - 2i + 1) p^{i - 1}(p - 1)
=ia/212pi+(a2a/2+1)pa/2=iapmin(i,ai)= \sum_{i \leq \lfloor a/2\rfloor - 1} 2p^i + (a - 2\lfloor a/2\rfloor + 1) p^{\lfloor a/2\rfloor} = \sum_{i \leq a} p^{\min(i, a - i)}

So we’re done. The sum telescopes in the middle. \square

Therefore

S=knkd2kτ(k/d2)ϕ(d)S = \sum_{k \leq n} k \sum_{d^2 \mid k} \tau(k/d^2) \phi(d)

The goal here was to turn the sum into one consisting of multiplicative functions, so we can try applying Dirichlet’s Hyperbola Method. We can quickly make the observation d2knd^2 \leq k \leq n, and it looks promising to try iterating over dnd \leq \sqrt{n} instead:

S=dnϕ(d)d2kkτ(k/d2)S = \sum_{d \leq \sqrt{n}} \phi(d) \sum_{d^2 \mid k} k \tau(k/d^2)

Now kk has a really simple characterization: it is just d2αd^2 \alpha for αn/d2\alpha \leq n/d^2. Let’s iterate over α\alpha,

S=dnϕ(d)d2αn/d2ατ(α)S = \sum_{d \leq \sqrt{n}} \phi(d) d^2 \sum_{\alpha \leq n/d^2} \alpha \tau(\alpha)

So we would be happy if we can compute partial sums of IτI \tau quickly, luckily it is multiplicative and simple enough, so let’s try to write it as a Dirichlet product:

Iτ=III\tau = I * I

Using the Hyperbola Method on partial sums of IτI \tau, choose splitting point pq=spq = s:

sIτ=pis/ij+qis/ijqipj\sum^s I\tau = \sum^{p} i \sum^{s/i} j + \sum^{q} i \sum^{s/i} j -\sum^{q} i \sum^{p} j

Of course, partial sums of II is an O(1)\mathcal{O}(1) calculation, we can optimally choose p=q=np = q = \sqrt{n}, making the IτI\tau calculation O(n)\mathcal{O}(\sqrt{n}).

Now to resolve

S=dnϕ(d)d2αn/d2ατ(α)S = \sum_{d \leq \sqrt{n}} \phi(d) d^2 \sum_{\alpha \leq n/d^2} \alpha \tau(\alpha)

We can linear sieve the first n\sqrt{n} values of ϕ\phi in the SS sum at no additional symptotic cost. Thus the total time complexity is

dnnd2=Θ(nlogn)\sum_{d \leq \sqrt{n}} \sqrt{\frac{n}{d^2}} = \Theta(\sqrt{n} \log n)

\blacksquare


© 2020-2025 • Last Updated 2025-02-20