dividing n with 2 until you get odd number (Function)

1.7k Views Asked by At

So I'm not really sure how to phrase this question so i apologize in advance.

I am trying to find a function that matches the image below. I generated this image by iterating through hole numbers between 1 and 100. Then I counted how many divisions of 2 it could do until it would reach an odd number (The y axis number of divisions).

So my question is, is there any function that matches this graph at the points not necessarily the lines? (I am thinking some kind of trick with modular)

This is the Graph!!!

3

There are 3 best solutions below

2
On

Well, if I understand you question correctly, this is just the number of 2's in factorisation of given number... E.g. $52=2^2*13$, so you can divide by 2 two times, $24=2^3*3$, so you can divide three times etc.

Anyway, if you want you may define $until$ recursively:

$until ( property(x),\ step(x),\ x_{starting}) = \\ -\ if\ property(x_{starting})\ then\ x\\ -\ otherwise\ until ( property(x),\ step(x),\ step(x_{starting}))$

0
On

This function is called the 2-adic valuation. It can be defined on $\mathbb Z^*$ (as you do) or on $\mathbb Q^*$, on a number field or on $\mathbb Z_2$.

https://en.wikipedia.org/wiki/P-adic_order

6
On

How about:

$f(x)=\sum\limits_{n=1}^{\log_2x}1-\Big\lceil x/2^n-\lfloor x/2^n\rfloor\Big\rceil$