Computing a Number Theoretic Sum
Published Jan 4, 2025
#math #cs #algorithmsWhile I was attempting this problem, I misread it after writing it down on paper and instead solved
Here the sum is over positive integers satisfying , rather than . In my opinion it turned out more interesting than the original one, so I wanted to share.
Compute in sublinear time. Here I present but it can be much faster.
—Source: Accidental misread
Solution
Setting up notation first, let be set of multiplicative functions, and * be the Dirichlet product, then is an abelian group. Let , , , , , , be the -identity (-indicator), constant , identity, divisor count, divisor sum, Mobius, and totient functions respectively. Note that they are all multiplicative.
Write so that we can iterate over as follows:
Key LemmaNote here that 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
Note that is multiplicative, and the left side is actually , making it multiplicative.
We can verify the right side is multiplicative too, with just the textbook approach. One can notice that if , , where and . Also, . Therefore,
It remains to simply check both sides coincide at prime powers:
So we’re done. The sum telescopes in the middle.
Therefore
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 , and it looks promising to try iterating over instead:
Now has a really simple characterization: it is just for . Let’s iterate over ,
So we would be happy if we can compute prefix sums of quickly, luckily it is multiplicative and simple enough, so let’s try to write it as a Dirichlet product:
At this point we are almost at the end, as many methods to calculate prefix sums of and are well known:
, which decomposes it into two functions whose prefix sums can be computed in . With the Hyperbola Method we can compute prefix sums of in .
Prefix sums of can be computed in by linear sieving the first values. Then using a recursion with another Hyperbola Method on the rest, with , while making use of the fact that there are unique values of . See here! This balances out to . In fact, through the recursion we get all the “important values”, the prefix sums up to , for free.
So now using the Hyperbola Method to consolidate , choose splitting point :
As the computation of ’s prefix sums dominate, we can simply pick while also not worrying about quickly obtaining all the “important values” of , which only contributes .
Now to resolve
We can linear sieve for the other part in the sum at no asymptotic cost. Thus the total cost of this is
Although this is asymptotically optimal, in practice for values like the constant factors come into play. Some experimentation shows that it’s better to decrease the precomputation of the linear sieves and rely more on the Hyperbola Method-induced recursion.
Further Improvements
I’m sure that many improvements can be made to this. I haven’t explored too much about it, but here’s some immediate ideas.
- A quick skim of search results tell me prefix sums of and can be calculated even quicker using more advanced methods that I can’t understand.
- I have seen this remarkable blog, showing how to compute the “important values” of the Dirichlet product of two functions whose “important values” we already know in . Combined with the above the whole algorithm can also be .
- A non-improvement: Although , the other part of the multiplicand, is also multiplicative, the Hyperbola Method is limited by a time. So trying to use it on the overall sum with a fast prefix sum gains nothing as long as is used.
- If anyone wants to correct me or share any improvements please do. With that being said I should implement blog commenting.