# Farey sequence

In mathematics, the Farey sequence of order n is the sequence of completely reduced fractions, either between 0 and 1, or without this restriction, which when in lowest terms have denominators less than or equal to n, arranged in order of increasing size.
With the restricted definition, each Farey sequence starts with the value 0, denoted by the fraction ⁠0/1⁠, and ends with the value 1, denoted by the fraction ⁠1/1⁠ (although some authors omit these terms).
A Farey sequence is sometimes called a Farey series, which is not strictly correct, because the terms are not summed.

## Examples

The Farey sequences of orders 1 to 8 are :
    F$_1$ = { ⁠0/1⁠$_,$ ⁠1/1⁠ }
    F$_2$ = { ⁠0/1⁠$_,$ ⁠1/2⁠$_,$ ⁠1/1⁠ }
    F$_3$ = { ⁠0/1⁠$_,$ ⁠1/3⁠$_,$ ⁠1/2⁠$_,$ ⁠2/3⁠$_,$ ⁠1/1⁠ }
    F$_4$ = { ⁠0/1⁠$_,$ ⁠1/4⁠$_,$ ⁠1/3⁠$_,$ ⁠1/2⁠$_,$ ⁠2/3⁠$_,$ ⁠3/4⁠$_,$ ⁠1/1⁠ }
    F$_5$ = { ⁠0/1⁠$_,$ ⁠1/5⁠$_,$ ⁠1/4⁠$_,$ ⁠1/3⁠$_,$ ⁠2/5⁠$_,$ ⁠1/2⁠$_,$ ⁠3/5⁠$_,$ ⁠2/3⁠$_,$ ⁠3/4⁠$_,$ ⁠4/5⁠$_,$ ⁠1/1⁠ }
    F$_6$ = { ⁠0/1⁠$_,$ ⁠1/6⁠$_,$ ⁠1/5⁠$_,$ ⁠1/4⁠$_,$ ⁠1/3⁠$_,$ ⁠2/5⁠$_,$ ⁠1/2⁠$_,$ ⁠3/5⁠$_,$ ⁠2/3⁠$_,$ ⁠3/4⁠$_,$ ⁠4/5⁠$_,$ ⁠5/6⁠$_,$ ⁠1/1⁠ }
    F$_7$ = { ⁠0/1⁠$_,$ ⁠1/7⁠$_,$ ⁠1/6⁠$_,$ ⁠1/5⁠$_,$ ⁠1/4⁠$_,$ ⁠2/7⁠$_,$ ⁠1/3⁠$_,$ ⁠2/5⁠$_,$ ⁠3/7⁠$_,$ ⁠1/2⁠$_,$ ⁠4/7⁠$_,$ ⁠3/5⁠$_,$ ⁠2/3⁠$_,$ ⁠5/7⁠$_,$ ⁠3/4⁠$_,$ ⁠4/5⁠$_,$ ⁠5/6⁠$_,$ ⁠6/7⁠$_,$ ⁠1/1⁠ }
    F$_8$ = { ⁠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⁠ }

### Farey sunburst

Plotting the numerators versus the denominators of a Farey sequence gives a shape like the one to the right, shown for F$_6$.
Reflecting this shape around the diagonal and main axes generates the Farey sunburst, shown below. The Farey sunburst of order n connects the visible integer grid points from the origin in the square of side 2n, centered at the origin. Using Pick's theorem, the area of the sunburst is 4(|F$_n$| − 1), where |F$_n$| is the number of fractions in F$_n$.

## History

    The history of 'Farey series' is very curious — Hardy & Wright (1979)
    ... once again the man whose name was given to a mathematical relation was not the original discoverer so far as the records go. — Beiler (1964)
Farey sequences are named after the British geologist John Farey, Sr., whose letter about these sequences was published in the Philosophical Magazine in 1816. Farey conjectured, without offering proof, that each new term in a Farey sequence expansion is the mediant of its neighbours. Farey's letter was read by Cauchy, who provided a proof in his Exercices de mathématique, and attributed this result to Farey. In fact, another mathematician, Charles Haros, had published similar results in 1802 which were not known either to Farey or to Cauchy. Thus it was a historical accident that linked Farey's name with these sequences. This is an example of Stigler's law of eponymy.

## Properties
### Sequence length and index of a fraction

