TITLE: The Second Moment of a Sum of Floor Functions
QUESTION [13 upvotes]: The following well known identity, where $\tau(n)$ denotes the number of divisors of $n$ appears in many number theory texts
$$
\sum_{k=1}^n \tau(k) = \sum_{d=1}^n \lfloor n/d \rfloor,
$$
and follows from the observation that "one out of d" integers in $\{1,2,\ldots,n\}$
are multiples of $d$ and then summing along rows $d$ as well as along columns $k$ the 
indicator function $1\{~ d~\mathrm{divides}~k~\}$.
Is there a good approximation, or are there any identities related to the following
sum, preferably containing arithmetic functions?
$$
 \sum_{d=1}^n \lfloor n/d \rfloor^2
$$

REPLY [5 votes]: On your comment concerning the sum $\sum_{d\leq n}\left\lfloor\frac{n}{d}\right\rfloor^k$ for various natural numbers $k$, note we have:
$$\sum_{n\leq x}\left\lfloor\frac{x}{n}\right\rfloor^k=\sum_{n\leq x}\sum_{m\leq \frac{x}{n}}m^{k}-(m-1)^k=\sum_{n\leq x}\sum_{d\mid n}d^k-(d-1)^k$$
Thus since we can write: $$\sum_{d\mid n} d^k-(d-1)^k=\sum_{d\mid n}(d^k-\sum_{j=0}^k\binom{k}{j}d^{k-j}(-1)^j)=\sum_{d\mid n}\sum_{j=1}^k \binom{k}{j}d^{k-j}(-1)^{j-1}$$
This means if we let $\sigma_r(n)=\sum_{d\mid n}d^r$ then we must get for all $k\in \mathbb{N}$ that:
$$\sum_{n\leq x}\left\lfloor\frac{x}{n}\right\rfloor^k=\sum_{j=1}^k\binom{k}{j}(-1)^{j-1}\left(\sum_{n\leq x}\sigma_{k-j}(n)\right)$$