TITLE: How to calculate these totient summation sums efficiently?
QUESTION [19 upvotes]: I am trying to find good ways to tackle sums of the form
$\sum_{k=1}^{N}k^j\varphi(k)$ 
$j$ can be anything but I am largely concerned about cases 0, 1, and 2.
$\varphi(k)$ is the Euler totient function.
Can this be done without needing to calculate $k^j\varphi(k)$ manually for every single step of $k$? Is there any optimization opportunity? Any identities that apply here that might help?

REPLY [9 votes]: A sublinear algorithm, given by daniel.is.fischer, using the identity 
$$F(N) = \sum_{m=1}^N R \left( \Big \lfloor \frac N m \Big \rfloor \right)$$
rewrites $$ R(N) = F(N) - \displaystyle\sum_{m=2}^N{ R\left(\Big\lfloor\dfrac{N}{m}\Big\rfloor\right) } $$ into $$ R(N) = F(N) - F\left( \Big\lfloor \frac{N}{2} \Big\rfloor \right)  - \sum_{k=1}^{\lfloor(N-1)/2\rfloor}{ R\left(\Big\lfloor\frac{N}{2k+1}\Big\rfloor\right) } $$ with $O(N^{3/4})$ time "if storing and retrieving the values are constant time operations." From Project Euler PDF 73 (73.10).

All the other answers are based on this Project Euler problem which deals with the totient sum in terms of the Farey sequence. There is a geometric way to view this (partially based on the explanation by philliplu in problem 625): Starting with $\sum_{d|n} \phi(d) = n$
we rewrite this as a double sum equal to the triangle numbers: $$T(n) = \sum_{a=1}^n \sum_{b|a} \phi(b) =  \sum_{a=1}^n \sum_{b|a} \phi \left(\frac a b \right)$$
Writing out this sum in rows for $n=6$ (We can make a very similar triangle for reduced fractions):
\begin{matrix}
a=1: & \phi(1) & &\\
a=2: & \phi(2) & \phi(1) & \\
a=3: & \phi(3) & & \phi(1) \\
a=4: & \phi(4) & \phi(2) & & \phi(1) \\
a=5: & \phi(5) & & & & \phi(1) \\
a=6: & \phi(6) & \phi(3) & \phi(2) & & & \phi(1) 
\end{matrix}
The first column doesn't skip any values. The next column has a totient value every two numbers, the column after that has totient values every three numbers, and so on.
Let $\Phi(n) = \phi(1) + \cdots + \phi(n)$. By summing over the columns we can see 
$$T(n) = \sum_{x=1}^n \Phi \left(\Big \lfloor \frac n x \Big \rfloor \right)$$
Rearrange to solve for $\Phi(n)$:
$$\Phi(n) = T(n) - \sum_{x=2}^n \Phi \left(\Big \lfloor \frac n x \Big \rfloor \right)$$
The observation is that for large $x$ (consider $x \ge \sqrt n$), $\lfloor n/x \rfloor$ is constant for many values. (A similar idea is used in my previous question for calculating $\sum \sigma(n)$.) We can calculate precisely how many times each $\Phi( k )$ value occurs. For $x$ in $(\lfloor n/2 \rfloor, n], \lfloor n/x \rfloor = 1$; for $x$ in $(\lfloor n/3 \rfloor, \lfloor n/2 \rfloor], \lfloor n/x \rfloor = 2$; etc.
Using this observation, we arrive at our $O(n^{3/4})$ formula:
$$\Phi(n) = T(n) - \sum_{x=2}^{\lfloor \sqrt n \rfloor} \Phi \left(\Big \lfloor \frac n x \Big \rfloor \right) - \sum_{y=1}^{y_{max}} \left(\Big \lfloor \frac n y \Big \rfloor - \Big \lfloor \frac n {y+1} \Big \rfloor \right) \Phi(y)$$
The summation bound $y_{max}$ may need to be adjusted slightly for an edge case. More precisely:
$$
y_{max} = 
\begin{cases}
\lfloor \sqrt{n} \rfloor - 1, & \text{if} \  \lfloor \sqrt{n} \rfloor = \left \lfloor \frac{n}{\lfloor \sqrt{n} \rfloor} \right\rfloor \\
\lfloor \sqrt{n} \rfloor, & \text{otherwise}
\end{cases}$$
For implementation, we can memoize by using pre-sieved totient values for $k \le n^{2/3}$ to calculate $\Phi(k)$ at an almost linear cost, and a dictionary or an array indexed by $x$ to save large $\Phi(k)$, for an overall cost of almost $O(n^{2/3})$. 

Any formula that can express $h(n)$ as a Dirichlet convolution 
$$h(n) = (f * g) (n) = \sum_{d|n} f(d) g(n/d)$$ 
has a sub-linear algorithm using the Dirichlet hyperbola method.