The Farey sequence of order n contains all of the members of the Farey sequences of lower orders. In particular F$_n$ contains all of the members of F$_{n−1}$ and also contains an additional fraction for each number that is less than n and coprime to n. Thus F$_6$ consists of F$_5$ together with the fractions ⁠1/6⁠ and ⁠5/6⁠.
The middle term of a Farey sequence F$_n$ is always ⁠1/2⁠, for n > 1. From this, we can relate the lengths of F$_n$ and F$_{n−1}$ using Euler's totient function φ(n):
$|F_{n}|=|F_{n-1}|+\varphi (n).$
Using the fact that |F$_1$| = 2, we can derive an expression for the length of F$_n$:
$|F_{n}|=1+\sum _{m=1}^{n}\varphi (m)=1+\Phi (n),$ where Φ(n) is the summatory totient.
We also have : $|F_{n}|={\frac {1}{2}}\left(3+\sum _{d=1}^{n}\mu (d)\left\lfloor {\tfrac {n}{d}}\right\rfloor ^{2}\right),$ and by a Möbius inversion formula : $|F_{n}|={\frac {1}{2}}(n+3)n-\sum _{d=2}^{n}|F_{\lfloor n/d\rfloor }|,$ where μ(d) is the number-theoretic Möbius function, and $\lfloor n/d\rfloor$ is the floor function.
The asymptotic behaviour of |F$_n$| is : $|F_{n}|\sim {\frac {3n^{2}}{\pi ^{2}}}.$
The number of Farey fractions with denominators equal to k in F$_n$ is given by φ(k) when k ≤ n and zero otherwise. Concerning the numerators one can define the function ${\mathcal {N}}_{n}(h)$ that returns the number of Farey fractions with numerators equal to h in F$_n$. This function has some interesting properties as
    ${\mathcal {N}}_{n}(1)=n$,
    ${\mathcal {N}}_{n}(p^{m})=\left\lceil (n-p^{m})\left(1-1/p\right)\right\rceil$ for any prime number $p$,
    ${\mathcal {N}}_{n+mh}(h)={\mathcal {N}}_{n}(h)+m\varphi (h)$ for any integer m ≥ 0,
    ${\mathcal {N}}_{n}(4h)={\mathcal {N}}_{n}(2h)-\varphi (2h).$
In particular, the property in the third line above implies ${\mathcal {N}}_{mh}(h)=(m-1)\varphi (h)$ and, further, ${\mathcal {N}}_{2h}(h)=\varphi (h).$ The latter means that, for Farey sequences of even order n, the number of fractions with numerators equal to ⁠n/2⁠ is the same as the number of fractions with denominators equal to ⁠n/2⁠, that is ${\mathcal {N}}_{n}(n/2)=\varphi (n/2)$.
The index $I_{n}(a_{k,n})=k$ of a fraction $a_{k,n}$ in the Farey sequence $F_{n}=\{a_{k,n}:k=0,1,\ldots ,m_{n}\}$ is simply the position that $a_{k,n}$ occupies in the sequence. This is of special relevance as it is used in an alternative formulation of the Riemann hypothesis, see below. Various useful properties follow: ${\begin{aligned}I_{n}(0/1)&=0,\\[6pt]I_{n}(1/n)&=1,\\[2pt]I_{n}(1/2)&={\frac {|F_{n}|-1}{2}},\\[2pt]I_{n}(1/1)&=|F_{n}|-1,\\[2pt]I_{n}(h/k)&=|F_{n}|-1-I_{n}\left({\frac {k-h}{k}}\right).\end{aligned}}$
The index of ⁠1/k⁠ where ⁠n/i+1⁠ < k ≤ ⁠n/i⁠ and n is the least common multiple of the first i numbers, n = lcm([2, i]), is given by: $I_{n}(1/k)=1+n\sum _{j=1}^{i}{\frac {\varphi (j)}{j}}-k\Phi (i).$
A similar expression was used as an approximation of $I_{n}(x)$ for low values of $x$ in the classical paper by F. Dress. A general expression for $I_{n}(h/k)$ for any Farey fraction $h/k$ is given in.

### Farey neighbours

