Function to define sequence $1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,1,\dots$

Given the following sequence:

$1,2,1,3,1,2,1,4,1,2,1,3,1,2,1,5,1,\dots$

Let $x_1$ be the first element in the sequence, $x_2$ be the second, $x_3$ be the third and so on.

Let the function $f(i)$ be defined such that $f(i) = x_i$.

If $i$ is odd, then $f(i) = 1$.

If $i = 2 + 4j$ where $j$ is an integer, then $f(i) = 2$.

If $i = 4 + 8j$ where $j$ is an integer, then $f(i) = 3$.

If $i = 8 + 16j$ where $j$ is an integer, then $f(i) = 4$.

If $i = 16 + 32j$ where $j$ is an integer, then $f(i) = 5$.

Etc.

How do I define $f(i)$ in a compact, easy to understand equation?


I have seen the notation $\operatorname {ord_p}(n)$ for the exponent of a prime $p$ in the prime factorization of $n$. Your sequence is then $x_n=\operatorname {ord_2}(n)+1$. I don't know how common it is.


This is a A001511 sequence

a(n) - 1 is the number of trailing zeros in the binary expansion of n.

If you are counting in binary and the least significant bit is numbered 1, the next bit is 2, etc., a(n) is the bit that is incremented when increasing from n-1 to n. - Jud McCranie, Apr 26 2004