TITLE: what is the sum of square of all elements in $U(n)$?
QUESTION [5 upvotes]: I know that $\sum\limits_{a\in U(n)} a=\frac{n\varphi(n)}{2}$ where $U(n):=\{1\leq r\leq n: (r, n)=1\}$ is a multiplicative group. And I know how to prove this result.
What I was willing to know was this $\sum\limits_{a\in U(n)} a^2$. is it possible to find in closed form?
what I tried is the following:
Let $S=\sum\limits_{a\in U(n)} a^2$. Now $(n, a)=1$ shows that $(n, n-a)=1$ which again under the fact  $(a, b)=1, (a, c)=1\Rightarrow (a, bc)=1$, shows that $(n, (n-a)^2)=1$. Hence $\{(n-a_1)^2, \cdots, (n-a_{\varphi(n)})^2\}$ is nothing but a permutation of the original set $\{a_1^2, \cdots, a_{\varphi(n)}^2\}$ in some order. Hence $S=\sum\limits_{a\in U(n)} (n-a)^2$. 
In other words we must have:
\begin{align*}
S=&\sum\limits_{a\in U(n)} (n^2-2an+a^2)\\
 =&n^2 \sum\limits_{a\in U(n)} 1-2n\sum\limits_{a\in U(n)} a+S\\
 =&n^2\varphi(n) -2n\times \frac{n\varphi(n)}{2}+S\\
 =&S
\end{align*}
and no result is obtained. 
What to do ? Please help me. 
Thanks
EDIT:
After the link has been provided below by Robert Israel
the formula reads $ n = p_1^{a_1}p_2^{a_2}\cdots p_r^{a_r}$ then $S = n^2\frac{\varphi(n)}{3}+(-1)^r p_1p_2\cdots p_r\frac{\varphi(n)}{6}$ but how to establish this ?

REPLY [4 votes]: I would like to point out that this one can be solved by Dirichlet series. 
Introduce $$L(s) = \sum_{n\ge 1} \frac{\varphi(n)}{n^s}$$ and recall that 
$$\sum_{d|n}\varphi(d) = n
\quad\text{implies that}\quad
L(s) \zeta(s) = \sum_{n\ge 1} \frac{n}{n^s}
\quad\text{or}\quad
L(s) = \frac{\zeta(s-1)}{\zeta(s)}.$$
By way of an introduction we will now prove that for $n>1$
$$f_1(n) = \sum_{d=1,\atop (d,n)=1}^n d = \frac{1}{2} n \varphi(n)$$
which is equivalent to showing that
$$F_1(s) = \sum_{n\ge 1} \frac{f_1(n)}{n^s}
= \frac{1}{2} + \frac{1}{2} \frac{\zeta(s-2)}{\zeta(s-1)}.$$
The constant term is to account for the value at $n$ being one, which is one.
This is of course trivial by pairing $d$ with $n-d$ but the goal is to
present the method.
Observe that
$$ \frac{1}{2} n (n+1) =
\sum_{d|n} \sum_{q=1\atop (q,n)=d}^n q
= \sum_{d|n} \sum_{p=1\atop (p,n/d)=1}^{n/d} dp
= \sum_{d|n} d\sum_{p=1\atop (p,n/d)=1}^{n/d} p
= \sum_{d|n} d f_1\left(\frac{n}{d}\right).$$
for $n>1.$
Translating to Dirichlet series this gives
$$\frac{1}{2} \zeta(s-2) + \frac{1}{2} \zeta(s-1)
= \zeta(s-1) F_1(s)$$
which implies
$$F_1(s) = 
\frac{1}{2} + \frac{1}{2} \frac{\zeta(s-2)}{\zeta(s-1)}.$$
which proves the claim for $n>1.$