Fractions which are neighbouring terms in any Farey sequence are known as a Farey pair and have the following properties.
If ⁠a/b⁠ and ⁠c/d⁠ are neighbours in a Farey sequence, with ⁠a/b⁠ < ⁠c/d⁠, then their difference ⁠c/d⁠ − ⁠a/b⁠ is equal to ⁠1/bd⁠. Since ${\frac {c}{d}}-{\frac {a}{b}}={\frac {bc-ad}{bd}},$
this is equivalent to saying that $bc-ad=1.$
Thus ⁠1/3⁠ and ⁠2/5⁠ are neighbours in F$_5$, and their difference is ⁠1/15⁠.
The converse is also true. If $bc-ad=1$
for positive integers a, b, c, d with a < b and c < d, then ⁠a/b⁠ and ⁠c/d⁠ will be neighbours in the Farey sequence of order max(b,d).
If ⁠p/q⁠ has neighbours ⁠a/b⁠ and ⁠c/d⁠ in some Farey sequence, with ⁠a/b⁠ < ⁠p/q⁠ < ⁠c/d⁠, then ⁠p/q⁠ is the mediant of ⁠a/b⁠ and ⁠c/d⁠ – in other words, ${\frac {p}{q}}={\frac {a+c}{b+d}}.$
This follows easily from the previous property, since if ${\begin{aligned}&&bp-aq&=qc-pd=1,\\[4pt]\implies &&bp+pd&=qc+aq,\\[4pt]\implies &&p(b+d)&=q(a+c),\\\implies &&{\frac {p}{q}}&={\frac {a+c}{b+d}}.\end{aligned}}$
It follows that if ⁠a/b⁠ and ⁠c/d⁠ are neighbours in a Farey sequence then the first term that appears between them as the order of the Farey sequence is incremented is ${\frac {a+c}{b+d}},$
which first appears in the Farey sequence of order b + d.
Thus the first term to appear between ⁠1/3⁠ and ⁠2/5⁠ is ⁠3/8⁠, which appears in F$_8$.
The total number of Farey neighbour pairs in F$_n$ is 2|F$_n$| − 3.
The Stern–Brocot tree is a data structure showing how the sequence is built up from 0 (= ⁠0/1⁠) and 1 (= ⁠1/1⁠), by taking successive mediants.

#### Equivalent-area interpretation

Every consecutive pair of Farey rationals have an equivalent area of 1. See this by interpreting consecutive rationals $r_{1}={\frac {p}{q}}\qquad r_{2}={\frac {p'}{q'}}$ as vectors (p, q) in the xy-plane. The area is given by $A\left({\frac {p}{q}},{\frac {p'}{q'}}\right)=qp'-q'p.$ As any added fraction in between two previous consecutive Farey sequence fractions is calculated as the mediant (⊕), then ${\begin{aligned}A(r_{1},r_{1}\oplus r_{2})&=A(r_{1},r_{1})+A(r_{1},r_{2})\\&=A(r_{1},r_{2})\\&=1\end{aligned}}$ (since r$_1$ = ⁠1/0⁠ and r$_2$ = ⁠0/1⁠, its area must be 1).

### Farey neighbours and continued fractions

Fractions that appear as neighbours in a Farey sequence have closely related continued fraction expansions. Every fraction has two continued fraction expansions — in one the final term is 1; in the other the final term is greater by 1. If ⁠p/q⁠, which first appears in Farey sequence F$_q$, has the continued fraction expansions ${\begin{aligned}&[0;\ a_{1},\ a_{2},\ \ldots ,\ a_{n-1},\ a_{n},\ 1]\\&[0;\ a_{1},\ a_{2},\ \ldots ,\ a_{n-1},\ a_{n}+1]\end{aligned}}$
then the nearest neighbour of ⁠p/q⁠ in F$_q$ (which will be its neighbour with the larger denominator) has a continued fraction expansion $[0;\ a_{1},\ a_{2},\ \ldots ,\ a_{n}]$
and its other neighbour has a continued fraction expansion $[0;\ a_{1},\ a_{2},\ \ldots ,\ a_{n-1}]$
For example, ⁠3/8⁠ has the two continued fraction expansions [0; 2, 1, 1, 1] and [0; 2, 1, 2], and its neighbours in F$_8$ are ⁠2/5⁠, which can be expanded as [0; 2, 1, 1]; and ⁠1/3⁠, which can be expanded as [0; 2, 1].

### Farey fractions and the least common multiple

The lcm can be expressed as the products of Farey fractions as ${\text{lcm}}[1,2,...,N]=e^{\psi (N)}={\frac {1}{2}}\left(\prod _{r\in F_{N},0<r\leq 1/2}2\sin(\pi r)\right)^{2}$
where ψ(N) is the second Chebyshev function.

