How many times can I do (n-1)/2 and get a whole number, recursive to formula

78 Views Asked by At

For a given $n$ I want to know how many times I can do $\dfrac{n-1}{2}$ and get a whole number. I also want the result.

For $15: \dfrac{15-1}{2}=7, \dfrac{7-1}{2}=3, \dfrac{3-1}{2}=1, \dfrac{1-1}{2}=0.$

So $15$ will take $4$ steps before it reaches $0$.

Is there any way to do this without recursion? And what are the steps for converting a recursive function like this to a formula?

2

There are 2 best solutions below

2
On BEST ANSWER

What you are asking for is the number of $1$'s at the end of the binary expansion of $n$. When you subtract $1$ and divide by $2$, you are simply deleting the last $1$ at the end of the binary expansion and considering the truncated sequence as the binary expansion of your next number.

When you come to a $0$ at the end of the expansion, you can't proceed any more since subtracting $1$ will give you an odd number.

For instance, $311=(100110111)_2$ will give you the sequence $155=(10011011)_2$, $77=(1001101)_2$, $38=(100110)_2$.

Another interpretation is that you want to find the largest integer $k$ such that your number is congruent to $(2^{k}-1)\mod 2^{k}$. For instance, $311$ is congruent to $7$ mod $8=2^3$, hence you can afford $3$ steps, but no more since $311$ is congruent to $7(\neq 15) \mod 16$.

0
On

Hint

let $$n=2m+1$$ when $m=0,1,2...$