Template:Short description

File:Farey diagram horizontal arc 9.svg
Farey diagram to F9 represented with circular arcs. In the SVG image, hover over a curve to highlight it and its terms.

Template:Multiple image In mathematics, the Farey sequence of order n is the sequence of completely reduced fractions, either between 0 and 1, or without this restriction,Template:Efn which 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 Template:Sfrac, and ends with the value 1, denoted by the fraction Template:Sfrac (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.<ref>Template:Cite book</ref>

ExamplesEdit

The Farey sequences of orders 1 to 8 are :

F1 = { Template:Sfrac, Template:Sfrac }
F2 = { Template:Sfrac, Template:Sfrac, Template:Sfrac }
F3 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
F4 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
F5 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
F6 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
F7 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
F8 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
Centered
F1 = { Template:Sfrac, Template:Sfrac }
F2 = { Template:Sfrac, Template:Sfrac, Template:Sfrac }
F3 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
F4 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
F5 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
F6 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
F7 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
F8 = { Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac, Template:Sfrac }
Sorted
 F1 = {0/1,                                                                                                          1/1}
 F2 = {0/1,                                                   1/2,                                                   1/1}
 F3 = {0/1,                               1/3,                1/2,                2/3,                               1/1}
 F4 = {0/1,                     1/4,      1/3,                1/2,                2/3,      3/4,                     1/1}
 F5 = {0/1,                1/5, 1/4,      1/3,      2/5,      1/2,      3/5,      2/3,      3/4, 4/5,                1/1}
 F6 = {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}
 F7 = {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}
 F8 = {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 sunburstEdit

File:Sunburst 8.png
Plotting F6 numerators vs denominators
File:Farey sunbursts 1-10.svg
Starbursts of iterations 1–10 superimposed

Plotting the numerators versus the denominators of a Farey sequence gives a shape like the one to the right, shown for Template:Math

Reflecting this shape around the diagonal and main axes generates the Farey sunburst, shown below. The Farey sunburst of order Template:Mvar connects the visible integer grid points from the origin in the square of side Template:Math, centered at the origin. Using Pick's theorem, the area of the sunburst is Template:Math, where Template:Math is the [[#Sequence_length_and_index_of_a_fraction|number of fractions in Template:Math]].

File:Farey sunburst 6.svg
Farey sunburst of order 6, with 1 interior (red) and 96 boundary (green) points giving an area of Template:Math according to Pick's theorem

HistoryEdit

The history of 'Farey series' is very curious — Hardy & Wright (1979)<ref>Template:Cite book</ref>
... 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)<ref name=Beiler>Template:Cite book Cited in {{#invoke:citation/CS1|citation

|CitationClass=web }}</ref>

Farey sequences are named after the British geologist John Farey, Sr., whose letter about these sequences was published in the Philosophical Magazine in 1816.<ref>Template:Citation</ref> 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.<ref name=Beiler/> Thus it was a historical accident that linked Farey's name with these sequences. This is an example of Stigler's law of eponymy.

PropertiesEdit

Sequence length and index of a fractionEdit

The Farey sequence of order Template:Mvar contains all of the members of the Farey sequences of lower orders. In particular Template:Mvar contains all of the members of Template:Math and also contains an additional fraction for each number that is less than Template:Mvar and coprime to Template:Mvar. Thus Template:Math consists of Template:Math together with the fractions Template:Sfrac and Template:Sfrac.

The middle term of a Farey sequence Template:Mvar is always Template:Sfrac, for Template:Math. From this, we can relate the lengths of Template:Mvar and Template:Math using Euler's totient function Template:Math:

<math display=block>|F_n| = |F_{n-1}| + \varphi(n).</math>

Using the fact that Template:Math, we can derive an expression for the length of Template:Mvar:<ref>Template:Cite OEIS</ref>

<math display=block>|F_n| = 1 + \sum_{m=1}^n \varphi(m) = 1 + \Phi(n),</math> where Template:Math is the summatory totient.

We also have : <math display=block>|F_n| = \frac{1}{2}\left(3+\sum_{d=1}^n \mu(d) \left\lfloor \tfrac{n}{d} \right\rfloor^2 \right),</math> and by a Möbius inversion formula : <math display=block>|F_n| = \frac{1}{2} (n+3)n - \sum_{d=2}^n|F_{\lfloor n/d \rfloor}|,</math> where Template:Math is the number-theoretic Möbius function, and <math>\lfloor n/d \rfloor </math> is the floor function.

The asymptotic behaviour of Template:Math is : <math display=block>|F_n| \sim \frac {3n^2}{\pi^2}.</math>

The number of Farey fractions with denominators equal to Template:Mvar in Template:Mvar is given by Template:Math when Template:Math and zero otherwise. Concerning the numerators one can define the function <math>\mathcal{N}_n(h)</math> that returns the number of Farey fractions with numerators equal to Template:Mvar in Template:Mvar. This function has some interesting properties as<ref name=Tomas2024>Template:Cite journal</ref>

<math>\mathcal{N}_n(1)=n</math>,
<math>\mathcal{N}_n(p^m)=\left\lceil(n-p^m) \left(1- 1/p \right)\right\rceil</math> for any prime number <math>p</math>,
<math>\mathcal{N}_{n+mh}(h)=\mathcal{N}_{n}(h) + m\varphi(h)</math> for any integer Template:Math,
<math>\mathcal{N}_{n}(4h)=\mathcal{N}_{n}(2h) - \varphi(2h).</math>

In particular, the property in the third line above implies <math>\mathcal{N}_{mh}(h)=(m-1)\varphi(h)</math> and, further, <math>\mathcal{N}_{2h}(h)=\varphi(h).</math> The latter means that, for Farey sequences of even order Template:Mvar, the number of fractions with numerators equal to Template:Math is the same as the number of fractions with denominators equal to Template:Math, that is <math>\mathcal{N}_{n}(n/2) = \varphi(n/2)</math>.

The index <math>I_n(a_{k,n}) = k</math> of a fraction <math>a_{k,n}</math> in the Farey sequence <math>F_n=\{a_{k,n} : k = 0, 1, \ldots, m_n\}</math> is simply the position that <math>a_{k,n}</math> 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: <math display=block>\begin{align}

 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{align}</math>

The index of Template:Math where Template:Math and Template:Mvar is the least common multiple of the first Template:Mvar numbers, Template:Math, is given by:<ref name=Tomas2018>Template:Cite journal</ref> <math display=block>I_n(1/k) = 1 + n \sum_{j=1}^{i} \frac{\varphi(j)}{j} - k\Phi(i).</math>

A similar expression was used as an approximation of <math>I_n(x)</math> for low values of <math>x</math> in the classical paper by F. Dress.<ref name=Dress1999>Template:Cite journal</ref> A general expression for <math>I_n(h/k)</math> for any Farey fraction <math>h/k</math> is given in.<ref name=Tomas2025>Template:Cite journal</ref>

Farey neighboursEdit

Fractions which are neighbouring terms in any Farey sequence are known as a Farey pair and have the following properties.

If Template:Math and Template:Math are neighbours in a Farey sequence, with Template:Math, then their difference Template:Math is equal to Template:Math. Since <math display=block>\frac{c}{d} - \frac{a}{b} = \frac{bc - ad}{bd},</math>

this is equivalent to saying that <math display=block>bc - ad = 1.</math>

Thus Template:Sfrac and Template:Sfrac are neighbours in Template:Math, and their difference is Template:Sfrac.

The converse is also true. If <math display=block>bc - ad = 1</math>

for positive integers Template:Math with Template:Math and Template:Math, then Template:Math and Template:Math will be neighbours in the Farey sequence of order Template:Math.

If Template:Math has neighbours Template:Math and Template:Math in some Farey sequence, with Template:Math, then Template:Math is the mediant of Template:Math and Template:Math – in other words, <math display=block>\frac{p}{q} = \frac{a + c}{b + d}.</math>

This follows easily from the previous property, since if <math display=block>\begin{align}

 && 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{align}</math>

It follows that if Template:Math and Template:Math are neighbours in a Farey sequence then the first term that appears between them as the order of the Farey sequence is incremented is <math display=block>\frac{a+c}{b+d},</math>

which first appears in the Farey sequence of order Template:Math.

Thus the first term to appear between Template:Sfrac and Template:Sfrac is Template:Sfrac, which appears in Template:Math.

The total number of Farey neighbour pairs in Template:Mvar is Template:Math.

The Stern–Brocot tree is a data structure showing how the sequence is built up from 0 Template:Pars and 1 Template:Pars, by taking successive mediants.

Equivalent-area interpretationEdit

Every consecutive pair of Farey rationals have an equivalent area of 1.<ref name=Austin2008>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> See this by interpreting consecutive rationals <math display=block>r_1 = \frac{p}{q} \qquad r_2 = \frac{p'}{q'}</math> as vectors Template:Math in the xy-plane. The area is given by <math display=block>A \left(\frac{p}{q}, \frac{p'}{q'} \right) = qp' - q'p.</math> As any added fraction in between two previous consecutive Farey sequence fractions is calculated as the mediant (⊕), then <math display=block>\begin{align}

 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{align}</math> (since Template:Math and Template:Math, its area must be 1).

Farey neighbours and continued fractionsEdit

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 Template:Math, which first appears in Farey sequence Template:Mvar, has the continued fraction expansions <math display=block>\begin{align}

 &[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{align}</math>

then the nearest neighbour of Template:Math in Template:Mvar (which will be its neighbour with the larger denominator) has a continued fraction expansion <math display=block>[0;\ a_1,\ a_2,\ \ldots,\ a_n]</math>

and its other neighbour has a continued fraction expansion <math display=block>[0;\ a_1,\ a_2,\ \ldots,\ a_{n-1}]</math>

For example, Template:Sfrac has the two continued fraction expansions Template:Math and Template:Math, and its neighbours in Template:Math are Template:Sfrac, which can be expanded as Template:Math; and Template:Sfrac, which can be expanded as Template:Math.

Farey fractions and the least common multipleEdit

The lcm can be expressed as the products of Farey fractions as <math display=block> \text{lcm}[1,2,...,N] = e^{\psi(N)} = \frac{1}{2} \left( \prod_{r \in F_N, 0<r \le 1/2} 2 \sin(\pi r) \right)^2 </math>

where Template:Math is the second Chebyshev function.<ref>Template:Cite arXiv</ref><ref>Template:Cite arXiv</ref>

Farey fractions and the greatest common divisorEdit

Since the Euler's totient function is directly connected to the gcd so is the number of elements in Template:Mvar, <math display=block>|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}} .</math>

For any 3 Farey fractions Template:Math the following identity between the gcd's of the 2x2 matrix determinants in absolute value holds:<ref name=TomasGarcia2020>Template:Cite journal</ref> <math display=block>

 \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)

</math>

<ref name=Tomas2018>Template:Cite journal</ref>

ApplicationsEdit

Farey sequences are very useful to find rational approximations of irrational numbers.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> For example, the construction by Eliahou<ref>Template:Cite journal</ref> 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 Template:Math.

In physical systems with resonance phenomena, Farey sequences provide a very elegant and efficient method to compute resonance locations in 1D<ref>Template:Cite journal</ref> and 2D.<ref>Template:Cite journal</ref>

Farey sequences are prominent in studies of any-angle path planning on square-celled grids, for example in characterizing their computational complexity<ref>Template:Cite journal</ref> or optimality.<ref>Template:Cite journal</ref> The connection can be considered in terms of Template:Mvar-constrained paths, namely paths made up of line segments that each traverse at most Template:Mvar rows and at most Template:Mvar columns of cells. Let Template:Mvar be the set of vectors Template:Math such that <math>1 \leq q \leq r</math>, <math>0 \leq p \leq q</math>, and Template:Mvar, Template:Mvar are coprime. Let Template:Mvar be the result of reflecting Template:Mvar in the line Template:Math. Let <math>S = \{ (\pm x, \pm y) : (x, y) \in Q \cup Q* \}</math>. Then any Template:Mvar-constrained path can be described as a sequence of vectors from Template:Mvar. There is a bijection between Template:Mvar and the Farey sequence of order Template:Mvar given by Template:Math mapping to <math>\tfrac{p}{q}</math>.

Ford circlesEdit

File:Comparison Ford circles Farey diagram.svg
Comparison of Ford circles and a Farey diagram with circular arcs for n from 1 to 9. Each arc intersects its corresponding circles at right angles. In the SVG image, hover over a circle or curve to highlight it and its terms.

There is a connection between Farey sequence and Ford circles.

For every fraction Template:Math (in its lowest terms) there is a Ford circle Template:Math, which is the circle with radius <math>\tfrac{1}{2q^2}</math> and centre at <math>\bigl(\tfrac{p}{q}, \tfrac{1}{2q^2}\bigr).</math> Two Ford circles for different fractions are either disjoint or they are tangent to one another—two Ford circles never intersect. If Template:Math then the Ford circles that are tangent to Template:Math are precisely the Ford circles for fractions that are neighbours of Template:Math in some Farey sequence.

Thus Template:Math is tangent to Template:Math, Template:Math, Template:Math, Template:Math, etc.

Ford circles appear also in the Apollonian gasket Template:Math. The picture below illustrates this together with Farey resonance lines.<ref name=Tomas2020>Template:Cite arXiv</ref>

Riemann hypothesisEdit

Farey sequences are used in two equivalent formulations of the Riemann hypothesis. Suppose the terms of Template:Mvar are <math>\{a_{k,n} : k = 0, 1, \ldots, m_n\}.</math> Define <math>d_{k,n} = a_{k,n} - \tfrac{k}{m_n},</math> in other words <math>d_{k,n}</math> is the difference between the Template:Mvarth term of the Template:Mvarth Farey sequence, and the Template:Mvarth member of a set of the same number of points, distributed evenly on the unit interval. In 1924 Jérôme Franel<ref>Template:Cite journal</ref> proved that the statement

<math display=block>\sum_{k=1}^{m_n} d_{k,n}^2 = O (n^r) \quad \forall r > -1</math>

is equivalent to the Riemann hypothesis, and then Edmund Landau<ref>Template:Cite journal</ref> remarked (just after Franel's paper) that the statement <math display=block>\sum_{k=1}^{m_n} |d_{k,n}| = O (n^r) \quad \forall r > \frac{1}{2}</math>

is also equivalent to the Riemann hypothesis.

Other sums involving Farey fractionsEdit

The sum of all Farey fractions of order Template:Mvar is half the number of elements: <math display=block>\sum_{r\in F_n} r = \frac{1}{2} |F_n| .</math>

The sum of the denominators in the Farey sequence is twice the sum of the numerators and relates to Euler's totient function:

<math display=block>\sum_{a/b \in F_n} b = 2 \sum_{a/b \in F_n} a = 1 + \sum_{i=1}^{n} i\varphi(i) , </math>

which was conjectured by Harold L. Aaron in 1962 and demonstrated by Jean A. Blake in 1966.<ref>Template:Cite journal</ref> A one line proof of the Harold L. Aaron conjecture is as follows. The sum of the numerators is <math display=block>1 + \sum_{2 \le b \le n} \ \sum_{(a,b)=1} a = 1 + \sum_{2 \le b \le n} b\frac{\varphi(b)}{2}.</math> The sum of denominators is <math display=block>2 + \sum_{2 \le b \le n} \ \sum_{(a,b)=1} b = 2 + \sum_{2 \le b \le n} b\varphi(b).</math> The quotient of the first sum by the second sum is Template:Sfrac.

Let Template:Mvar be the ordered denominators of Template:Mvar, then:<ref>Template:Cite journal</ref>

<math display=block>\sum_{j=0}^{|F_n|-1} \frac{b_j}{b_{j+1}} = \frac{3|F_n|-4}{2} </math> and <math display=block>\sum_{j=0}^{|F_n|-1} \frac{1}{b_{j+1}b_{j}} = 1.</math>

Let <math>\tfrac{a_j}{b_j}</math> the Template:Mvarth Farey fraction in Template:Mvar, then <math display=block>

 \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, </math>

which is demonstrated in.<ref>Template:Cite journal </ref> Also according to this reference the term inside the sum can be expressed in many different ways: <math display=block> 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, </math>

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

<math display=block>\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 , </math>

The Mertens function can be expressed as a sum over Farey fractions as <math display=block>M(n)= -1+ \sum_{a\in \mathcal{F}_n} e^{2\pi i a}</math> where <math> \mathcal{F}_n</math> is the Farey sequence of order Template:Mvar.

This formula is used in the proof of the Franel–Landau theorem.<ref>Template:Cite book</ref>

Next termEdit

A surprisingly simple algorithm exists to generate the terms of Fn 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 Template:Sfrac and Template:Sfrac are the two given entries, and Template:Sfrac is the unknown next entry, then Template:Math. Since Template:Sfrac is in lowest terms, there must be an integer k such that Template:Math and Template:Math, giving Template:Math and Template:Math. If we consider p and q to be functions of k, then

<math> \frac{p(k)}{q(k)}- \frac{c}{d} = \frac{cb - da}{d(kd - b)}</math>

so the larger k gets, the closer Template:Sfrac gets to Template:Sfrac.

To give the next term in the sequence k must be as large as possible, subject to Template:Math (as we are only considering numbers with denominators not greater than n), so k is the greatest Template:Nowrap. Putting this value of k back into the equations for p and q gives

<math> p = \left\lfloor\frac{n+b}{d}\right\rfloor c - a</math>
<math> q = \left\lfloor\frac{n+b}{d}\right\rfloor d - b</math>

This is implemented in Python as follows: <syntaxhighlight lang="python"> from fractions import Fraction from collections.abc import Generator

def farey_sequence(n: int, descending: bool = False) -> Generator[Fraction]:

   """
   Print the n'th Farey sequence. Allow for either ascending or descending.
   >>> print(*farey_sequence(5), sep=' ')
   0 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1
   """
   a, b, c, d = 0, 1, 1, n
   if descending:
       a, c = 1, n - 1
   yield Fraction(a, b)
   while 0 <= c <= n:
       k = (n + b) // d
       a, b, c, d = c, d, k * c - a, k * d - b
       yield Fraction(a, b)

if __name__ == "__main__":

   import doctest
   doctest.testmod()

</syntaxhighlight>

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.<ref>Template:Cite magazine</ref>

See alsoEdit

FootnotesEdit

Template:Notelist

ReferencesEdit

Template:Reflist

Further readingEdit

  • Template:Citation
  • Template:Cite book — in particular, see §4.5 (pp. 115–123), Bonus Problem 4.61 (pp. 150, 523–524), §4.9 (pp. 133–139), §9.3, Problem 9.3.6 (pp. 462–463).
  • {{#invoke:citation/CS1|citation

|CitationClass=web }} — reviews the isomorphisms of the Stern-Brocot Tree.

  • {{#invoke:citation/CS1|citation

|CitationClass=web }} — reviews connections between Farey Fractions and Fractals.

|CitationClass=web }} {{#invoke:citation/CS1|citation |CitationClass=web }}

External linksEdit

  • {{#invoke:citation/CS1|citation

|CitationClass=web }} Online copy of book

  • {{#invoke:citation/CS1|citation

|CitationClass=web }}

  • {{#invoke:citation/CS1|citation

|CitationClass=web }}

  • {{#invoke:citation/CS1|citation

|CitationClass=web }}

|_exclude=urlname, _debug, id |url = https://mathworld.wolfram.com/{{#if:Stern-BrocotTree%7CStern-BrocotTree.html}} |title = Stern-Brocot Tree |author = Weisstein, Eric W. |website = MathWorld |access-date = |ref = Template:SfnRef }}

Template:Authority control