The premise is this, you have two containers one is red and the other is blue. The red container has 1000 marbles, and the blue container is empty. You can easily split the marbles in any container in half (truncated if odd) and dump them into the other container, yet you want the red container to end up with 800 marbles, and the blue one to contain 200 marbles (5:1 ratio). However, since doing it manually (one by one) would be too time consuming you want to know how to achieve that ratio just transferring halves from one container to another back and forth.
The way I found to achieve this is by following the binary expansion of $\frac{1}{5}$. In practice it also worked for $\frac{1}{3}$, $\frac{1}{7}$, $\frac{1}{11}$, and even $\frac{1}{17}$.
$\frac{1}{3} = 0.\overline3 = 0.\overline{01}_2 \\ \frac{1}{5} = 0.2 = 0.\overline{0011}_2 \\ \frac{1}{7} = 0.\overline{142857} = 0.\overline{001}_2 \\ \frac{1}{11} = 0.\overline{09} = 0.\overline{0001011101}_2 \\ ...$
After the decimal point the 'zeroes' would mean pull halves from red to blue, and 'ones' would, likewise, mean pulling halves from blue to red. Doing this will eventually arrive to the ratio the number itself represents.
The following is the pattern the values of the containers in the previous problem would follow as you go moving splits back and forth eventually arriving to 800 in the red container and 200 in blue one, and continuing would give all the other multiples of 5. That is 800, 600, 400, and 200.
$$ \begin{array}{c|lcr} n & \text{Red} & \text{Blue} \\ \hline 1 & 1000 & 0 \\ 2 & 500 & 500 \\ 3 & 250 & 750 \\ 4 & 625 & 375 \\ 5 & 812 & 188 \\ 6 & 406 & 594 \\ 7 & 203 & 797 \\ 8 & 601 & 399 \\ 9 & 800 & 200 \\ 10 & 400 & 600 \\ 11 & 200 & 800 \\ \end{array} $$
However here is where my problem begins. As I said earlier I was able to work out this method for any number when I needed any ratio using the prime numbers 3, 5, 7, 11, and 17, but for some reason this DOES NOT work at all for 13. Moreover, that is not the only prime for which it doesn't work, it doesn't seem to work either for 19 and 23. It worked partially for 29.
It is also important to note that the method is also effective to approximate the desired ratio even if you work with fractions or an arbitrary number of decimal places.
So is there any theorem for this? If so, what is it called? Do anyone know of a working solution for this specific problem?
Edit: I wrote all fractions as 0.3