### Farey fractions and the greatest common divisor

Since the Euler's totient function is directly connected to the gcd so is the number of elements in F$_n$, $|F_{n}|=1+\sum _{m=1}^{n}\varphi (m)=1+\sum \limits _{m=1}^{n}\sum \limits _{k=1}^{m}\gcd(k,m)\cos {2\pi {\frac {k}{m}}}.$
For any 3 Farey fractions ⁠a/b⁠, ⁠c/d⁠, ⁠e/f⁠ the following identity between the gcd's of the 2x2 matrix determinants in absolute value holds: $\gcd \left({\begin{Vmatrix}a&c\\b&d\end{Vmatrix}},{\begin{Vmatrix}a&e\\b&f\end{Vmatrix}}\right)=\gcd \left({\begin{Vmatrix}a&c\\b&d\end{Vmatrix}},{\begin{Vmatrix}c&e\\d&f\end{Vmatrix}}\right)=\gcd \left({\begin{Vmatrix}a&e\\b&f\end{Vmatrix}},{\begin{Vmatrix}c&e\\d&f\end{Vmatrix}}\right)$

### Applications

Farey sequences are very useful to find rational approximations of irrational numbers. For example, the construction by Eliahou of a lower bound on the length of non-trivial cycles in the 3x+1 process uses Farey sequences to calculate a continued fraction expansion of the number log$_2$(3).
In physical systems with resonance phenomena, Farey sequences provide a very elegant and efficient method to compute resonance locations in 1D and 2D.
Farey sequences are prominent in studies of any-angle path planning on square-celled grids, for example in characterizing their computational complexity or optimality. The connection can be considered in terms of r-constrained paths, namely paths made up of line segments that each traverse at most r rows and at most r columns of cells. Let Q be the set of vectors (q, p) such that $1\leq q\leq r$, $0\leq p\leq q$, and p, q are coprime. Let Q* be the result of reflecting Q in the line y = x. Let $S=\{(\pm x,\pm y):(x,y)\in Q\cup Q*\}$. Then any r-constrained path can be described as a sequence of vectors from S. There is a bijection between Q and the Farey sequence of order r given by (q, p) mapping to ${\tfrac {p}{q}}$.

### Ford circles

There is a connection between Farey sequence and Ford circles.
For every fraction ⁠p/q⁠ (in its lowest terms) there is a Ford circle C[p/q], which is the circle with radius ${\tfrac {1}{2q^{2}}}$ and centre at ${\bigl{\tfrac {p}{q}},{\tfrac {1}{2q^{2}}}{\bigr )}.$ Two Ford circles for different fractions are either disjoint or they are tangent to one another—two Ford circles never intersect. If 0 < ⁠p/q⁠ < 1 then the Ford circles that are tangent to C[p/q] are precisely the Ford circles for fractions that are neighbours of ⁠p/q⁠ in some Farey sequence.
Thus C[2/5] is tangent to C[1/2], C[1/3], C[3/7], C[3/8], etc.
Ford circles appear also in the Apollonian gasket (0,0,1,1). The picture below illustrates this together with Farey resonance lines.

### Riemann hypothesis

