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?
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}$