CS 405 Algorithms: Farey

The Farey Fn is the (finite) sequence of rational numbers from the closed unit interval [0, 1] that have representations with denominator less than or equal to n, in ascending order. Fn is sometimes called the Farey Series even though it is not a series.

Thus, for example, F3 is 0 1 1 2 1 , , , , . 1 3 2 3 1

FareySequence is pseudo-code for a Brute Force algorithm to generate Fn. The first part of the code creates all possible , using the auxillary Euclidean algorithm program GCD to reject anything not in lowest terms. The construction is completed by calling a sorting algorithm FracSort to put the fractions in proper order. FracSort knows that [a, b] represents a larger than [c, d] when ad − bc is positive, and sorts the array according to this ordering of the pairs. A critical fact needed to use an array is that Fn contains fewer than n2 fractions.

Algorithm FareySequence(n) Integer k, a, b; Array A[0..n2] // declare variables A[0] ← [0, 1]; A[1] ← [1, 1]; // initialize variables k ← 2 // Current number of fractions for b ← 1 to n do for a ← 1 to b − 1 do if GCD(a, b) = 1 //check for lowest terms then A[k] ← [a, b]; // add to array A k ← k + 1; fi endfor endfor FracSort(A[0..k − 1]) // Sorting routine puts things in order return A[0..k − 1] end

In order to develop more efficient algorithms for generating Farey sequences we need more insight into the nature of these objects. Toward that end we make some observations. Fractions which are neighboring terms of a are known as a Farey pair. a c a c If b and d are a Farey pair, both in lowest terms with b > d , then their difference is a c ad−bc 1 b − d = bd = bd . Moreover, any two fractions with such a difference are both reduced a to lowest terms and neighbors in all Fn with n < b + d. For n = b + d the mediant of b c a+c and d , namely the fraction b+d , is reduced and falls between the two fractions, so it slips into Fn. These facts can be exploited to generate Farey sequences, either recursively or

1 iteratively. For example, using F3 above we can easily compute F4 by adding those mediants with denominator 4, obtaining 0 1 1 1 2 3 1 , , , , , , ; 1 4 3 2 3 4 1

Doing this again yields F5, which is 0 1 1 1 2 1 3 2 3 4 1 , , , , , , , , , , . 1 5 4 3 5 2 5 3 4 5 1

In fact the middle of any three consecutive terms is the of its neighbors even though 1 1 2 1 it might need to be reduced (between 5 and 3 we find 8 = 4 ). This offers one hope that it may be possible to generate the terms of Fn in order since we konow the first two terms are 0 1 1 and n . For n ≥ 2 the sequence of denominators is symmetrical, has odd length, and has a 2 in 1 the middle position (since 2 is the middle term). Can you give a recurrence relation for the length (number of terms) of Fn? At the most Fn has n − 1 more fractions than Fn−1 (and this happens only when n is prime). How about a (big Oh) bound on the growth of the length of Fn?(Hint: The recurrence involves the Euler phi function φ(n).)

2