TITLE: Puzzle: Dropping balls along the way
QUESTION [5 upvotes]: A man has some balls in his pocket. Let the number of balls in his pocket be $n$.(Consider $n$ as an integer. If any decimal value occurs, consider its floor value. For example, if $n$ = 2.6 then take it as 2). For every mile that he runs, he is left with half the number of balls.

For instance, if initially he has 10 balls.
  After running the first mile he'll have 5 balls. 
  After running the second mile, he'll have 2 balls.
  After running the third mile, he'll have 1 ball.
  After running the fourth mile, he'll have 0 balls.
  So, it takes 4 miles to lose all the balls.

So how many miles does he have to run, in order to lose $n$ balls.

REPLY [6 votes]: Assume $n$ is written in base $2$,
$$n = \sum_{k = 0}^s a_k 2^k = \overline{a_s \ldots a_1 a_0}^2$$
If $u_t$ is the number of balls after $t$ miles, then $u_0 = n$, and $u_{t+1} = \left\lfloor\frac{u_t}{2}\right\rfloor$. You can easily check that
$$\begin{eqnarray*}
u_0 &=& \overline{a_s \ldots a_2 a_1 a_0}^2 \\
u_1 &=& \overline{a_s \ldots a_2 a_1}^2 \\
u_2 &=& \overline{a_s \ldots a_2}^2 \\
\vdots & & \vdots \\
u_s &=& \overline{a_s}^2
\end{eqnarray*}$$
So he has to run $s+1$ miles to lose all the balls (which is the number of digits of $n$ in base $2$). Now the inequality $2^s \le n < 2^{s+1}$ yields the formula $s = \lfloor \log_2(n) \rfloor$. So finally the answers is $\lfloor \log_2(n) \rfloor + 1$.