Is there a closed formula to the problem $f(1)=1, f(2n)=f(n), f(2n+1)=f(2n)+1$. So far i have found a solution for $n$, which is the number of power of $2$'s needed to add up to the number starting with the greatest power of $2$. Also $n=$ the number of $1$'s needed to represent $n$ in binary form. So for example $f(12)=2, 2^3 + 2^2 = 12$, $12$ in binary is represented as $1100$, two $1$'s so $n=2$. My problem is i know the process but can't find a simple formula to this problem.
|
|
There is no known closed form solution for the number of ones in the binary representation of a number, also known as the Hamming weight of the number. You may want to check out this related question on efficient ways to compute |
|||
|
|
|
It's obvious that
And for efficiency, the following code in C was given in chapter 5.1 of Hacker's Delight
EDIT: If you want a more mathematical representation, |
||||
|
|
$$f\left(\sum\limits_{n=0}^{+\infty}2^ix_i\right)=\sum\limits_{n=0}^{+\infty}x_i,\qquad x_i\in\{0,1\}. $$ |
|||
|
|
|
http://en.wikipedia.org/wiki/Hamming_distance there is not close formula, but the algorithm can be known review the works of Richard Hamming. |
||||
|
|
