Constructing a deterministic one way infinite single tape Turing machine If I have an input string that is only composed of $a$'s and $b$'s, how can I construct a Turing machine that only accepts strings where the number of $b$'s divides the number of $a$'s?

For example:

$aaaabb$ is accepted

$baaab$ is rejected
Here is a rather reasonable way to implement this in Turing machines:


Sort the bs and the as so that the input tape is composed of bs followed by as.
Repeatedly perform the following operation: remove $B$ as from the end of the input tape, where $B$ is the number of bs.
Accept if you never got stuck perform an iteration of the previous step.


The second step can be implemented without explicitly counting $B$. For the first step, you can implement the simplest algorithm you can think of, say bubble sort.