Start at the tail of each bug. The strands represent the numbers from 1 to n. Multiply each of these numbers by 2 and take the results mod n, the result tells a new position the strand will move to. If the result is 0, instead of moving to a new place a circle is drawn and the strand terminates. The next step along the bug is the result of multiplication by 3, then 4, 5, and so on, continuing until all strands terminate.
This artwork shows bugs for the numbers 3 to 11. Prime numbers have maximum length.