3

Problem:

Given $n$, Calculate $ \sum_{k=1}^{n} k\cdot \varphi(k) $

This is the oeis series.

My Thoughts:

Oeis gives a couple of approximate estimates/asymptotics but no real formula, exact closed form for this might not be possible. Any algorithm smarter than calculating $\varphi$ all $n$ times is welcome. My hunch says we might be fine with calculating $\varphi$ something of the order of $n^{1/2}$ times.

You can assume $O(n^{2/3})$ memory is available.

I am also thinking on the lines of reducing the problem to some combinations of $\sum_{i=1}^n \varphi(i)$ as I know a few efficient ways to calculate that

To summarise:

  • Expected Time complexity: $O(n^{3/4})$ or better.
  • Expected Space complexity: $O(n^{2/3})$ or better.
sibillalazzerini
  • 470
  • 2
  • 10
  • OEIS provides asymptotics, which is all you can hope to get in general. – Brevan Ellefsen Mar 18 '23 at 10:57
  • If you read the OEIS link you posted, you can see that $$\sum_{k\leqslant n}k,\varphi(k)\sim\frac{2}{\pi^2}n^3.$$ – Luke Collins Mar 18 '23 at 10:58
  • @LukeCollins as mentioned in the question no closed form is expected but something like $O(n^{1/2})$ or $O(n^{3/4})$ is expected . which will be way better than naive approach of iterating over all n and calculating totients which would be $O(n.n^{1/4})=O(n^{5/4})$. – sibillalazzerini Mar 18 '23 at 11:04
  • @LukeCollins additionally you can assume $O(n^{1/2})$ memory is available – sibillalazzerini Mar 18 '23 at 11:06
  • @BrevanEllefsen I am not against asymptotics, just that I need the exact answer here. If you can land on the asymptote and through some computational approach(costing no more than $O(n^{3/4})$ ) correct the error, you are always welcome. – sibillalazzerini Mar 18 '23 at 11:24
  • If you can compute $S_n=\sum_{k=1}^{n} k\cdot\varphi(k)$ in better than $O(n)$ time, then you can compute $(S_{n}-S_{n-1})/n=\varphi(n)$ in better than $O(n)$ time. – Sil Mar 20 '23 at 01:54
  • @Sil Good point. But if you follow the already posted answers and answers to linked questions you would know we can. So either you can prove the answers wrong or I have found a new way to compute $\varphi$ – sibillalazzerini Mar 20 '23 at 02:01
  • It seems $\varphi(n)$ is possible in $O(\sqrt{n})$ since the factorization is possible in such complexity. So your problem complexity seems to be bounded by complexity of factorization algorithms. (unless there is a way to compute $\varphi(n)$ faster without factorizing $n$ first) – Sil Mar 20 '23 at 02:04
  • @Sil Factorization is $O(n^{1/4})$ so time for $\varphi$ is $O(n^{1/4}.log(n))$ – sibillalazzerini Mar 20 '23 at 02:13

2 Answers2

4

This equality might help a bit:

$$\sum_{k=1}^{n} k\cdot \varphi(k) \ \ = \ \ \sum_{d \le n}{\mu(d)\cdot d \cdot S\left(\left[\frac{n}{d}\right]\right)}, \tag{1}$$

where $S(k)$ is the sum of the first $k$ squares, and $\mu(d)$ is the mobius function.

You can use the formula

$$S(k) = \frac{k(k+1)(2k+1)}{6}, \tag{2}$$

which is quite fast, but $\mu(d)$ will raise the total time complexity above your $O\left(n^{\frac{3}{4}}\right)$.

As mentioned in the link you included, you can use the sieving by intervals method to calculate values of the Mobius function across an interval.

With each interval being of length $\sqrt{n}$, the total space complexity will be $O\left(n^{\frac{1}{2}}\right)$, and I believe the total time complexity will be around $O\left(n\log(n)\log^2(\log(n))\right)$.

Though I think it would be quite hard if not impossible to beat a time complexity of $O(n^{3/4})$ using this method.


Proof of $(1)$:

$$\sum_{k=1}^{n} k\cdot \varphi(k) \ \ = \ \ \sum_{k=1}^{n} k \sum_{d\mid k}{\mu(d)\left[\frac{k}{d}\right]}$$

by the inclusion-exclusion principle. Then by changing the order of summations, we have

$$\sum_{d=1}^{n} \mu(d) \ \sum_{k \le n \\ d\mid k}{k\cdot\left[\frac{k}{d}\right]}$$

$$= \sum_{d=1}^{n} \mu(d) \ \sum_{ad \le n}{(ad)\cdot\left[\frac{ad}{d}\right]}$$

$$= \sum_{d=1}^{n} \mu(d) \cdot d \ \sum_{a \le \frac{n}{d}}{a^2}$$