Returning to the original problem we now apply this same method
to show that for $n>1$
$$f_2(n) = \sum_{d=1,\atop (d,n)=1}^n d^2 
= \frac{1}{3} n^2 \varphi(n) + \frac{1}{6} n \beta(n)$$
where $\beta$ is given by $\beta * \varphi = \epsilon$
which is equivalent to showing that
$$F_2(s) = \sum_{n\ge 1} \frac{f_2(n)}{n^s}
=  \frac{1}{2} + \frac{1}{3} \frac{\zeta(s-3)}{\zeta(s-2)} +
\frac{1}{6} \frac{\zeta(s-1)}{\zeta(s-2)} $$
Observe that
$$ \sum_{k=1}^n k^2 = 
\frac{1}{3} n^3 + \frac{1}{2} n^2 + \frac{1}{6} n =
\sum_{d|n} \sum_{q=1\atop (q,n)=d}^n q^2
= \sum_{d|n} \sum_{p=1\atop (p,n/d)=1}^{n/d} (dp)^2
\\= \sum_{d|n} d^2\sum_{p=1\atop (p,n/d)=1}^{n/d} p^2
= \sum_{d|n} d^2 f_2\left(\frac{n}{d}\right).$$
for $n>1.$
Translating to Dirichlet series this gives
$$\frac{1}{3} \zeta(s-3) + \frac{1}{2} \zeta(s-2) + 
\frac{1}{6} \zeta(s-1)
= \zeta(s-2) F_2(s)$$
which implies
$$F_2(s) = 
\frac{1}{2} + 
\frac{1}{3} \frac{\zeta(s-3)}{\zeta(s-2)} +
\frac{1}{6} \frac{\zeta(s-1)}{\zeta(s-2)}.$$
which proves the claim for $n>1.$

Remark. To  evaluate $\beta$ use multiplicativity  and first note
that  $\beta(1)  = 1.$  Then  proceed  by  induction to  obtain  that
$\beta(p)\varphi(1)+\beta(1)\varphi(p)  = 0$  which is  $\beta(p) =
-(p-1).$    Similarly    $\beta(p^2)\varphi(1)+   \beta(p)\varphi(p)
+\beta(1)\varphi(p^2) = 0$ which gives $\beta(p^2) - (p-1)^2 + p^2 -
p = 0$ or $\beta(p^2) = -(p-1)$ leading to the inductive hypothesis that
$$\beta(p^k) = - (p-1).$$
To prove this by induction suppose it is true for $q\lt k$ where $k>2$
to get
$$\sum_{q=0}^k \beta(p^{k-q}) \varphi(p^q)
= \beta(p^k) + (p^k-p^{k-1})
- \sum_{q=1}^{k-1} (p-1) (p^q - p^{q-1}) = 0$$
or
$$\beta(p^k) = -p^k+p^{k-1} +
\sum_{q=1}^{k-1} (p-1) (p^q - p^{q-1}) = 0$$
These terms telescope and we obtain
$$\beta(p^k) = -p^k+p^{k-1} +
(p^k - p) - (p^{k-1} - 1)= -(p-1),$$
which gives for $n = \prod_p p^v$ with $p$ primes that
$$\beta(n) = \prod_p (-1)\times (p-1)
= (-1)^{\omega(n)} \prod_p (p-1)
\\= (-1)^{\omega(n)} \prod_p \left(1-\frac{1}{p}\right)
\prod_p p = (-1)^{\omega(n)} \frac{\varphi(n)}{n}
\prod_p p,$$
which concludes the evaluation of $\beta.$

Closed form. For $f_2(n)$ we have the formula
$$\frac{1}{3} n^2 \varphi(n) + \frac{1}{6} n 
(-1)^{\omega(n)} \frac{\varphi(n)}{n}
\prod_p p
= \frac{1}{3} n^2 \varphi(n) + \frac{1}{6} 
(-1)^{\omega(n)} \varphi(n) \prod_p p,$$
thus proving the claim.

Addendum. We can also evaluate $\beta$ using the fact that 
$\mathrm{Id} * \mu = \varphi$ as pointed out in the first answer.
We get $\beta * \mathrm{Id} * \mu = \epsilon$ or
$$\beta * \mathrm{Id} = 1 * \epsilon = 1.$$

This  implies $\beta(1)  = 1$  and  $\beta(p) +  p =  1$ or  $-(p-1).$
Furthermore, we get $\beta(p^2) - (p-1)  p + p^2 = 1$ or $\beta(p^2) =
-(p-1).$
The induction step now becomes
$$\sum_{q=0}^k \beta(p^{k-q}) p^q
= \beta(p^k) + p^k
- \sum_{q=1}^{k-1} (p-1) p^q = 1.$$
This telescopes once more and we get
$$\beta(p^k) = 1 -p^k + p^k - p = -(p-1).$$
Remark. I just noticed that we have a lot of overlap between this answer and the first one, which is why I will upvote the first answer.