Let $d(n)$ be the number of positive integers that divide $n$. It is well known that $d(n)$ is on average $\log n$. However, it is also well known that for most $n$ the number $d(n)$ is rather close to $(\log n)^{\log 2}$. What explains the anomaly is that the values of $d(n)$ that are larger than its typical value are typically way larger, hence, they dominate the first moment. The average of $d^2(n)$ is $\gg (\log n)^3$, which is just another manifestation of the same anomaly.
To understand these things a little better I was interested in estimating the average $$ R(x):= \sum_{1\leq n \leq x } | d(n)-\log n|.$$ Based on the normal order of $d(n)$ this might have correct order $x \log x $. Has anyone seen an asymptotic for this in the literature?
By Cauchy's inequalityAs Alexander Kalmynin wrote in the comments one gets $$R(x) \leq x^{1/2} \left(\sum_{n\leq x}(d^2(n)-2 d(n) \log n+\log^2 n)\right)^{1/2} \ll x (\log x)^{3/2}.$$has Since$$R(x)=\sum_{1\leq n \leq x } |d(n)-\log n| \ll \sum_{n\leq x } d(n) +x\log x\ll x\log x .$$ Furthermore, for each $\epsilon>0$ there exists a subset $A_\epsilon\subset \mathbb N$ of density $1$ with $d(n)\leq (\log n)^{\log 2+\epsilon}$ for all $n\in A_\epsilon$, we get $$ R(x) \geq \sum_{n\in A_\epsilon\cap[1,x]} ((\log n)- (\log n)^{9/10}) \gg x \log x.$$ By https://mathoverflow.net/a/404427/9232 we also see that for any constant $A>1$ the contribution to $R(x)$ of the terms with $d(n)> (\log x)^A$ is at most $$ \ll_A x(\log x)^{A-\left(1+\frac{A}{\log 2}\left(\log\left(\frac{A}{\log 2}\right) -1 \right)\right)}\ll x \log x$$ since the maximum of the function in the exponent is obtained when $A=\log 4$ and equals $1$. The contribution of terms with $d(n) \ll \log x $ is trivially at most $\ll \sum_{n\leq x } \log x\ll x \log x$. HenceHence, for all $\epsilon>0$ we have $$R(x)=O(x\log x)+ \sum_{\substack{1\leq n \leq x \\ \log x <d(n) \leq (\log x)^{1+\epsilon}}} |d(n)-\log n| ,$$ but I cannot see how to bound the second sum by $\ll x \log x $.$$ x\log x\ll R(x) \ll x \log x.$$