TITLE: Sum involving $\gcd$
QUESTION [7 upvotes]: I was asked to evaluate the sum $\sum_{k = 1}^n \frac{n}{\gcd(n,k)}$ in terms of the prime factorization of $n$. I know that there are $\phi(n)$ integers $k < n$ such that $\gcd(n,k) = 1$ so I tried writting $\sum_{k = 1}^n \frac{n}{\gcd(n,k)} = \phi(n)n + 1 + S$ where $S$ is the contribution from the terms where $k < n$ and $\gcd(n,k) > 1$.
I've also tried evaluating $\sum_{k = 1}^n \frac{\text{lcm}(n,k)}{k}$ but to no avail. 
I would preferably like a hint as opposed to a full solution. Any help is appreciated.

REPLY [3 votes]: If $G$ is the cyclic group of order $n$ and $G=\langle g\rangle $,
then $ord(g^k)=\dfrac{n}{\gcd(n,k)}$.
Therefore,
$$
\sum_{k = 1}^n \frac{n}{\gcd(n,k)}
= \sum_{k = 1}^n ord(g^k)
= \sum_{d\mid n} \sum_{x \in G, ord(x)=d} d
= \sum_{d\mid n} d \phi(d)
$$
For the last sum, see this question.