what is the sum of square of all elements in $U(n)$? 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 ?
 A: Lemma 1: For any function $f$ defined on rational values in $[0,1]$, denoting, $\displaystyle F(n) = \sum\limits_{k=1}^{n}f\left(\frac{k}{n}\right)$ and $\displaystyle F^{*}(n) = \sum\limits_{1\le k\le n;(k,n)=1} f\left(\frac{k}{n}\right)$.
We have, $\displaystyle 1*F^{*} = F$
Proof: First we observe that each integer $m \in \{1,2,\cdots,n\}$ has a unique representation in the form $m = k.\frac{n}{d}$, where, $d|n$, $1 \le k\le d$ and $(k,d) = 1$. 
Since, $\displaystyle \frac{m}{n} = \frac{k}{d}$, be the reduced fraction i.e., $(k,d) = 1$, each integer $m \le n$ has a representation in the required form, and representation is unique. 
Now, $\displaystyle \begin{align} (1*F^{*})(n) = \sum\limits_{d|n}F^{*}(d) = \sum\limits_{d|n}\sum\limits_{1\le k\le n;(k,d)=1} f\left(\frac{k}{d}\right) &= \sum\limits_{d|n}\sum\limits_{1\le k\le n;(k,d)=1} f\left(\frac{k\frac{n}{d}}{n}\right) \\ &= \sum\limits_{k=1}^{n}f\left(\frac{k}{n}\right) = F(n)\end{align}$
Now, take $\displaystyle f(x) = x^2$, we have $\displaystyle F(n) = \frac{\sum\limits_{k=1}^{n} k^2}{n^2} = \sum\limits_{d|n}\sum\limits_{1\le k\le n;(k,d)=1} \left(\frac{k}{d}\right)^2 = \sum\limits_{d|n} \frac{\phi_2(d)}{d^2}$
Where, $\displaystyle \phi_2(n) = \sum\limits_{k \le n;(k,n) = 1} k^2$
That is, $\displaystyle \frac{1}{6}n(n+1)(2n+1) = (\phi_2*f)(n) = g(n)$, (say)
Applying Dirichlet Inversion to the above identity, we have,
$\displaystyle \begin{align} \phi_2(n) = \sum\limits_{d|n} g(d)f^{-1}\left(\frac{n}{d}\right) \end{align}$
Since, $f$ is an absolutely multiplicative function the inverse is given by $f^{-1}(n) = \mu(n)f(n)$
(a proof of this fact can be found in T. Apostol, Introduction to Analytic Number Theory, page 36)
Thus, 
$\displaystyle \begin{align} \phi_2(n) &= \sum\limits_{d|n} g(d)\left(\frac{d}{n}\right)^2\mu\left(\frac{n}{d}\right) \\ &= \frac{n^2}{3}\sum\limits_{d|n} d\mu\left(\frac{n}{d}\right) + \frac{n^2}{2}\sum\limits_{d|n} \mu\left(\frac{n}{d}\right) + \frac{n}{6}\sum\limits_{d|n} \left(\frac{n}{d}\right)\mu\left(\frac{n}{d}\right) \tag{1} \\ &= \frac{n^2}{3}\phi(n) + \frac{n}{6}\sum\limits_{d|n}d\mu(d) \end{align}$
Where, in $(1)$, we used $\sum\limits_{d|n} d\mu\left(\frac{n}{d}\right) = \phi(n)$ and $\displaystyle \sum\limits_{d|n} \mu(d) = 0$, for $n > 1$.
Now, $\displaystyle \sum\limits_{d|n}d\mu(d) = \prod\limits_{p^\alpha || n} \left(\sum\limits_{e|p^{\alpha}}e\mu(e)\right) = \prod\limits_{p | n}(1-p)$
Thus, $\displaystyle \phi_2(n) = \frac{n^2}{3}\phi(n) + \frac{n}{6}\prod\limits_{p | n}(1-p)$ as required.
A: 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.
A: See OEIS sequence A053818 and references there.
