Farey sequence: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Perl 6}}: work-around for bug in flattening lists > 2**16)
(replaced some italics (that wasn't rendering properly) with bold and italics highlighting, added other whitespace in the task's preamble.)
Line 1: Line 1:
{{draft task}}
{{draft task}}
The [[wp:Farey sequence|Farey sequence]] (sometimes incorrectly called a Farey series) <math>\textbf{\textit{F}}_n</math> of order <math>n</math> is the sequence of completely reduced fractions between <math>0</math> and <math>1</math> which, when in lowest terms, have denominators less than or equal to <math>n</math>, arranged in order of increasing size.


The &nbsp; [[wp:Farey sequence|Farey sequence]] &nbsp; ''' ''F''<sub>n</sub>''' &nbsp; of order &nbsp; '''n''' &nbsp; is the sequence of completely reduced fractions between &nbsp; '''0''' &nbsp; and &nbsp; '''1''' &nbsp; which, when in lowest terms, have denominators less than or equal to &nbsp; '''n''', &nbsp; arranged in order of increasing size.
Each Farey sequence starts with value <math>0</math>, denoted by the fraction <math> \frac{0}{1} </math> and ends with the value <math>1</math>, denoted by the fraction <math> \frac{1}{1}</math>.


The &nbsp; ''Farey sequence'' &nbsp; is sometimes incorrectly called a &nbsp; ''Farey series''.
The Farey sequences of orders <math>1</math> to <math>5</math> are:


Each Farey sequence:
:::* &nbsp; starts with the value &nbsp; '''0''', &nbsp; denoted by the fraction &nbsp; <math> \frac{0}{1} </math>
:::* &nbsp; ends with the value &nbsp; '''1''', &nbsp; denoted by the fraction &nbsp; <math> \frac{1}{1}</math>.


The Farey sequences of orders &nbsp; '''1''' &nbsp; to &nbsp; '''5''' &nbsp; are:

:::::: ''' ''F''<sub>1</sub>''' = <math> \frac{0}{1}, \frac{1}{1} </math>
: <br>
:::::: ''' ''F''<sub>2</sub>''' = <math> \frac{0}{1}, \frac{1}{2}, \frac{1}{1} </math>
: <br>
:::::: ''' ''F''<sub>3</sub>''' = <math> \frac{0}{1}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{1}{1} </math>
:::::: <br>
:::::: ''' ''F''<sub>4</sub>''' = <math> \frac{0}{1}, \frac{1}{4}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{3}{4}, \frac{1}{1} </math>
: <br>
:::::: ''' ''F''<sub>5</sub>''' = <math> \frac{0}{1}, \frac{1}{5}, \frac{1}{4}, \frac{1}{3}, \frac{2}{5}, \frac{1}{2}, \frac{3}{5}, \frac{2}{3}, \frac{3}{4}, \frac{4}{5}, \frac{1}{1} </math>


:<math>\textbf{\textit{F}}_1 = \frac{0}{1}, \frac{1}{1}</math>
:<br>
:<math>\textbf{\textit{F}}_2 = \frac{0}{1}, \frac{1}{2}, \frac{1}{1}</math>
:<br>
:<math>\textbf{\textit{F}}_3 = \frac{0}{1}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{1}{1}</math>
:<br>
:<math>\textbf{\textit{F}}_4 = \frac{0}{1}, \frac{1}{4}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{3}{4}, \frac{1}{1}</math>
:<br>
:<math>\textbf{\textit{F}}_5 = \frac{0}{1}, \frac{1}{5}, \frac{1}{4}, \frac{1}{3}, \frac{2}{5}, \frac{1}{2}, \frac{3}{5}, \frac{2}{3}, \frac{3}{4}, \frac{4}{5}, \frac{1}{1}</math>


;Task
;Task
* Compute and show the Farey sequence for orders <math>1</math> through <math>11</math> (inclusive).
* Compute and show the Farey sequence for orders &nbsp; '''1''' &nbsp; through &nbsp; '''11''' &nbsp; (inclusive).
* Compute and display the ''number'' of fractions in the Farey sequence for order <math>100</math> through <math>1000</math> (inclusive) by hundreds.
* Compute and display the &nbsp; ''number'' &nbsp; of fractions in the Farey sequence for order &nbsp; '''100''' &nbsp; through &nbsp; '''1,000''' &nbsp; (inclusive) &nbsp; by hundreds.


;See also
;See also
* Sequence [http://oeis.org/A006842 A006842 numerators of Farey series of order 1, 2, ···] on OEIS.
* Sequence [http://oeis.org/A006842 A006842 numerators of Farey series of order 1, 2, ···] on OEIS.
* Sequence [http://oeis.org/A006843 A006843 denominators of Farey series of order 1, 2, ···] on OEIS.
* Sequence [http://oeis.org/A006843 A006843 denominators of Farey series of order 1, 2, ···] on OEIS.
* Sequence [http://oeis.org/A005728 A005728 number of fractions in Farey series of order n.] on OEIS.
* Sequence [http://oeis.org/A005728 A005728 number of fractions in Farey series of order n.] on OEIS.
* Entry [http://mathworld.wolfram.com/FareySequence.html Farey sequence] on Mathworld.
* Entry [http://mathworld.wolfram.com/FareySequence.html Farey sequence] on MathWorld.
<br><br>


=={{header|C}}==
=={{header|C}}==

Revision as of 00:37, 22 May 2016

Farey sequence is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The   Farey sequence   Fn   of order   n   is the sequence of completely reduced fractions between   0   and   1   which, when in lowest terms, have denominators less than or equal to   n,   arranged in order of increasing size.

The   Farey sequence   is sometimes incorrectly called a   Farey series.


Each Farey sequence:

  •   starts with the value   0,   denoted by the fraction  
  •   ends with the value   1,   denoted by the fraction   .


The Farey sequences of orders   1   to   5   are:

F1 =

F2 =

F3 =

F4 =

F5 =


Task
  • Compute and show the Farey sequence for orders   1   through   11   (inclusive).
  • Compute and display the   number   of fractions in the Farey sequence for order   100   through   1,000   (inclusive)   by hundreds.


See also



C

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <string.h>

void farey(int n) { typedef struct { int d, n; } frac; frac f1 = {0, 1}, f2 = {1, n}, t; int k;

printf("%d/%d %d/%d", 0, 1, 1, n); while (f2.n > 1) { k = (n + f1.n) / f2.n; t = f1, f1 = f2, f2 = (frac) { f2.d * k - t.d, f2.n * k - t.n }; printf(" %d/%d", f2.d, f2.n); }

putchar('\n'); }

typedef unsigned long long ull; ull *cache; size_t ccap;

ull farey_len(int n) { if (n >= ccap) { size_t old = ccap; if (!ccap) ccap = 16; while (ccap <= n) ccap *= 2; cache = realloc(cache, sizeof(ull) * ccap); memset(cache + old, 0, sizeof(ull) * (ccap - old)); } else if (cache[n]) return cache[n];

ull len = (ull)n*(n + 3) / 2; int p, q = 0; for (p = 2; p <= n; p = q) { q = n/(n/p) + 1; len -= farey_len(n/p) * (q - p); }

cache[n] = len; return len; }

int main(void) { int n; for (n = 1; n <= 11; n++) { printf("%d: ", n); farey(n); }

for (n = 100; n <= 1000; n += 100) printf("%d: %llu items\n", n, farey_len(n));

n = 10000000; printf("\n%d: %llu items\n", n, farey_len(n)); return 0; }</lang>

Output:
1: 0/1 1/1
2: 0/1 1/2 1/1
3: 0/1 1/3 1/2 2/3 1/1
4: 0/1 1/4 1/3 1/2 2/3 3/4 1/1
5: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
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
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
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
9: 0/1 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1
10: 0/1 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1/1
11: 0/1 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1/1
100: 3045 items
200: 12233 items
300: 27399 items
400: 48679 items
500: 76117 items
600: 109501 items
700: 149019 items
800: 194751 items
900: 246327 items
1000: 304193 items

10000000: 30396356427243 items

D

This imports the module from the Arithmetic/Rational task. <lang d>import std.stdio, std.algorithm, std.range, arithmetic_rational;

auto farey(in int n) pure nothrow @safe {

   return rational(0, 1).only.chain(
           iota(1, n + 1)
           .map!(k => iota(1, k + 1).map!(m => rational(m, k)))
           .join.sort().uniq);

}

void main() @safe {

   writefln("Farey sequence for order 1 through 11:\n%(%s\n%)",
            iota(1, 12).map!farey);
   writeln("\nFarey sequence fractions, 100 to 1000 by hundreds:\n",
           iota(100, 1_001, 100).map!(i => i.farey.walkLength));

}</lang>

Output:
Farey sequence for order 1 through 11:
[0, 1]
[0, 1/2, 1]
[0, 1/3, 1/2, 2/3, 1]
[0, 1/4, 1/3, 1/2, 2/3, 3/4, 1]
[0, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1]
[0, 1/6, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 5/6, 1]
[0, 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]
[0, 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]
[0, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 1]
[0, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 3/10, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 7/10, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 9/10, 1]
[0, 1/11, 1/10, 1/9, 1/8, 1/7, 1/6, 2/11, 1/5, 2/9, 1/4, 3/11, 2/7, 3/10, 1/3, 4/11, 3/8, 2/5, 3/7, 4/9, 5/11, 1/2, 6/11, 5/9, 4/7, 3/5, 5/8, 7/11, 2/3, 7/10, 5/7, 8/11, 3/4, 7/9, 4/5, 9/11, 5/6, 6/7, 7/8, 8/9, 9/10, 10/11, 1]

Farey sequence fractions, 100 to 1000 by hundreds:
[3045, 12233, 27399, 48679, 76117, 109501, 149019, 194751, 246327, 304193]

Alternative Version

This is as fast as the C entry (total run-time is 0.20 seconds).

Translation of: C

<lang d>import core.stdc.stdio: printf, putchar;

void farey(in uint n) nothrow @nogc {

   static struct Frac { uint d, n; }
   Frac f1 = { 0, 1 }, f2 = { 1, n };
   printf("%u/%u %u/%u", 0, 1, 1, n);
   while (f2.n > 1) {
       immutable k = (n + f1.n) / f2.n;
       immutable aux = f1;
       f1 = f2;
       f2 = Frac(f2.d * k - aux.d, f2.n * k - aux.n);
       printf(" %u/%u", f2.d, f2.n);
   }
   putchar('\n');

}

ulong fareyLength(in uint n, ref ulong[] cache) pure nothrow @safe {

   if (n >= cache.length) {
       auto newLen = cache.length;
       if (newLen == 0)
           newLen = 16;
       while (newLen <= n)
           newLen *= 2;
       cache.length = newLen;
   } else if (cache[n])
       return cache[n];
   ulong len = ulong(n) * (n + 3) / 2;
   for (uint p = 2, q = 0; p <= n; p = q) {
       q = n / (n / p) + 1;
       len -= fareyLength(n / p, cache) * (q - p);
   }
   cache[n] = len;
   return len;

}

void main() nothrow {

   foreach (immutable uint n; 1 .. 12) {
       printf("%u: ", n);
       n.farey;
   }
   ulong[] cache;
   for (uint n = 100; n <= 1_000; n += 100)
       printf("%u: %llu items\n", n, fareyLength(n, cache));
   immutable uint n = 10_000_000;
   printf("\n%u: %llu items\n", n, fareyLength(n, cache));

}</lang>

Output:
1: 0/1 1/1
2: 0/1 1/2 1/1
3: 0/1 1/3 1/2 2/3 1/1
4: 0/1 1/4 1/3 1/2 2/3 3/4 1/1
5: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
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
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
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
9: 0/1 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1
10: 0/1 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1/1
11: 0/1 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1/1
100: 3045 items
200: 12233 items
300: 27399 items
400: 48679 items
500: 76117 items
600: 109501 items
700: 149019 items
800: 194751 items
900: 246327 items
1000: 304193 items

10000000: 30396356427243 items

EchoLisp

<lang scheme> (define distinct-divisors (compose make-set prime-factors))

euler totient
Φ
n / product(p_i) * product (p_i - 1)
# of divisors <= n

(define (Φ n) (let ((pdiv (distinct-divisors n))) (/ (* n (for/product ((p pdiv)) (1- p))) (for/product ((p pdiv)) p))))

farey-sequence length |Fn| = 1 + sigma (m=1..) Φ(m)

(define ( F-length n) (1+ (for/sum ((m (1+ n))) (Φ m))))

farey sequence
apply the definition
O(n^2)

(define (Farey N) (set! N (1+ N)) (make-set (for*/list ((n N) (d (in-range n N))) (rational n d))))

</lang>

Output:

<lang scheme> (for ((n (in-range 1 12))) ( printf "F(%d) %s" n (Farey n))) F(1) { 0 1 } F(2) { 0 1/2 1 } F(3) { 0 1/3 1/2 2/3 1 } F(4) { 0 1/4 1/3 1/2 2/3 3/4 1 } F(5) { 0 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1 } F(6) { 0 1/6 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 5/6 1 } F(7) { 0 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 } F(8) { 0 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 } F(9) { 0 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1 } F(10) { 0 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1 } F(11) { 0 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1 }

(for (( n (in-range 100 1100 100))) (printf "|F(%d)| = %d" n (F-length n))) |F(100)| = 3045 |F(200)| = 12233 |F(300)| = 27399 |F(400)| = 48679 |F(500)| = 76117 |F(600)| = 109501 |F(700)| = 149019 |F(800)| = 194751 |F(900)| = 246327 |F(1000)| = 304193

(for (( n '(10_000 100_000))) (printf "|F(%d)| = %d" n (F-length n))) |F(10000)| = 30397487 |F(100000)| = 3039650755 </lang>

FunL

Translation of Python code at [1]. <lang funl>def farey( n ) =

 res = seq()
 a, b, c, d = 0, 1, 1, n
 res += "$a/$b"
 
 while c <= n
   k = (n + b)\d
   a, b, c, d = c, d, k*c - a, k*d - b
   res += "$a/$b"

for i <- 1..11

 println( "$i: ${farey(i).mkString(', ')}" )

for i <- 100..1000 by 100

 println( "$i: ${farey(i).length()}" )</lang>
Output:
1: 0/1, 1/1
2: 0/1, 1/2, 1/1
3: 0/1, 1/3, 1/2, 2/3, 1/1
4: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1
5: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1
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
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
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
9: 0/1, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 1/1
10: 0/1, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 3/10, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 7/10, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 9/10, 1/1
11: 0/1, 1/11, 1/10, 1/9, 1/8, 1/7, 1/6, 2/11, 1/5, 2/9, 1/4, 3/11, 2/7, 3/10, 1/3, 4/11, 3/8, 2/5, 3/7, 4/9, 5/11, 1/2, 6/11, 5/9, 4/7, 3/5, 5/8, 7/11, 2/3, 7/10, 5/7, 8/11, 3/4, 7/9, 4/5, 9/11, 5/6, 6/7, 7/8, 8/9, 9/10, 10/11, 1/1
100: 3045
200: 12233
300: 27399
400: 48679
500: 76117
600: 109501
700: 149019
800: 194751
900: 246327
1000: 304193

Go

<lang go>package main

import "fmt"

type frac struct{ num, den int }

func (f frac) String() string {

   return fmt.Sprintf("%d/%d", f.num, f.den)

}

func f(l, r frac, n int) {

   m := frac{l.num + r.num, l.den + r.den}
   if m.den <= n {
       f(l, m, n)
       fmt.Print(m, " ")
       f(m, r, n)
   }

}

func main() {

   // task 1.  solution by recursive generation of mediants
   for n := 1; n <= 11; n++ {
       l := frac{0, 1}
       r := frac{1, 1}
       fmt.Printf("F(%d): %s ", n, l)
       f(l, r, n)
       fmt.Println(r)
   }
   // task 2.  direct solution by summing totient function
   // 2.1 generate primes to 1000
   var composite [1001]bool
   for _, p := range []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31} {
       for n := p * 2; n <= 1000; n += p {
           composite[n] = true
       }
   }
   // 2.2 generate totients to 1000
   var tot [1001]int
   for i := range tot {
       tot[i] = 1
   }
   for n := 2; n <= 1000; n++ {
       if !composite[n] {
           tot[n] = n - 1
           for a := n * 2; a <= 1000; a += n {
               f := n - 1
               for r := a / n; r%n == 0; r /= n {
                   f *= n
               }
               tot[a] *= f
           }
       }
   }
   // 2.3 sum totients
   for n, sum := 1, 1; n <= 1000; n++ {
       sum += tot[n]
       if n%100 == 0 {
           fmt.Printf("|F(%d)|: %d\n", n, sum)
       }
   }

}</lang>

Output:
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
F(9): 0/1 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1
F(10): 0/1 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1/1
F(11): 0/1 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1/1
|F(100)|: 3045
|F(200)|: 12233
|F(300)|: 27399
|F(400)|: 48679
|F(500)|: 76117
|F(600)|: 109501
|F(700)|: 149019
|F(800)|: 194751
|F(900)|: 246327
|F(1000)|: 304193


Haskell

Generating an n'th order Farey sequence follows the algorithm described in Wikipedia. However, for fun, to generate a list of Farey sequences we generate only the highest order sequence, creating the rest by successively pruning the original. <lang Haskell>import Data.List import Data.Ratio import Text.Printf

-- The n'th order Farey sequence. farey :: Integer -> [Rational] farey n = 0 : unfoldr step (0,1,1,n)

 where step (a,b,c,d) | c > n     = Nothing
                      | otherwise = let k = (n+b) `quot` d
                                    in Just (c%d, (c,d,k*c-a,k*d-b))

-- A list of pairs, (n, fn n), where fn is a function applied to the n'th order -- Farey sequence. We assume the list of orders is increasing. Only the -- highest order Farey sequence is evaluated; the remainder are generated by -- successively pruning this sequence. fareys :: ([Rational] -> a) -> [Integer] -> [(Integer, a)] fareys fn ns = snd $ mapAccumR prune (farey $ last ns) ns

 where prune rs n = let rs' = filter ((<=n) . denominator) rs
                    in (rs', (n, fn rs'))

fprint :: (PrintfArg b) => String -> [(Integer, b)] -> IO () fprint fmt = mapM_ (uncurry $ printf fmt)

showFracs :: [Rational] -> String showFracs = unwords . map showFrac

 where showFrac r = show (numerator r) ++ "/" ++ show (denominator r)

main :: IO () main = do

 putStrLn "Farey Sequences\n"
 fprint "%2d %s\n" $ fareys showFracs [1..11]
 putStrLn "\nSequence Lengths\n"
 fprint "%4d %d\n" $ fareys length [100,200..1000]</lang>

Output:

Farey Sequences

 1 0/1 1/1
 2 0/1 1/2 1/1
 3 0/1 1/3 1/2 2/3 1/1
 4 0/1 1/4 1/3 1/2 2/3 3/4 1/1
 5 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
 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
 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
 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
 9 0/1 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1
10 0/1 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1/1
11 0/1 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1/1

Sequence Lengths

 100 3045
 200 12233
 300 27399
 400 48679
 500 76117
 600 109501
 700 149019
 800 194751
 900 246327
1000 304193

J

J has an internal data representation for completely reduced rational numbers. This displays as integers where that is possible and otherwise displays as NNNrDDD where the part to the left of the 'r' is the numerator and the part to the right of the 'r' is the denominator.

This mechanism is a part of J's "constant language", and is similar to scientific notation (which uses an 'e' instead of an 'r') and with J's complex number notation (which uses a 'j' instead of an 'r'), and which follow similar display rules.

This mechanism also hints that J's type promotion rules are designed to give internally consistent results a priority. As much as possible you do not get different results from the same operation just because you "used a different data type". J's design adopts the philosophy that "different results from the same operation based on different types" is likely to introduce errors in thinking. (Of course there are machine limits and certain floating point operations tend to introduce internal inconsistencies, but those are mentioned only in passing - they are not directly relevant to this task.)

<lang J>Farey=:3 :0

 0,/:~~.(#~ <:&1),%/~1x+i.y

)</lang>

Required examples:

<lang J> Farey 1 0 1

  Farey 2

0 1r2 1

  Farey 3

0 1r3 1r2 2r3 1

  Farey 4

0 1r4 1r3 1r2 2r3 3r4 1

  Farey 5

0 1r5 1r4 1r3 2r5 1r2 3r5 2r3 3r4 4r5 1

  Farey 6

0 1r6 1r5 1r4 1r3 2r5 1r2 3r5 2r3 3r4 4r5 5r6 1

  Farey 7

0 1r7 1r6 1r5 1r4 2r7 1r3 2r5 3r7 1r2 4r7 3r5 2r3 5r7 3r4 4r5 5r6 6r7 1

  Farey 8

0 1r8 1r7 1r6 1r5 1r4 2r7 1r3 3r8 2r5 3r7 1r2 4r7 3r5 5r8 2r3 5r7 3r4 4r5 5r6 6r7 7r8 1

  Farey 9

0 1r9 1r8 1r7 1r6 1r5 2r9 1r4 2r7 1r3 3r8 2r5 3r7 4r9 1r2 5r9 4r7 3r5 5r8 2r3 5r7 3r4 7r9 4r5 5r6 6r7 7r8 8r9 1

  Farey 10

0 1r10 1r9 1r8 1r7 1r6 1r5 2r9 1r4 2r7 3r10 1r3 3r8 2r5 3r7 4r9 1r2 5r9 4r7 3r5 5r8 2r3 7r10 5r7 3r4 7r9 4r5 5r6 6r7 7r8 8r9 9r10 1

  Farey 11

0 1r11 1r10 1r9 1r8 1r7 1r6 2r11 1r5 2r9 1r4 3r11 2r7 3r10 1r3 4r11 3r8 2r5 3r7 4r9 5r11 1r2 6r11 5r9 4r7 3r5 5r8 7r11 2r3 7r10 5r7 8r11 3r4 7r9 4r5 9r11 5r6 6r7 7r8 8r9 9r10 10r11 1

  (,. #@Farey"0) 100*1+i.10
100   3045
200  12233
300  27399
400  48679
500  76117
600 109501
700 149019
800 194751
900 246327

1000 304193</lang>

Optimized

A small change in the 'Farey' function makes the last request, faster.

A second change in the 'Farey' function makes the last request, much faster.

A third change in the 'Farey' function makes the last request, again, a little bit faster.

Even if it is 20 times faster, the response time is just acceptable. Now the response time is quite satisfactory.

The script produces the sequences in rational number notation as well in fractional number notation.

<lang J>Farey=: 3 : '/:~,&0 1~.(#~<&1),(1&+%/2&+)i.y-1'

NB. rational number notation rplc&(' 0';'= 0r0');,&('r1',LF)@:,~&'F'@:":@:x:&.>(,Farey)&.>1+i.11

NB. fractional number notation rplc&('r';'/';' 0';'= 0/0');,&('r1',LF)@:,~&'F'@:":@:x:&.>(,Farey)&.>1+i.11

NB. number of fractions

,&(' items',LF)@
,~&'F'@:":&.>(,.#@:Farey)&.>100*1+i.10</lang>
Output:
F1= 0r0 1r1
F2= 0r0 1r2 1r1
F3= 0r0 1r3 1r2 2r3 1r1
F4= 0r0 1r4 1r3 1r2 2r3 3r4 1r1
F5= 0r0 1r5 1r4 1r3 2r5 1r2 3r5 2r3 3r4 4r5 1r1
F6= 0r0 1r6 1r5 1r4 1r3 2r5 1r2 3r5 2r3 3r4 4r5 5r6 1r1
F7= 0r0 1r7 1r6 1r5 1r4 2r7 1r3 2r5 3r7 1r2 4r7 3r5 2r3 5r7 3r4 4r5 5r6 6r7 1r1
F8= 0r0 1r8 1r7 1r6 1r5 1r4 2r7 1r3 3r8 2r5 3r7 1r2 4r7 3r5 5r8 2r3 5r7 3r4 4r5 5r6 6r7 7r8 1r1
F9= 0r0 1r9 1r8 1r7 1r6 1r5 2r9 1r4 2r7 1r3 3r8 2r5 3r7 4r9 1r2 5r9 4r7 3r5 5r8 2r3 5r7 3r4 7r9 4r5 5r6 6r7 7r8 8r9 1r1
F10= 0r0 1r10 1r9 1r8 1r7 1r6 1r5 2r9 1r4 2r7 3r10 1r3 3r8 2r5 3r7 4r9 1r2 5r9 4r7 3r5 5r8 2r3 7r10 5r7 3r4 7r9 4r5 5r6 6r7 7r8 8r9 9r10 1r1
F11= 0r0 1r11 1r10 1r9 1r8 1r7 1r6 2r11 1r5 2r9 1r4 3r11 2r7 3r10 1r3 4r11 3r8 2r5 3r7 4r9 5r11 1r2 6r11 5r9 4r7 3r5 5r8 7r11 2r3 7r10 5r7 8r11 3r4 7r9 4r5 9r11 5r6 6r7 7r8 8r9 9r10 10r11 1r1

F1= 0/0 1/1
F2= 0/0 1/2 1/1
F3= 0/0 1/3 1/2 2/3 1/1
F4= 0/0 1/4 1/3 1/2 2/3 3/4 1/1
F5= 0/0 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
F6= 0/0 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/0 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/0 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
F9= 0/0 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1
F10= 0/0 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1/1
F11= 0/0 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1/1

F100 3045 items
F200 12233 items
F300 27399 items
F400 48679 items
F500 76117 items
F600 109501 items
F700 149019 items
F800 194751 items
F900 246327 items
F1000 304193 items

Java

Works with: Java version 1.5+

This example uses the fact that it generates the fraction candidates from the bottom up as well as Set's internal duplicate removal (based on Comparable.compareTo) to get rid of un-reduced fractions. It also uses TreeSet to sort based on the value of the fraction. <lang java5>import java.util.TreeSet;

public class Farey{ private static class Frac implements Comparable<Frac>{ int num; int den;

public Frac(int num, int den){ this.num = num; this.den = den; }

@Override public String toString(){ return num + " / " + den; }

@Override public int compareTo(Frac o){ return Double.compare((double)num / den, (double)o.num / o.den); } }

public static TreeSet<Frac> genFarey(int i){ TreeSet<Frac> farey = new TreeSet<Frac>(); for(int den = 1; den <= i; den++){ for(int num = 0; num <= den; num++){ farey.add(new Frac(num, den)); } } return farey; }

public static void main(String[] args){ for(int i = 1; i <= 11; i++){ System.out.println("F" + i + ": " + genFarey(i)); }

for(int i = 100; i <= 1000; i += 100){ System.out.println("F" + i + ": " + genFarey(i).size() + " members"); } } }</lang>

Output:
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]
F9: [0 / 1, 1 / 9, 1 / 8, 1 / 7, 1 / 6, 1 / 5, 2 / 9, 1 / 4, 2 / 7, 1 / 3, 3 / 8, 2 / 5, 3 / 7, 4 / 9, 1 / 2, 5 / 9, 4 / 7, 3 / 5, 5 / 8, 2 / 3, 5 / 7, 3 / 4, 7 / 9, 4 / 5, 5 / 6, 6 / 7, 7 / 8, 8 / 9, 1 / 1]
F10: [0 / 1, 1 / 10, 1 / 9, 1 / 8, 1 / 7, 1 / 6, 1 / 5, 2 / 9, 1 / 4, 2 / 7, 3 / 10, 1 / 3, 3 / 8, 2 / 5, 3 / 7, 4 / 9, 1 / 2, 5 / 9, 4 / 7, 3 / 5, 5 / 8, 2 / 3, 7 / 10, 5 / 7, 3 / 4, 7 / 9, 4 / 5, 5 / 6, 6 / 7, 7 / 8, 8 / 9, 9 / 10, 1 / 1]
F11: [0 / 1, 1 / 11, 1 / 10, 1 / 9, 1 / 8, 1 / 7, 1 / 6, 2 / 11, 1 / 5, 2 / 9, 1 / 4, 3 / 11, 2 / 7, 3 / 10, 1 / 3, 4 / 11, 3 / 8, 2 / 5, 3 / 7, 4 / 9, 5 / 11, 1 / 2, 6 / 11, 5 / 9, 4 / 7, 3 / 5, 5 / 8, 7 / 11, 2 / 3, 7 / 10, 5 / 7, 8 / 11, 3 / 4, 7 / 9, 4 / 5, 9 / 11, 5 / 6, 6 / 7, 7 / 8, 8 / 9, 9 / 10, 10 / 11, 1 / 1]
F100: 3045 members
F200: 12233 members
F300: 27399 members
F400: 48679 members
F500: 76117 members
F600: 109501 members
F700: 149019 members
F800: 194751 members
F900: 246327 members
F1000: 304193 members

PARI/GP

<lang parigp>Farey(n)=my(v=List()); for(k=1,n,for(i=0,k,listput(v,i/k))); vecsort(Set(v)); countFarey(n)=1+sum(k=1, n, eulerphi(k)); for(n=1,11,print(Farey(n))) apply(countFarey, 100*[1..10])</lang>

Output:
[0, 1]
[0, 1/2, 1]
[0, 1/3, 1/2, 2/3, 1]
[0, 1/4, 1/3, 1/2, 2/3, 3/4, 1]
[0, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1]
[0, 1/6, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 5/6, 1]
[0, 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]
[0, 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]
[0, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 1]
[0, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 3/10, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 7/10, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 9/10, 1]
[0, 1/11, 1/10, 1/9, 1/8, 1/7, 1/6, 2/11, 1/5, 2/9, 1/4, 3/11, 2/7, 3/10, 1/3, 4/11, 3/8, 2/5, 3/7, 4/9, 5/11, 1/2, 6/11, 5/9, 4/7, 3/5, 5/8, 7/11, 2/3, 7/10, 5/7, 8/11, 3/4, 7/9, 4/5, 9/11, 5/6, 6/7, 7/8, 8/9, 9/10, 10/11, 1]

%1 = [3045, 12233, 27399, 48679, 76117, 109501, 149019, 194751, 246327, 304193]

Perl

Recurrence

This uses the recurrence from Concrete Mathematics exercise 4.61 to create them quickly (this is also on the Wikipedia page). It also uses the totient sum to quickly get the counts. <lang perl>use warnings; use strict; use Math::BigRat; use ntheory qw/euler_phi vecsum/;

sub farey {

 my $N = shift;
 my @f;
 my($m0,$n0, $m1,$n1) = (0, 1, 1, $N);
 push @f, Math::BigRat->new("$m0/$n0");
 push @f, Math::BigRat->new("$m1/$n1");
 while ($f[-1] < 1) {
   my $m = int( ($n0 + $N) / $n1) * $m1 - $m0;
   my $n = int( ($n0 + $N) / $n1) * $n1 - $n0;
   ($m0,$n0, $m1,$n1) = ($m1,$n1, $m,$n);
   push @f, Math::BigRat->new("$m/$n");
 }
 @f;

} sub farey_count { 1 + vecsum(euler_phi(1, shift)); }

for (1 .. 11) {

 my @f = map { join "/", $_->parts }   # Force 0/1 and 1/1
         farey($_);
 print "F$_: [@f]\n";

} for (1 .. 10, 100000) {

 print "F${_}00: ", farey_count(100*$_), " members\n";

}</lang>

Output:
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]
F9: [0/1 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1]
F10: [0/1 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1/1]
F11: [0/1 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1/1]
F100: 3045 members
F200: 12233 members
F300: 27399 members
F400: 48679 members
F500: 76117 members
F600: 109501 members
F700: 149019 members
F800: 194751 members
F900: 246327 members
F1000: 304193 members
F10000000: 30396356427243 members

Mapped Rationals

Similar to Pari and Perl6. Same output, quite slow. Using the recursive formula for the count, utilizing the Memoize module, would be a big help. <lang perl>use warnings; use strict; use Math::BigRat;

sub farey {

 my $n = shift;
 my %v;
 for my $k (1 .. $n) {
   for my $i (0 .. $k) {
     $v{ Math::BigRat->new("$i/$k")->bstr }++;
   }
 }
 my @f = sort {$a <=> $b }
         map { Math::BigRat->new($_) }
         keys %v;
 @f;

}

for (1 .. 11) {

 my @f = map { join "/", $_->parts }   # Force 0/1 and 1/1
         farey($_);
 print "F$_: [@f]\n";

} for (1 .. 10) {

 my @f = farey(100*$_);
 print "F${_}00: ", scalar(@f), " members\n";

}</lang>

Perl 6

Works with: rakudo version 2016.05

<lang perl6>sub farey ($order) {

   my @l = 0/1;
   (1..$order).map: { push @l, |(1..$^d).map: { $^n/$d } }
   unique gather @l.deepmap(*.take);

}

say "Farey sequence order "; say "$_: ", .&farey.sort.map: *.nude.join('/') for 1..11; say "Farey sequence order $_ has ", [.&farey].elems, ' elements.' for 100, 200 ... 1000;</lang>

Output:
Farey sequence order 
1: 0/1 1/1
2: 0/1 1/2 1/1
3: 0/1 1/3 1/2 2/3 1/1
4: 0/1 1/4 1/3 1/2 2/3 3/4 1/1
5: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
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
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
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
9: 0/1 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1
10: 0/1 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1/1
11: 0/1 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1/1
Farey sequence order 100 has 3045 elements.
Farey sequence order 200 has 12233 elements.
Farey sequence order 300 has 27399 elements.
Farey sequence order 400 has 48679 elements.
Farey sequence order 500 has 76117 elements.
Farey sequence order 600 has 109501 elements.
Farey sequence order 700 has 149019 elements.
Farey sequence order 800 has 194751 elements.
Farey sequence order 900 has 246327 elements.
Farey sequence order 1000 has 304193 elements.

Prolog

The following uses SWI-Prolog's rationals (rdiv(p,q)) and assumes the availability of predsort/3. The presentation is top-down. <lang Prolog>task(1) :- between(1, 11, I), farey(I, F), write(I), write(': '), rwrite(F), nl, fail; true.

task(2) :- between(1, 10, I), I100 is I*100, farey( I100, F), length(F,N), write('|F('), write(I100), write(')| = '), writeln(N), fail; true.

% farey(+Order, Sequence) farey(Order, Sequence) :-

 bagof( R,

I^J^(between(1, Order, J), between(0, J, I), R is I rdiv J), S),

 predsort( rcompare, S, Sequence ).

rprint( rdiv(A,B) ) :- write(A), write(/), write(B), !. rprint( I ) :- integer(I), write(I), write(/), write(1), !.

rwrite([]). rwrite([R]) :- rprint(R). rwrite([R, T|Rs]) :- rprint(R), write(', '), rwrite([T|Rs]).

rcompare(<, A, B) :- A < B, !. rcompare(>, A, B) :- A > B, !. rcompare(=, A, B) :- A =< B.</lang>

Interactive session:

?- task(1).
1: 0/1, 1/1
2: 0/1, 1/2, 1/1
3: 0/1, 1/3, 1/2, 2/3, 1/1
4: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1
5: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1
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
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
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
9: 0/1, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 1/1
10: 0/1, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 3/10, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 7/10, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 9/10, 1/1
11: 0/1, 1/11, 1/10, 1/9, 1/8, 1/7, 1/6, 2/11, 1/5, 2/9, 1/4, 3/11, 2/7, 3/10, 1/3, 4/11, 3/8, 2/5, 3/7, 4/9, 5/11, 1/2, 6/11, 5/9, 4/7, 3/5, 5/8, 7/11, 2/3, 7/10, 5/7, 8/11, 3/4, 7/9, 4/5, 9/11, 5/6, 6/7, 7/8, 8/9, 9/10, 10/11, 1/1
true

?- task(2).
|F(100)| = 3045
|F(200)| = 12233
|F(300)| = 27399
|F(400)| = 48679
|F(500)| = 76117
|F(600)| = 109501
|F(700)| = 149019
|F(800)| = 194751
|F(900)| = 246327
|F(1000)| = 304193
true.

Python

<lang python>from fractions import Fraction


class Fr(Fraction):

   def __repr__(self):
       return '(%s/%s)' % (self.numerator, self.denominator)


def farey(n, length=False):

   if not length:
       return [Fr(0, 1)] + sorted({Fr(m, k) for k in range(1, n+1) for m in range(1, k+1)})
   else:
       #return 1         +    len({Fr(m, k) for k in range(1, n+1) for m in range(1, k+1)})
       return  (n*(n+3))//2 - sum(farey(n//k, True) for k in range(2, n+1))
       

if __name__ == '__main__':

   print('Farey sequence for order 1 through 11 (inclusive):')
   for n in range(1, 12): 
       print(farey(n))
   print('Number of fractions in the Farey sequence for order 100 through 1,000 (inclusive) by hundreds:')
   print([farey(i, length=True) for i in range(100, 1001, 100)])</lang>
Output:
Farey sequence for order 1 through 11 (inclusive):
[(0/1), (1/1)]
[(0/1), (1/2), (1/1)]
[(0/1), (1/3), (1/2), (2/3), (1/1)]
[(0/1), (1/4), (1/3), (1/2), (2/3), (3/4), (1/1)]
[(0/1), (1/5), (1/4), (1/3), (2/5), (1/2), (3/5), (2/3), (3/4), (4/5), (1/1)]
[(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)]
[(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)]
[(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)]
[(0/1), (1/9), (1/8), (1/7), (1/6), (1/5), (2/9), (1/4), (2/7), (1/3), (3/8), (2/5), (3/7), (4/9), (1/2), (5/9), (4/7), (3/5), (5/8), (2/3), (5/7), (3/4), (7/9), (4/5), (5/6), (6/7), (7/8), (8/9), (1/1)]
[(0/1), (1/10), (1/9), (1/8), (1/7), (1/6), (1/5), (2/9), (1/4), (2/7), (3/10), (1/3), (3/8), (2/5), (3/7), (4/9), (1/2), (5/9), (4/7), (3/5), (5/8), (2/3), (7/10), (5/7), (3/4), (7/9), (4/5), (5/6), (6/7), (7/8), (8/9), (9/10), (1/1)]
[(0/1), (1/11), (1/10), (1/9), (1/8), (1/7), (1/6), (2/11), (1/5), (2/9), (1/4), (3/11), (2/7), (3/10), (1/3), (4/11), (3/8), (2/5), (3/7), (4/9), (5/11), (1/2), (6/11), (5/9), (4/7), (3/5), (5/8), (7/11), (2/3), (7/10), (5/7), (8/11), (3/4), (7/9), (4/5), (9/11), (5/6), (6/7), (7/8), (8/9), (9/10), (10/11), (1/1)]
Number of fractions in the Farey sequence for order 100 through 1,000 (inclusive) by hundreds:
[3045, 12233, 27399, 48679, 76117, 109501, 149019, 194751, 246327, 304193]

Racket

Once again, racket's math/number-theory package comes to the rescue! <lang racket>#lang racket (require math/number-theory) (define (display-farey-sequence order show-fractions?)

 (define f-s (farey-sequence order))
 (printf "-- Farey Sequence for order ~a has ~a fractions~%" order (length f-s))
 ;; racket will simplify 0/1 and 1/1 to 0 and 1 respectively, so deconstruct into numerator and
 ;; denomimator (and take the opportunity to insert commas
 (when show-fractions?
   (displayln
    (string-join
     (for/list ((f f-s))
       (format "~a/~a" (numerator f) (denominator f)))
     ", "))))
compute and show the Farey sequence for order
1 through 11 (inclusive).

(for ((order (in-range 1 (add1 11)))) (display-farey-sequence order #t))

compute and display the number of fractions in the Farey sequence for order
100 through 1,000 (inclusive) by hundreds.

(for ((order (in-range 100 (add1 1000) 100))) (display-farey-sequence order #f))</lang>

Output:
-- Farey Sequence for order 1 has 2 fractions
0/1, 1/1
-- Farey Sequence for order 2 has 3 fractions
0/1, 1/2, 1/1
-- Farey Sequence for order 3 has 5 fractions
0/1, 1/3, 1/2, 2/3, 1/1
-- Farey Sequence for order 4 has 7 fractions
0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1
-- Farey Sequence for order 5 has 11 fractions
0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1
-- Farey Sequence for order 6 has 13 fractions
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
-- Farey Sequence for order 7 has 19 fractions
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
-- Farey Sequence for order 8 has 23 fractions
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 Sequence for order 9 has 29 fractions
0/1, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 1/1
-- Farey Sequence for order 10 has 33 fractions
0/1, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 3/10, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 7/10, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 9/10, 1/1
-- Farey Sequence for order 11 has 43 fractions
0/1, 1/11, 1/10, 1/9, 1/8, 1/7, 1/6, 2/11, 1/5, 2/9, 1/4, 3/11, 2/7, 3/10, 1/3, 4/11, 3/8, 2/5, 3/7, 4/9, 5/11, 1/2, 6/11, 5/9, 4/7, 3/5, 5/8, 7/11, 2/3, 7/10, 5/7, 8/11, 3/4, 7/9, 4/5, 9/11, 5/6, 6/7, 7/8, 8/9, 9/10, 10/11, 1/1
-- Farey Sequence for order 100 has 3045 fractions
-- Farey Sequence for order 200 has 12233 fractions
-- Farey Sequence for order 300 has 27399 fractions
-- Farey Sequence for order 400 has 48679 fractions
-- Farey Sequence for order 500 has 76117 fractions
-- Farey Sequence for order 600 has 109501 fractions
-- Farey Sequence for order 700 has 149019 fractions
-- Farey Sequence for order 800 has 194751 fractions
-- Farey Sequence for order 900 has 246327 fractions
-- Farey Sequence for order 1000 has 304193 fractions

REXX

<lang rexx>/*REXX program computes & shows a Farey sequence (or the # of fractions)*/ parse arg L H I . /*get optional values from C.L. */ if L== then L=5 /*L not specified? Use default.*/ oldL=L /*original L (negativity=noshow)*/ L=abs(L) /*but ··· use │L│ for all else.*/ if H== then H=L /*H not specified? Use default.*/ if I== then I=1 /*I " " " " */

                                      /*step through range by increment*/
    do n=L  to  H  by I               /*process range (could be only 1)*/
    @=fareyF(n);    #=' 'words(@)" "  /*go ye forth & compute Farey #s.*/
    say center('Farey sequence for order '   n   " has"   #   'fractions.', 150, '═')
    if oldL<0  then iterate           /*no show Farey fractions if neg.*/
    say @;          say               /*show Farey fractions+blank line*/
    end   /*n*/                       /* [↑] build/show Farey fractions*/

exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────FAREYF subroutine───────────────────*/ fareyF: procedure; parse arg x; n.1=0; d.1=1; n.2=1; d.2=x /*kit parts.*/ $=n.1'/'d.1 n.2"/"d.2 /*a starter kit for the Farey seq*/

                                      /* [↓]  now, build on the starter*/
          do j=1;    y=j+1;   z=j+2   /*construct from thirds on "up". */
          n.z=(d.j+x)%d.y*n.y - n.j   /*    "     fraction numerator.  */
          d.z=(d.j+x)%d.y*d.y - d.j   /*    "         "    denominator.*/
          if n.z>x  then leave        /*Should construction be stopped?*/
          $=$ n.z'/'d.z               /*Heck no, add this to party mix.*/
          end   /*j*/                 /* [↑]   construct the Farey seq.*/

return $ /*return with the Farey fractions*/</lang> output when using the following for input:   1 11

═══════════════════════════════════════════════════Farey sequence for order  1  has  2  fractions.════════════════════════════════════════════════════
0/1 1/1

═══════════════════════════════════════════════════Farey sequence for order  2  has  3  fractions.════════════════════════════════════════════════════
0/1 1/2 1/1

═══════════════════════════════════════════════════Farey sequence for order  3  has  5  fractions.════════════════════════════════════════════════════
0/1 1/3 1/2 2/3 1/1

═══════════════════════════════════════════════════Farey sequence for order  4  has  7  fractions.════════════════════════════════════════════════════
0/1 1/4 1/3 1/2 2/3 3/4 1/1

═══════════════════════════════════════════════════Farey sequence for order  5  has  11  fractions.═══════════════════════════════════════════════════
0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1

═══════════════════════════════════════════════════Farey sequence for order  6  has  13  fractions.═══════════════════════════════════════════════════
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

═══════════════════════════════════════════════════Farey sequence for order  7  has  19  fractions.═══════════════════════════════════════════════════
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

═══════════════════════════════════════════════════Farey sequence for order  8  has  23  fractions.═══════════════════════════════════════════════════
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 sequence for order  9  has  29  fractions.═══════════════════════════════════════════════════
0/1 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1

══════════════════════════════════════════════════Farey sequence for order  10  has  33  fractions.═══════════════════════════════════════════════════
0/1 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1/1

══════════════════════════════════════════════════Farey sequence for order  11  has  43  fractions.═══════════════════════════════════════════════════
0/1 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1/1

output when using the following for input:   -100 1000 100

═════════════════════════════════════════════════Farey sequence for order  100  has  3045  fractions.═════════════════════════════════════════════════
════════════════════════════════════════════════Farey sequence for order  200  has  12233  fractions.═════════════════════════════════════════════════
════════════════════════════════════════════════Farey sequence for order  300  has  27399  fractions.═════════════════════════════════════════════════
════════════════════════════════════════════════Farey sequence for order  400  has  48679  fractions.═════════════════════════════════════════════════
════════════════════════════════════════════════Farey sequence for order  500  has  76117  fractions.═════════════════════════════════════════════════
════════════════════════════════════════════════Farey sequence for order  600  has  109501  fractions.════════════════════════════════════════════════
════════════════════════════════════════════════Farey sequence for order  700  has  149019  fractions.════════════════════════════════════════════════
════════════════════════════════════════════════Farey sequence for order  800  has  194751  fractions.════════════════════════════════════════════════
════════════════════════════════════════════════Farey sequence for order  900  has  246327  fractions.════════════════════════════════════════════════
═══════════════════════════════════════════════Farey sequence for order  1000  has  304193  fractions.════════════════════════════════════════════════

Ruby

Translation of: Python

<lang ruby>def farey(n, length=false)

 if length
   (n*(n+3))/2 - (2..n).inject(0){|sum,k| sum + farey(n/k, true)}
 else
   (1..n).each_with_object([]){|k,a|(0..k).each{|m|a << Rational(m,k)}}.uniq.sort
 end

end

puts 'Farey sequence for order 1 through 11 (inclusive):' for n in 1..11

 puts "F(#{n}): " + farey(n).join(", ")

end puts 'Number of fractions in the Farey sequence:' for i in (100..1000).step(100)

 puts "F(%4d) =%7d" % [i, farey(i, true)]

end</lang>

Output:
Farey sequence for order 1 through 11 (inclusive):
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
F(9): 0/1, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 1/1
F(10): 0/1, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 2/9, 1/4, 2/7, 3/10, 1/3, 3/8, 2/5, 3/7, 4/9, 1/2, 5/9, 4/7, 3/5, 5/8, 2/3, 7/10, 5/7, 3/4, 7/9, 4/5, 5/6, 6/7, 7/8, 8/9, 9/10, 1/1
F(11): 0/1, 1/11, 1/10, 1/9, 1/8, 1/7, 1/6, 2/11, 1/5, 2/9, 1/4, 3/11, 2/7, 3/10, 1/3, 4/11, 3/8, 2/5, 3/7, 4/9, 5/11, 1/2, 6/11, 5/9, 4/7, 3/5, 5/8, 7/11, 2/3, 7/10, 5/7, 8/11, 3/4, 7/9, 4/5, 9/11, 5/6, 6/7, 7/8, 8/9, 9/10, 10/11, 1/1
Number of fractions in the Farey sequence:
F( 100) =   3045
F( 200) =  12233
F( 300) =  27399
F( 400) =  48679
F( 500) =  76117
F( 600) = 109501
F( 700) = 149019
F( 800) = 194751
F( 900) = 246327
F(1000) = 304193

Tcl

Works with: Tcl version 8.6

<lang tcl>package require Tcl 8.6

proc farey {n} {

   set nums [lrepeat [expr {$n+1}] 1]
   set result Template:0 1
   for {set found 1} {$found} {} {

set nj [lindex $nums [set j 1]] for {set found 0;set i 1} {$i <= $n} {incr i} { if {[lindex $nums $i]*$j < $nj*$i} { set nj [lindex $nums [set j $i]] set found 1 } } lappend result [list $nj $j] for {set i $j} {$i <= $n} {incr i $j} { lset nums $i [expr {[lindex $nums $i] + 1}] }

   }
   return $result

}

for {set i 1} {$i <= 11} {incr i} {

   puts F($i):\x20[lmap n [farey $i] {join $n /}]

} for {set i 100} {$i <= 1000} {incr i 100} {

   puts |F($i)|\x20=\x20[llength [farey $i]]

}</lang>

Output:
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
F(9): 0/1 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1
F(10): 0/1 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1/1
F(11): 0/1 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1/1
|F(100)| = 3045
|F(200)| = 12233
|F(300)| = 27399
|F(400)| = 48679
|F(500)| = 76117
|F(600)| = 109501
|F(700)| = 149019
|F(800)| = 194751
|F(900)| = 246327
|F(1000)| = 304193