Sequence/List Formula Consider the following sequence $\{1,1,2,1,2,3,4,1,2,3,4,5,6,7,8,1,...,16,1,...,32,1...\}$; it is the list of integers $1$ to $2^0$, then $1$ to $2^1$, then $1$ to $2^2$, etc. Let $f(n)$ return the $n$th entry in the sequence.
What is a closed form for $f(n)$? Currently I've just been building the sequence as a list and using the $n$th entry... but an analytical expression would be quite helpful. So far what I have is:
\begin{equation}
f(n)=j\quad \text{ if }\quad n=2^{m+\lceil \log_2 j\rceil}+j-1
\end{equation}
where $m$ is a positive integer or $0$. It's a little small but we're taking the ceiling of $\log_2 j$ in the above equation. This question has been bothering me for a while because it seems like I should've gotten this immediately... but I couldn't do it!
 A: $$f(n)=n-2^{\lfloor log_2 n \rfloor}+1$$
where the sequence starts $a_1=1, a_2=1,a_3=2, a_4=1, \dots$

 Add $\{0,1,1,3,3,3,3,7,7,7,\dots\}$ to your existing sequence.

A: Analytical, With the final value of in this case $2^{d-1}$ at $f(2^d-1)$
$$f(n)=\sum_{d=1}^{\infty}\bigg( \bigg( \sum_{m=1}^{d-1}2^{d-m-1}\sum_{j=2^{d-1}-2^{d-m}}^{2^{d-1}-2^{d-m-1}} \frac{1}{2^d}\sum_{k=0}^{2^d-1} e^{\frac{2 i \pi k (n+j)}{2^d}}\bigg) + \sum_{k=0}^{2^d-1}e^{\frac{2 i \pi k (n+2^{d-1})}{2^d}}\bigg)$$
For practical purpose you can stop after 2^d>=n ofcourse.
How to create repeated integers see also:
Explicit sum formula for repeated integers
A: In case you need this for a purely coding purpose, perhaps you would benefit from re-defining your function $f(n)$ as "the sum of digits in the binary representation of $n$"; more technically,
$$ f(n) = \sum_i^{\infty} (n\ \mathrm{mod}\ 2^{i+1})\ \mathrm{div}\ 2^i, $$
or
$$ f(n) = \sum_i^{\lfloor n \rfloor} (n\ \mathrm{mod}\ 2^{i+1})\ \mathrm{div}\ 2^i. $$
Here $a\ \mathrm{div}\ b$ is the integer quotient of $a$ and $b$, i.e. $b \cdot \lfloor a/b \rfloor$, and $a\ \mathrm{mod}\ b$ is the remainder on division of $a$ by $b$, i.e. $(a - b \cdot \lfloor a/b \rfloor)$.
