Unexplanied pattern from increasing rational sequences

I've stummbled upon some strange pattern when working with series of rational numbers. If anyone could shed light on this phenomenon I will be most greatful.

Backgroud: Working in an integer lattice I take a beam, which starts from $(0,0)$ and coincides with the positive side of the x-axis. In addition I draw a rectangle of size $M \times N$, whose lower left corner is: $(0,0)$. I then proceed to increase the slope of the beam until it reaches another lattice point in (or on the border of) the rectangle. I continue this process, saving the list of points I encountered, until the beam meets the y-axis.

What the above essentially amounts to is finding the ordered set of values $(x,y)$, such that:$$gcd(x,y) = 1,\,\,\,\,1\le x\le M,\,\,\,\,1\le y\le N$$ where the order is determined by the value of $y\over{x}$.

The Pattern: I've written a small program to calculate these $(x,y)$ lists for rectangles of various sizes, and I was particularily interested in the value of $y$, more specifically, interested in when will $y$ first reach some given value. Looking at the values I noticed a pattern. Obviously, before the first time $y$ will reach the value 2, it will always have the value 1. However, once it reaches the value 2, it has the pattern: $$...,2,1,2,1,2,1,2,1,...$$i.e. No $2,2$ or $1,1$ at any point. This seems to hold regardless of the size of the rectangle. In addition, Once it reaches the value 3 it has the following pattern: $$...,3,2,3,1,3,2,3,1,3,2,3,1,...$$ This too seems to hold regardless of the size of the rectangle. The Next few patterns are: $$4,3,2,3,4,1$$ $$5,4,3,5,2,5,3,4,5,1$$ $$6,5,4,3,5,2,5,3,4,5,6,1$$ $$7,6,5,4,7,3,5,7,2,7,5,3,7,4,5,6,7,1$$ $$8,7,6,5,4,7,3,8,5,7,2,7,5,8,3,7,4,5,6,7,8,1$$ $$9,8,7,6,5,9,4,7,3,8,5,7,9,2,9,7,5,8,3,7,4,9,5,6,7,8,9,1$$

Note that the sequences may have different starting points for different rectangles, but once started, they maintain the (circular) order of the above patterns. I can't find any explanation for these pattens. Any help will be appreciated.

• You may want to check out the literature on "Farey Series". – Gerry Myerson Jun 25 '14 at 11:30
• @GerryMyerson Fascinating.. Thanks! – SomeStrangeUser Jun 25 '14 at 12:33