Second moment for the number of divisors function Let $d(n)$ be the number of divisors of an integer $n$.
Does there exists a bound for $\sum_{k\leq n}d^2(k)$?
I saw in a paper of Barry and Louboutin that the asymptotics is $\frac1{\pi^2}nlog^3n$ but there was no proof nor a reference or any indication that this is the best possible bound.
Any idea where can I find this?
Thanks.
 A: Inspired by Gerry Myerson's comment, I present below an elementary argument that does not use any complex analysis. 
Our starting point is the identity
$$ d^2(k)=\sum_{k=l^2m}\mu(l)d_4(m), $$
where $d_4(m)$ enumerates the number of decompositions $m=m_1m_2m_3m_4$. By the multiplicative nature of this equation, it suffices to verify it when $m=p^r$ is a prime power. In this case the identity boils down to
$$ (r+1)^2=\binom{r+3}{3}-\binom{r+1}{3}, $$
which is straightforward to check. With the above identity at hand, we rewrite
$$ \sum_{k\leq n}d^2(k)=\sum_{l^2m\leq n}\mu(l)d_4(m)=\sum_{l\leq\sqrt{n}}\mu(l)\sum_{m\leq\frac{n}{l^2}}d_4(m). $$
We approximate the inner sum by Dirichlet's hyperbola method (cf. Exercise 2 in Section 1.5 of Iwaniec-Kowalski: Analytic Number Theory), and we obtain
$$ \sum_{k\leq n}d^2(k)=\sum_{l\leq\sqrt{n}}\mu(l)\frac{n}{6l^2}\left\{\log^3\left(\frac{n}{l^2}\right)+O\left(\log^2\left(\frac{n}{l^2}\right)\right)\right\}. $$
It follows that
$$ \sum_{k\leq n}d^2(k)=\frac{n\log^3(n)}{6}\left(\sum_{l\leq\sqrt{n}}\frac{\mu(l)}{l^2}\right)+O\left(n\log^2(n)\right) =\frac{n\log^3(n)}{6\zeta(2)}+O\left(n\log^2(n)\right).$$
Finally, using $\zeta(2)=\pi^2/6$ as a black box, we obtain the formula asked by the OP.
A: This can be proved by Perron's formula, making use of the properties of the associated Dirichlet series
$$ g(s):=\sum_{k=1}^\infty\frac{d^2(k)}{k^s},\quad\Re s>1. $$
We have an Euler product decomposition over the primes $p$,
$$ g(s)=\prod_p\left(\sum_{m=0}^\infty\frac{d^2(p^m)}{p^{ms}}\right)=\prod_p\left(\sum_{m=0}^\infty\frac{(m+1)^2}{p^{ms}}\right)=\prod_p\frac{1-p^{-2s}}{(1-p^{-s})^4},\quad \Re s>1.$$
This shows that $g(s)$ has meromorphic extension to $\mathbb{C}$, namely
$$ g(s)=\frac{\zeta(s)^4}{\zeta(2s)}. $$
In particular, $(s-1)^4g(s)$ is holomorphic and bounded in any half-plane $\Re s>\sigma_0>1/2$, and at $s=1$ it takes the value $1/\zeta(2)$. Combining Perron's formula, Cauchy's theorem, the residue theorem, and a contour shift, it is not hard to deduce that
$$ \sum_{k=1}^nd^2(k)\sim\frac{1/\zeta(2)}{3!}n(\log n)^3=\frac{1}{\pi^2}n(\log n)^3.$$
The $3!$ and the $(\log n)^3$ come from the third term in the Taylor expansion
$$ n^{s-1}=\sum_{r=0}^\infty \frac{(\log n)^r}{r!}(s-1)^r.$$
Of course the same method yields secondary terms with smaller powers of $\log n$. Finding a good error term next to these is a difficult task and goes under the label "Dirichlet's divisor problem". You can learn about these things in textbooks of analytic number theory (e.g. Tenenbaum or Montgomery-Vaughan). Hope this helps.