$$= \sum_{d \le n}{\mu(d)\cdot d \cdot S\left(\left[\frac{n}{d}\right]\right)}$$

  • 1
    Can you write a couple of lines explaining the derivation of the lemma. Thanks for your effort by the way. From my sources I can say with certainty some sublinear algorithm exists for sure. Your reduction now lets me search for sublinear algo for $ \sum_{i=1}^{n} i \cdot \mu(i) $ , may be I should create a separate question for that. – sibillalazzerini Mar 19 '23 at 02:48
  • I actually realised I can go upto $O(n^{2/3})$ space , let me know if that spur new ideas. – sibillalazzerini Mar 19 '23 at 05:35
  • @sibillalazzerini I've written a proof of the equality in my answer now. Do you mind letting me see the sources that you are using? I'm struggling to see how this algorithm could be sublinear when even without the Mobius function, computing $S(\left[\frac{n}{d}\right]) \ $ n-times would require at minimum, $O(n)$ operations just for the addition. – Luca Armstrong Mar 19 '23 at 11:01
  • 1
    If you drop the mobius it's $O(n^{1/2})$ . You can solve by splitting it into two parts, first part you traverse from $\sum_{d=1}^{n^{1/2}}$ trivially . For the 2nd part, first you have to observe there are multiple $d$ for which we have same $\lfloor \frac{n}{d} \rfloor$, so we need to club them together, so instead of traversing by $d$ you need to traverse by $\lfloor \frac{n}{d} \rfloor$(let's call it $v$), for each you need to count of how many times $v$ occurs, you can get that by $\lfloor \frac{n}{v} \rfloor-\lfloor \frac{n}{v+1} \rfloor$. so it $O(n^{1/2}+n^{1/2})=O(n^{1/2})$. – sibillalazzerini Mar 19 '23 at 12:59
  • To incorporate the additional $d$ what I said as "count" earlier, it wont be mere count but difference of triangular numbers. Let me know if I was able to explain or you need more clarifications. – sibillalazzerini Mar 19 '23 at 12:59
  • 1
    Oh right, I understand now. So you use my method for the first $\sqrt{n} \ $ values of $d$, then you sum the rest as $$\sum_{v \le \sqrt{n}}{S(v) \ \sum_{\left[\frac{n}{d}\right] = v}{\mu(d)\cdot d}}. \ $$ This means you just need an efficient algorithm for computing $\sum{\mu(d)\cdot d} \ $ when the values of $d$ are in a range. – Luca Armstrong Mar 19 '23 at 13:53
  • Yes exactly, on my other question @reuns have proposed a $O(n^{3/4})$ algo requiring only $O(n^{1/2})$ space which you might want to check. So from what you termed as "impossible to beat O(n)", you get a $O(n^{3/4})$ with more head rooms for improvement if we can make use of the additional space that I am allowing now which is what probably those advanced algorithms like Deléglise & Rivat or similar attempts to do. – sibillalazzerini Mar 20 '23 at 01:45
  • Another thing I wanted to add, there are sequences where there is the pattern of $[/]=$ can sometimes be optimised further(I am not saying all of that is applicable here too). For example I once studied this sequence: http://oeis.org/A006218 . The naive approach is $(n)$, using the above trick you can get $(n^{1/2})$ and using hyperbolic method you can get $(n^{1/3})$ http://arxiv.org/pdf/1206.3369.pdf . I was surprised that you had given up at $(n)$ so soon. – sibillalazzerini Mar 28 '23 at 01:54
  • 1
    Yes, well the $[n/d]$ trick crossed my mind briefly but I just neglected it for some reason. That’s quite interesting using the hyperbolic method though. – Luca Armstrong Mar 28 '23 at 06:54
  • For Divisor problem optimising from $O(n^{1/2})$ to $O(n^{1/3}log n)$ via geometric means is possible. I have observed the class of algos which are $O(n^{3/4})$ can be optimised further by geometric method or some sort of precomputed sieve to $O(n^{2/3}log n)$. This paper does something like that and might be more relevant for this question. – sibillalazzerini Mar 28 '23 at 07:36
2

Found more powerful way of arranging,

let $F(x)=\sum_{k \le x} k\cdot\varphi(k)$

$$ \sum_{i \le x} i \cdot F(x/i) = x(x+1)(2x+1)/6$$

$$ \implies F(x) = x(x+1)(2x+1)/6 \ - \sum_{2 \le i \le x} i \cdot F(x/i)$$

$$ \implies F(x) = x(x+1)(2x+1)/6 \ - \sum_{2 \le i \le \sqrt x} i \cdot F(x/i) \ - \sum_{\sqrt x \lt i \le x} i \cdot F(x/i)$$

let$\ v=x/i$ $$ \implies F(x) = x(x+1)(2x+1)/6 \ - \sum_{2 \le i \le \sqrt x} i \cdot F(x/i) \ - \sum_{\sqrt {x} \lt (x/v) \le x} (x/v) \cdot F(v)$$

$$ \implies F(x) = x(x+1)(2x+1)/6 \ - \sum_{2 \le i \le \sqrt x} i \cdot F(x/i) \ - \sum_{{1} \lt v \le \sqrt {x}} F(v) \sum_{x/v=i}(x/v) $$

Time is primarily governed by the time to sum inside each $F(x)$. And there are $ \sqrt x $ number of $F(i)$ with small $i$ and $ \sqrt x $ number of $F(i)$ with large $i$
Total time = $ \sqrt 1 + \sqrt 2 + \sqrt 3 + ... + \sqrt{\sqrt x} + \sqrt {x/2} + \sqrt {x/3} + ... + \sqrt{\sqrt{x/(x-1)}}$ = $\int\limits_{1}^{\sqrt x} \sqrt t dt +\int\limits_{1}^{\sqrt x} \sqrt {x/t} dt $ = $O(x^{3/4})$

sibillalazzerini
  • 470
  • 2
  • 10