Command:
$ cat farey.py
Result:
def farey( n, asc=True ):
if asc:
a, b, c, d = 0, 1, 1 , n
else:
a, b, c, d = 1, 1, n-1, n
print "%d/%d" % (a,b)
while (asc and c <= n) or (not asc and a > 0):
k = int((n + b)/d)
a, b, c, d = c, d, k*c - a, k*d - b
print "%d/%d" % (a,b)
farey(8)
Command:
$ python farey.py
Result:
0/1
1/8
1/7
1/6
1/5
1/4
2/7
1/3
3/8
2/5
3/7
1/2
4/7
3/5
5/8
2/3
5/7
3/4
4/5
5/6
6/7
7/8
1/1