An easy way to define the sequence $0$, $1$, $0$, $\frac12$, $1$, $0$, $\frac13$, $\frac23$, $1$, $0$, $\frac14$, $\frac24$, $\frac34$, $1$, $\ldots$? Define $a_0=0$, $a_1=1$, $a_2=0$, $a_3=\frac 1 2$, $a_4=1$, $a_5=0$, $a_6=\frac 1 3$, $a_7=\frac 2 3$, $a_8=1$, $a_9=0$, $a_{10}=\frac 1 4 $, $a_{11}= \frac 2 4$, $a_{12}= \frac 3 4$, $a_{13}=1$ and so on.
How can we define it recursively or by a closed form?
 A: Recursion would not work, as this pattern does not necessarily depend on previous terms in a predictable fashion. You can actually do one better here, you can find $a_n$ in terms of $n$.
Now, for $n$, we need to find the largest $k$ such that
$$\frac{k(k+1)}{2} -1\leq n$$
$$\implies k^2 +k-2(n+1) \leq 0$$
$$k_{max} = \left[\frac{-1 + \sqrt{8n+9}}{2}\right]$$
Once you find $k_{max}$, you then start the sequence for the $(k+1)^{th}$ sequence, and hence for the remainder of the terms will just be dependant on $n-k_{max}$
A: There is a closed form solution which I found using the corresponding OEIS sequences for the numerators and denominators respectively. We have
$$a_n=\frac{(2n+\lfloor1/2+\sqrt{2n+3}\rfloor-\lfloor1/2+\sqrt{2n+3}\rfloor^2)/2+1}{\lfloor(\sqrt{8n+9}-1)/2\rfloor}$$
A: This is a famous sequence in equidistribution, and it is often divided in blocks, each of $k=1,\dots$ terms (usually the ones are not written)
The $n$-th ($n = 0, \dots, k-1$) term of the $k$-th block is simply
$$a_n^k = \frac{n}{k}$$
