The asymptotic you want does not hold just because the quantity $$ g(n)-g(n-1) = 2\sum_{a=1}^n \gcd(a,n)-n $$ is too large. Denoting the sum in the right-hand side by $\sigma(n)$, we have $$ \sigma(n) =\sum_{d\mid n} d\varphi(n/d) = n \sum_{d\mid n} \prod_{p\mid(n/d)} \Big(1-\frac1p\Big) = n \sum_{d\mid n} \prod_{p\mid d} \Big(1-\frac1p\Big). $$ If $n=p_1\dotsb p_k$ is the product of the first $k$ primes, then $$ \sigma(n) = \Big(2-\frac1{p_1}\Big)\dotsb\Big(2-\frac1{p_k}\Big)\,n \ge (3/2)^k n. $$ On the other hand, using the prime number theorem, it is not difficult to see that $k\ge c\ln n/\ln\ln n$ with an absolute constant $c>0$. It follows that for $n=p_1\dotsb p_k$ we have $$ \sigma(n) \ge \exp(c'\ln n/\ln\ln n)\,n $$ while for $n$ prime, $\sigma(n)=2n-1$. Hence, one cannot get a remainder term better than $O(\exp(c'\ln n/\ln\ln n)\,n)$.
Seva
- 23k
- 2
- 59
- 141