O(n) solution is here. Understand that first, because the O(\sqrt{n}) is just a sped-up version.
The main idea is that the answer is equal to \sum\limits_{i = 1}^{n}{i \cdot s(\lfloor \frac{n}{i} \rfloor)}, where s(n) is the sum of numbers from 1 to n: \frac{n(n + 1)}{2}. But there are only O(\sqrt{n}) distinct values of \lfloor \frac{n}{i} \rfloor, so you can group them together and compute the entire answer for a value of \lfloor \frac{n}{i} \rfloor with O(1) formulas. See here