Farey sequences are used in two equivalent formulations of the Riemann hypothesis. Suppose the terms of F$_n$ are $\{a_{k,n}:k=0,1,\ldots ,m_{n}\}.$ Define $d_{k,n}=a_{k,n}-{\tfrac {k}{m_{n}}},$ in other words $d_{k,n}$ is the difference between the kth term of the nth Farey sequence, and the kth member of a set of the same number of points, distributed evenly on the unit interval. In 1924 Jérôme Franel proved that the statement
$\sum _{k=1}^{m_{n}}d_{k,n}^{2}=O(n^{r})\quad \forall r>-1$
is equivalent to the Riemann hypothesis, and then Edmund Landau remarked (just after Franel's paper) that the statement $\sum _{k=1}^{m_{n}}|d_{k,n}|=O(n^{r})\quad \forall r>{\frac {1}{2}}$
is also equivalent to the Riemann hypothesis.

### Other sums involving Farey fractions

The sum of all Farey fractions of order n is half the number of elements: $\sum _{r\in F_{n}}r={\frac {1}{2}}|F_{n}|.$
The sum of the denominators in the Farey sequence is twice the sum of the numerators and relates to Euler's totient function:
$\sum _{a/b\in F_{n}}b=2\sum _{a/b\in F_{n}}a=1+\sum _{i=1}^{n}i\varphi (i),$
which was conjectured by Harold L. Aaron in 1962 and demonstrated by Jean A. Blake in 1966. A one line proof of the Harold L. Aaron conjecture is as follows. The sum of the numerators is $1+\sum _{2\leq b\leq n}\ \sum _{(a,b)=1}a=1+\sum _{2\leq b\leq n}b{\frac {\varphi (b)}{2}}.$ The sum of denominators is $2+\sum _{2\leq b\leq n}\ \sum _{(a,b)=1}b=2+\sum _{2\leq b\leq n}b\varphi (b).$ The quotient of the first sum by the second sum is ⁠1/2⁠.
Let b$_j$ be the ordered denominators of F$_n$, then:
$\sum _{j=0}^{|F_{n}|-1}{\frac {b_{j}}{b_{j+1}}}={\frac {3|F_{n}|-4}{2}}$ and $\sum _{j=0}^{|F_{n}|-1}{\frac {1}{b_{j+1}b_{j}}}=1.$
Let ${\tfrac {a_{j}}{b_{j}}}$ the jth Farey fraction in F$_n$, then $\sum _{j=1}^{|F_{n}|-1}(a_{j-1}b_{j+1}-a_{j+1}b_{j-1})=\sum _{j=1}^{|F_{n}|-1}{\begin{Vmatrix}a_{j-1}&a_{j+1}\\b_{j-1}&b_{j+1}\end{Vmatrix}}=3(|F_{n}|-1)-2n-1,$
which is demonstrated in. Also according to this reference the term inside the sum can be expressed in many different ways: $a_{j-1}b_{j+1}-a_{j+1}b_{j-1}={\frac {b_{j-1}+b_{j+1}}{b_{j}}}={\frac {a_{j-1}+a_{j+1}}{a_{j}}}=\left\lfloor {\frac {n+b_{j-1}}{b_{j}}}\right\rfloor ,$
obtaining thus many different sums over the Farey elements with same result. Using the symmetry around 1/2 the former sum can be limited to half of the sequence as
$\sum _{j=1}^{\left\lfloor {\frac {|F_{n}|}{2}}\right\rfloor }(a_{j-1}b_{j+1}-a_{j+1}b_{j-1})={\frac {3(|F_{n}|-1)}{2}}-n-\left\lceil {\frac {n}{2}}\right\rceil ,$
The Mertens function can be expressed as a sum over Farey fractions as $M(n)=-1+\sum _{a\in {\mathcal {F}}_{n}}e^{2\pi ia}$ where ${\mathcal {F}}_{n}$ is the Farey sequence of order n.
This formula is used in the proof of the Franel–Landau theorem.

## Next term

A surprisingly simple algorithm exists to generate the terms of F$_n$ in either traditional order (ascending) or non-traditional order (descending). The algorithm computes each successive entry in terms of the previous two entries using the mediant property given above. If ⁠a/b⁠ and ⁠c/d⁠ are the two given entries, and ⁠p/q⁠ is the unknown next entry, then ⁠c/d⁠ = ⁠a + p/b + q⁠. Since ⁠c/d⁠ is in lowest terms, there must be an integer k such that kc = a + p and kd = b + q, giving p = kc − a and q = kd − b. If we consider p and q to be functions of k, then
    ${\frac {p(k)}{q(k)}}-{\frac {c}{d}}={\frac {cb-da}{d(kd-b)}}$
so the larger k gets, the closer ⁠p/q⁠ gets to ⁠c/d⁠.
To give the next term in the sequence k must be as large as possible, subject to kd − b ≤ n (as we are only considering numbers with denominators not greater than n), so k is the greatest integer ≤ ⁠n + b/d⁠. Putting this value of k back into the equations for p and q gives
    $p=\left\lfloor {\frac {n+b}{d}}\right\rfloor c-a$
    $q=\left\lfloor {\frac {n+b}{d}}\right\rfloor d-b$
This is implemented in Python as follows:
Brute-force searches for solutions to Diophantine equations in rationals can often take advantage of the Farey series (to search only reduced forms). While this code uses the first two terms of the sequence to initialize a, b, c, and d, one could substitute any pair of adjacent terms in order to exclude those less than (or greater than) a particular threshold.