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.
Find a pattern
I start with a chart to get an idea of a pattern:
Guess and check on a formula
You might notice a pattern already. Whenever we hit a number of balls thats a power of 2, the number of miles increases by one. Let's do another chart, with my guess of a formula:
So the formula
⌊log2(n)+1⌋looks like a good fit. I came up withlog2(n)since that undoes2^n, which is the pattern I noticed in the first chart.I started by trying
⌊log2(n)⌋and⌈log2(n)⌉, but I noticed they were a little off, so I made some changes until I finally arrived at⌊log2(n)+1⌋.Try it out
Try it with decimals. Try it with larger numbers. I did in my head, just to make sure this is really the correct answer.