I'm trying to calculate the sum of integers that are being divided by two (applying the floor function if necessary): $n\mapsto \lfloor \frac{n}{2}\rfloor$.
Let $S(n)=n+\lfloor\frac{n}{2}\rfloor+\left\lfloor\frac{\lfloor\frac{n}{2}\rfloor}{2}\right\rfloor+\ldots$.
For example, $$ \begin{align*} S(100) &= 100 + 50 + 25 + 12 + 6 + 3 + 1 + 0 +\ldots\\ S(3) &= 3 + 1 + 0 + \ldots\\ S(1000) &= 1000 + 500 + 250 + 125 + 62 + 31 + 15 + 7 + 3 + 1 + 0 + \ldots\end{align*} $$
I'm trying to find a closed form for $S(n)$, where $n\in \mathbb N$. Any ideas?
[Solution] A lot of great answers. Thanks! Here's my java implementation.
int computeHalvesSum(int n) {
return 2 * n - Integer.bitCount(n)
}
See OEIS sequence A005187 and references there. Depending on what language you're using, the simplest way to compute it may be as $2n - (\text{sum of binary digits of }n)$.