Farey sequence: Difference between revisions

(→‎{{header|Scala}}: migrate to Scala 2.13)
 
(106 intermediate revisions by 38 users not shown)
Line 13:
The Farey sequences of orders   '''1'''   to   '''5'''   are:
 
:::: <math>{\bf\it{F}}_1 = \frac{0}{1}, \frac{1}{1}</math>
:<br>
:::: <math>{\bf\it{F}}_2 = \frac{0}{1}, \frac{1}{2}, \frac{1}{1}</math>
:<br>
:::: <math>{\bf\it{F}}_3 = \frac{0}{1}, \frac{1}{3}, \frac{1}{2}, \frac{2}{3}, \frac{1}{1}</math>
:<br>
:::: <math>{\bf\it{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>{\bf\it{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
Line 29:
 
 
The length &nbsp; (the number of fractions) &nbsp; of a Farey sequence asymptotically approaches:
;See also
 
* &nbsp; OEIS sequence &nbsp; [http://oeis.org/A006842 A006842 numerators of Farey series of order 1, 2, ···]
::::::::::: <big><big> 3 &times; n<sup>2</sup> &nbsp;<big> ÷ </big>&nbsp; <big><math>\pi</math></big><sup>2</sup> </big></big>
* &nbsp; OEIS sequence &nbsp; [http://oeis.org/A006843 A006843 denominators of Farey series of order 1, 2, ···]
 
* &nbsp; OEIS sequence &nbsp; [http://oeis.org/A005728 A005728 number of fractions in Farey series of order n.]
;See also:
* &nbsp; MathWorld entry &nbsp; [http://mathworld.wolfram.com/FareySequence.html Farey sequence]
* &nbsp; OEIS sequence &nbsp; [[oeis:A006842|A006842 numerators of Farey series of order 1, 2, ···]]
* &nbsp; OEIS sequence &nbsp; [[oeis:A006843|A006843 denominators of Farey series of order 1, 2, ···]]
* &nbsp; OEIS sequence &nbsp; [[oeis:A005728|A005728 number of fractions in Farey series of order n]]
* &nbsp; MathWorld entry &nbsp; [http://mathworld.wolfram.com/FareySequence.html Farey sequence]
* &nbsp; Wikipedia &nbsp; entry &nbsp; [[wp:Farey_sequence|Farey sequence]]
<br><br>
 
Line 39 ⟶ 44:
{{trans|Lua}}
 
<langsyntaxhighlight lang="11l">F farey(n)
V a = 0
V b = 1
Line 57 ⟶ 62:
 
L(i) (100..1000).step(100)
print(i‘: ’farey(i)[1]‘ items’)</langsyntaxhighlight>
 
{{out}}
Line 84 ⟶ 89:
</pre>
 
=={{header|AWKALGOL 68}}==
Based on...
<lang AWK>
{{trans|Lua}}... but calculates and optionally prints the sequences without actually storing them.
# syntax: GAWK -f FAREY_SEQUENCE.AWK
<syntaxhighlight lang="algol68">BEGIN # construct some Farey Sequences and calculate their lengths #
BEGIN {
# prints an element of a Farey Sequence #
for (i=1; i<=11; i++) {
PROC print element = ( INT a, b )VOID:
farey(i); printf("\n")
print( ( " ", whole( a, 0 ), "/", whole( b, 0 ) ) );
}
# returns the length of the Farey Sequence of order n, optionally #
for (i=100; i<=1000; i+=100) {
# printing it #
printf(" %d items\n",farey(i))
PROC farey sequence length = ( INT n, BOOL print sequence )INT:
}
IF n < 1 THEN 0
exit(0)
ELSE
}
INT a := 0, b := 1, c := 1, d := n;
function farey(n, a,aa,b,bb,c,cc,d,dd,items,k) {
a = 0; b = 1; c = 1;IF dprint =sequence nTHEN
print( ( whole( n, -2 ), ":" ) );
printf("%d:",n)
if print element(n <=a, 11)b {)
printf(" %d/%d",a,b) FI;
INT length := 1;
}
while ( WHILE c <= n) {DO
INT k = int(( n + b )/ OVER d);
aa = c; bb = d; cc INT old a = k*c-a;, ddold b = k*d-b;
a = aa; b = bb; c = cc; d a := ddc;
items++ b := d;
if (n < c := 11( k * c ) {- old a;
printf d := (" %k * d/%d",a,b ) - old b;
IF print sequence THEN print element( a, b ) FI;
}
length +:= 1
}
return(1+items) OD;
IF print sequence THEN print( ( newline ) ) FI;
}
length
</lang>
FI # farey sequence length # ;
# task #
FOR i TO 11 DO farey sequence length( i, TRUE ) OD;
FOR n FROM 100 BY 100 TO 1 000 DO
print( ( "Farey Sequence of order ", whole( n, -4 )
, " has length: ", whole( farey sequence length( n, FALSE ), -6 )
, newline
)
)
OD
END</syntaxhighlight>
{{out}}
<pre>
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 of order 100 has length: 3045
100: 3045 items
Farey Sequence of order 200 has length: 12233
200: 12233 items
Farey Sequence of order 300 has length: 27399
300: 27399 items
Farey Sequence of order 400 has length: 48679
400: 48679 items
Farey Sequence of order 500 has length: 76117
500: 76117 items
Farey Sequence of order 600 has length: 109501 items
Farey Sequence of order 700 has length: 149019 items
Farey Sequence of order 800 has length: 194751 items
Farey Sequence of order 900 has length: 246327 items
Farey Sequence of order 1000 has length: 304193 items
</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">
<lang APL>
farey←{{⍵[⍋⍵]}∪∊{(0,⍳⍵)÷⍵}¨⍳⍵}
fract←{1∧(0(⍵=0)+⊂⍵)*1 ¯1}
print←{{(⍕⍺),'/',(⍕⍵),' '}⌿↑fract farey ⍵}
</syntaxhighlight>
</lang>
Note that this is a brute-force algorithm, not the sequential one given on Wikipedia.
Basically, given n this one generates and then sorts the set
Line 212 ⟶ 228:
1000 | 304193
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">farey: function [n][
f1: [0 1]
f2: @[1 n]
result: @["0/1" ~"1/|n|"]
 
while [1 < f2\1][
k: (n + f1\1) / f2\1
aux: f1
f1: f2
f2: @[
(f2\0 * k) - aux\0,
(f2\1 * k) - aux\1
]
'result ++ (to :string f2\0) ++ "/" ++ (to :string f2\1)
]
return result
]
 
loop 1..11 'i ->
print [pad (to :string i) ++ ":" 3 join.with:" " farey i]
 
print ""
print "Number of fractions in the Farey sequence:"
 
loop range.step: 100 100 1000 'r ->
print "F(" ++ (pad (to :string r) 4) ++ ") = " ++ (pad to :string size farey r 6)
</syntaxhighlight>
 
{{out}}
 
<pre> 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
 
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</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FAREY_SEQUENCE.AWK
BEGIN {
for (i=1; i<=11; i++) {
farey(i); printf("\n")
}
for (i=100; i<=1000; i+=100) {
printf(" %d items\n",farey(i))
}
exit(0)
}
function farey(n, a,aa,b,bb,c,cc,d,dd,items,k) {
a = 0; b = 1; c = 1; d = n
printf("%d:",n)
if (n <= 11) {
printf(" %d/%d",a,b)
}
while (c <= n) {
k = int((n+b)/d)
aa = c; bb = d; cc = k*c-a; dd = k*d-b
a = aa; b = bb; c = cc; d = dd
items++
if (n <= 11) {
printf(" %d/%d",a,b)
}
}
return(1+items)
}
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">for i = 1 to 11
print "F"; i; " = ";
call farey(i, FALSE)
next i
print
for i = 100 to 1000 step 100
print "F"; i;
if i <> 1000 then print " "; else print "";
print " = ";
call farey(i, FALSE)
next i
end
 
subroutine farey(n, descending)
a = 0 : b = 1 : c = 1 : d = n : k = 0
cont = 0
 
if descending = TRUE then
a = 1 : c = n -1
end if
 
cont += 1
if n < 12 then print a; "/"; b; " ";
 
while ((c <= n) and not descending) or ((a > 0) and descending)
aa = a : bb = b : cc = c : dd = d
k = (n + b) \ d
a = cc : b = dd : c = k * cc - aa : d = k * dd - bb
cont += 1
if n < 12 then print a; "/"; b; " ";
end while
 
if n < 12 then print else print rjust(cont,7)
end subroutine</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">FUNCTION farey (n, dsc)
b = 1: c = 1: d = n
IF dsc = TRUE THEN a = 1: c = n - 1
 
cnt = cnt + 1
IF n < 12 THEN PRINT a; "/"; b;
WHILE ((c <= n) AND NOT dsc) OR ((a > 0) AND dsc)
aa = a: bb = b: cc = c: dd = d
k = (n + b) \ d
a = cc: b = dd: c = k * cc - aa: d = k * dd - bb
cnt = cnt + 1
IF n < 12 THEN PRINT a; "/"; b;
WEND
IF n < 12 THEN PRINT
farey = cnt
END FUNCTION
 
CLS
CONST TRUE = -1: FALSE = NOT TRUE
FOR i = 1 TO 9
PRINT USING "F# ="; i;
t = farey(i, FALSE)
NEXT i
FOR i = 10 TO 11
PRINT USING "F## ="; i;
t = farey(i, FALSE)
NEXT i
PRINT
FOR i = 100 TO 900 STEP 100
PRINT USING "F#### = ######"; i; farey(i, FALSE)
NEXT i
PRINT USING "F1000 = ######"; farey(1000, FALSE)</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 274 ⟶ 482:
printf("\n%d: %llu items\n", n, farey_len(n));
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 302 ⟶ 510:
</pre>
 
=={{header|C sharp|C#}}==
{{works with|C sharp|7}}
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 330 ⟶ 538:
return seq;
}
}</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 357 ⟶ 565:
 
=={{header|C++}}==
===Object-based programming===
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
 
struct fraction {
void print_fraction(int a, int b)
fraction(int n, int d) : numerator(n), denominator(d) {}
{
int numerator;
std::cout << a << '/' << b;
int denominator;
};
 
std::ostream& operator<<(std::ostream& out, const fraction& f) {
out << f.numerator << '/' << f.denominator;
return out;
}
 
class farey_sequence {
//
public:
// See https://en.wikipedia.org/wiki/Farey_sequence#Next_term
explicit farey_sequence(int n) : n_(n), a_(0), b_(1), c_(1), d_(n) {}
//
fraction next() {
void print_farey_sequence(int n, bool length_only)
// See https://en.wikipedia.org/wiki/Farey_sequence#Next_term
{
std::cout << n << ":fraction "result(a_, b_);
int a = 0, bint = 1, ck = 1,(n_ d =+ nb_)/d_;
int next_c = k * c_ - a_;
if (!length_only)
print_fraction(a,int b)next_d = k * d_ - b_;
int count a_ = 1c_;
for (; c < b_ = nd_; ++count)
c_ = next_c;
{
int kd_ = (n + b)/dnext_d;
intreturn next_c = k * c - aresult;
int next_d = k * d - b;
a = c;
b = d;
c = next_c;
d = next_d;
if (!length_only)
{
std::cout << ' ';
print_fraction(a, b);
}
}
bool has_next() const { return a_ <= n_; }
if (length_only)
private:
std::cout << count << " items";
std::coutint <<n_, '\n'a_, b_, c_, d_;
};
 
int main() {
for (int n = 1; n <= 11; ++n) {
{
for (int i = 1;farey_sequence i <= 11seq(n); ++i)
std::cout << n << ": " << seq.next();
print_farey_sequence(i, false);
for (int i = 100;while i <= 1000; i += 100(seq.has_next())
std::cout << ' ' << seq.next();
print_farey_sequence(i, true);
std::cout << '\n';
}
for (int n = 100; n <= 1000; n += 100) {
int count = 0;
for (farey_sequence seq(n); seq.has_next(); seq.next())
++count;
std::cout << n << ": " << count << '\n';
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 428 ⟶ 641:
</pre>
 
===Object-oriented programming===
=={{header|Common Lisp}}==
{{Works with|C++17}}
<syntaxhighlight lang="cpp">#include <iostream>
#include <list>
#include <utility>
 
struct farey_sequence: public std::list<std::pair<uint, uint>> {
{{improve|Common Lisp| <br><br> The output for the first and last term &nbsp; (as per the task's requirement)
explicit farey_sequence(uint n) : order(n) {
<br> is to show the first term as &nbsp; <big>'''0/1'''</big>,
push_back(std::pair(0, 1));
<br> and to show the last term as &nbsp; <big>'''1/1'''</big>. <br><br> }}
uint a = 0, b = 1, c = 1, d = n;
while (c <= n) {
const uint k = (n + b) / d;
const uint next_c = k * c - a;
const uint next_d = k * d - b;
a = c;
b = d;
c = next_c;
d = next_d;
push_back(std::pair(a, b));
}
}
 
const uint order;
};
 
std::ostream& operator<<(std::ostream &out, const farey_sequence &s) {
out << s.order << ":";
for (const auto &f : s)
out << ' ' << f.first << '/' << f.second;
return out;
}
 
int main() {
for (uint i = 1u; i <= 11u; ++i)
std::cout << farey_sequence(i) << std::endl;
for (uint i = 100u; i <= 1000u; i += 100u) {
const auto s = farey_sequence(i);
std::cout << s.order << ": " << s.size() << " items" << std::endl;
}
 
return EXIT_SUCCESS;
}</syntaxhighlight>
 
=={{header|Common Lisp}}==
 
The common lisp version of the code is taken from the scala version with some modifications:
<langsyntaxhighlight lang="lisp">(defun farey (n)
(labels ((helper (begin end)
(let ((med (/ (+ (numerator begin) (numerator end))
Line 446 ⟶ 698:
 
 
;; Force printing of integers in X/1 format
(defun print-ratio (stream object &optional colonp at-sign-p)
(format stream "~d/~d" (numerator object) (denominator object)))
(loop for i from 1 to 11 do
(format t "~a: ~{~a/print-ratio/ ~}~%" i (farey i)))
 
(loop for i from 100 to 1001 by 100 do
(format t "Farey sequence of order ~a has ~a terms.~%" i (length (farey i))))
</syntaxhighlight>
</lang>
{{out}}
<pre>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
 
NIL
 
Farey sequence of order 100 has 3045 terms.
Line 477 ⟶ 731:
Farey sequence of order 900 has 246327 terms.
Farey sequence of order 1000 has 304193 terms.
 
NIL
</pre>
<math>Insert formula here</math>
 
=={{header|Crystal}}==
 
 
{{incorrect|Crystal| <br><br> The output for the first and last term &nbsp; (as per the task's requirement)
<br> is to show the first term as &nbsp; <big>'''0/1'''</big>,
<br> and to show the last term as &nbsp; <big>'''1/1'''</big>.
<br> Also, the first term is missing &nbsp; (in each Farey sequence). <br><br> }}
 
 
{{trans|the function from Lua, the output from Ruby.}}
Slow Version
<lang ruby>require "big"
<syntaxhighlight lang="ruby">require "big"
 
def farey (n)
a, b, c, d = 0, 1, 1, n
fracs = [] of BigRational
fracs << BigRational.new(0,1)
while c <= n
k = (n + b) // d
a, b, c, d = c, d, k * c - a, k * d - b
fracs << BigRational.new(a,b)
Line 507 ⟶ 752:
puts "Farey sequence for order 1 through 11 (inclusive):"
(1..11).each do |n|
puts "F(#{n}): " +0/1 #{farey(n)[1..-2].join(", ")} 1/1"
end
 
Line 514 ⟶ 759:
puts "F(%4d) =%7d" % [i, farey(i).size]
end
</syntaxhighlight>
</lang>
 
{{trans|the function from Python, the output from Ruby.}}
Fast Version
<syntaxhighlight lang="ruby">require "big"
 
def farey(n, length = false)
a = [] of BigRational
if length
(n*(n+3))//2 - (2..n).sum{ |k| farey(n//k, true).as(Int32) }
else
(1..n).each{ |k| (0..k).each{ |m| a << BigRational.new(m,k) } }; a.uniq.sort
end
end
 
puts "Farey sequence for order 1 through 11 (inclusive):"
(1..11).each do |n|
puts "F(#{n}): 0/1 #{farey(n).as(Array(BigRational))[1..-2].join(" ")} 1/1"
end
 
puts "Number of fractions in the Farey sequence:"
(100..1000).step(100) do |i|
puts "F(%4d) =%7d" % [i, farey(i, true)]
end
</syntaxhighlight>
 
{{out}}
<pre>
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) = 30443045
F( 200) = 1223212233
F( 300) = 2739827399
F( 400) = 4867848679
F( 500) = 7611676117
F( 600) = 109500109501
F( 700) = 149018149019
F( 800) = 194750194751
F( 900) = 246326246327
F(1000) = 304192304193
</pre>
 
Line 544 ⟶ 814:
 
 
 
{{improve|D| <br><br> The output for the first and last term &nbsp; (as per the task's requirement)
This should import a slightly modified version of the module from the Arithmetic/Rational task renamed here as arithmetic_rational2.d and where toString() is redefined as follows
<br> is to show the first term as &nbsp; <big>'''0/1'''</big>,
 
<br> and to show the last term as &nbsp; <big>'''1/1'''</big>. <br><br> }}
<syntaxhighlight lang="d"> string toString() const /*pure nothrow*/ {
if (den != 0)
//return num.text ~ (den == 1 ? "" : "/" ~ den.text);
return num.text ~ "/" ~ den.text;
if (num == 0)
return "NaRat";
else
return ((num < 0) ? "-" : "+") ~ "infRat";
}
</syntaxhighlight>
 
 
 
 
 
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, arithmetic_rational2;
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 {
Line 564 ⟶ 846:
writeln("\nFarey sequence fractions, 100 to 1000 by hundreds:\n",
iota(100, 1_001, 100).map!(i => i.farey.walkLength));
}</langsyntaxhighlight>
{{out}}
<pre>Farey sequence for order 1 through 11:
[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]
 
Farey sequence fractions, 100 to 1000 by hundreds:
Line 585 ⟶ 867:
This is as fast as the C entry (total run-time is 0.20 seconds).
{{trans|C}}
<langsyntaxhighlight lang="d">import core.stdc.stdio: printf, putchar;
 
void farey(in uint n) nothrow @nogc {
Line 637 ⟶ 919:
immutable uint n = 10_000_000;
printf("\n%u: %llu items\n", n, fareyLength(n, cache));
}</langsyntaxhighlight>
{{out}}
<pre>1: 0/1 1/1
Line 662 ⟶ 944:
 
10000000: 30396356427243 items</pre>
 
=={{header|Delphi}}==
See [https://www.rosettacode.org/wiki/Farey_sequence#Pascal Pascal].
 
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight lang="easylang">
proc farey n . .
b = 1 ; c = 1 ; d = n
write n & ": "
repeat
if n <= 11
write " " & a & "/" & b
.
until c > n
k = (n + b) div d
aa = c ; bb = d ; cc = k * c - a ; dd = k * d - b
a = aa ; b = bb ; c = cc ; d = dd
items += 1
.
if n > 11
print items & " items"
else
print ""
.
.
for i = 1 to 11
farey i
.
for i = 100 step 100 to 1000
farey i
.
</syntaxhighlight>
 
 
=={{header|EchoLisp}}==
 
 
{{improveincorrect|EchoLisp| <br><br> The output for the first and last term &nbsp; (as per the task's requirement)
<br> is to show the first term as &nbsp; <big>'''0/1'''</big>,
<br> and to show the last term as &nbsp; <big>'''1/1'''</big>. <br><br> }}
 
 
<langsyntaxhighlight lang="scheme">
(define distinct-divisors
(compose make-set prime-factors))
Line 692 ⟶ 1,008:
(make-set (for*/list ((n N) (d (in-range n N))) (rational n d))))
 
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
(for ((n (in-range 1 12))) ( printf "F(%d) %s" n (Farey n)))
F(1) { 0 1 }
Line 723 ⟶ 1,039:
|F(10000)| = 30397487
|F(100000)| = 3039650755
</syntaxhighlight>
</lang>
 
=={{header|EDSAC order code}}==
Line 734 ⟶ 1,050:
The code is slightly simplified by adding a formal term -1/0 before the first term 0/1.
The second term can then be included in the calculation loop.
<langsyntaxhighlight lang="edsac">
[Farey sequence for Rosetta Code website.
EDSAC program, Initial Orders 2.
Line 860 ⟶ 1,176:
E 27 Z [define start of execution]
P F [start with accumulator = 0]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 878 ⟶ 1,194:
=== Count terms ===
Counts the terms by summing Euler's totient function.
<langsyntaxhighlight lang="edsac">
[Farey sequence for Rosetta Code website.
Get number of terms by using Euler's totient function.
Line 1,106 ⟶ 1,422:
E 32 Z [define start of execution]
P F [start with accumulator = 0]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,121 ⟶ 1,437:
1000 304193
</pre>
 
 
=={{header|Factor}}==
Factor's <code>ratio</code> type automatically reduces fractions such as <code>0/1</code> and <code>1/1</code> to integers, so we print those separately at the beginning and ending of every sequence. This implementation makes use of the algorithm for calculating the next term from the wiki page [https://en.wikipedia.org/wiki/Farey_sequence#Next_term]. It also makes use of Euler's totient function for recursively calculating the length [https://en.wikipedia.org/wiki/Farey_sequence#Sequence_length_and_index_of_a_fraction].
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.primes.factors math.ranges
locals prettyprint sequences sequences.extras sets tools.time ;
IN: rosetta-code.farey-sequence
Line 1,155 ⟶ 1,470:
: main ( -- ) [ part1 part2 nl ] time ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,184 ⟶ 1,499:
 
</pre>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Farey_sequence this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 05-04-2017
' compile with: fbc -s console
 
Line 1,247 ⟶ 1,554:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>F1 = 0/1 1/1
Line 1,274 ⟶ 1,581:
=={{header|FunL}}==
Translation of Python code at [http://en.wikipedia.org/wiki/Farey_sequence#Next_term].
<langsyntaxhighlight lang="funl">def farey( n ) =
res = seq()
a, b, c, d = 0, 1, 1, n
Line 1,288 ⟶ 1,595:
 
for i <- 100..1000 by 100
println( "$i: ${farey(i).length()}" )</langsyntaxhighlight>
 
{{out}}
Line 1,315 ⟶ 1,622:
1000: 304193
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn FareySequence( n as long, descending as BOOL )
long a = 0, b = 1, c = 1, d = n, k = 0
long aa, bb, cc, dd
long count = 0
if descending = YES
a = 1
c = n -1
end if
count++
if n < 12 then print a; "/"; b; " ";
while ( (c <= n) and not descending ) or ( (a > 0) and descending)
aa = a
bb = b
cc = c
dd = d
k = int( (n + b) / d )
a = cc
b = dd
c = k * cc - aa
d = k * dd - bb
count++
if n < 12 then print a;"/"; b; " ";
wend
if n < 12 then print else print count
end fn
 
long i
 
for i = 1 to 11
if i < 10 then printf @" F%ld = \b", i else printf @"F%ld = \b", i
fn FareySequence( i, NO )
next
print
for i = 100 to 1000 step 100
if i < 1000 then printf @" F%ld = \b", i else printf @"F%ld = \b", i
fn FareySequence( i, NO )
next
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
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
F200 = 12233
F300 = 27399
F400 = 48679
F500 = 76117
F600 = 109501
F700 = 149019
F800 = 194751
F900 = 246327
F1000 = 304193
</pre>
 
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Farey_sequence}}
 
'''Solution'''
 
[[File:Fōrmulæ - Farey sequence 01.png]]
 
'''Case 1.''' Compute and show the Farey sequence for orders 1 through 11 (inclusive).
 
[[File:Fōrmulæ - Farey sequence 02.png]]
 
[[File:Fōrmulæ - Farey sequence 03.png]]
 
'''Case 2.''' Compute and display the number of fractions in the Farey sequence for order 100 through 1,000 (inclusive) by hundreds.
 
[[File:Fōrmulæ - Farey sequence 04.png]]
 
[[File:Fōrmulæ - Farey sequence 05.png]]
 
=={{header|Gambas}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Function farey(n As Long, descending As Long) As Long
Dim a, b, c, d, k As Long
Dim aa, bb, cc, dd, count As Long
b = 1
c = 1
d = n
count = 0
If descending = True Then
a = 1
c = n - 1
End If
count += 1
If n < 12 Then Print Str(a); "/"; Str(b); " ";
While ((c <= n) And Not descending) Or ((a > 0) And descending)
aa = a
bb = b
cc = c
dd = d
k = (n + b) \ d
a = cc
b = dd
c = k * cc - aa
d = k * dd - bb
count += 1
If n < 12 Then Print Str(a); "/"; Str(b); " ";
Wend
If n < 12 Then Print
Return count
End Function
 
Public Sub Main()
Dim i As Long
For i = 1 To 11
Print "F"; Str(i); " = ";
farey(i, False)
Next
Print
For i = 100 To 1000 Step 100
Print "F"; Str(i); IIf(i <> 1000, " ", ""); " = "; Format$(farey(i, False), "######")
Next
End </syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,377 ⟶ 1,835:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>F(1): 0/1 1/1
Line 1,401 ⟶ 1,859:
|F(1000)|: 304193
</pre>
 
 
=={{header|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.
<langsyntaxhighlight Haskelllang="haskell">import Data.List (unfoldr, mapAccumR)
import Data.Ratio ((%), denominator, numerator)
import Text.Printf (PrintfArg, printf)
Line 1,438 ⟶ 1,895:
showFracs =
unwords .
map (concat . (<*>) [show . numerator, const "/", show . denominator] . pure)
map
(concat . ([show . numerator, const "/", show . denominator] <*>) . pure)
 
main :: IO ()
Line 1,446 ⟶ 1,902:
fprint "%2d %s\n" $ fareys showFracs [1 .. 11]
putStrLn "\nSequence Lengths\n"
fprint "%4d %d\n" $ fareys length [100,200 .. 1000]</langsyntaxhighlight>
Output:
<pre>Farey Sequences
Line 1,477 ⟶ 1,933:
=={{header|J}}==
 
'''Solution:'''
<syntaxhighlight lang="j">Farey=: x:@/:~@(0 , ~.)@(#~ <:&1)@:,@(%/~@(1 + i.)) NB. calculates Farey sequence
displayFarey=: ('r/' charsub '0r' , ,&'r1')@": NB. format character representation of Farey sequence according to task requirements
order=: ': ' ,~ ": NB. decorate order of Farey sequence</syntaxhighlight>
 
'''Required examples:'''
{{improve|J| <br><br> The output for the first and last term &nbsp; (as per the task's requirement)
<br> is to show the first term as &nbsp; <big>'''0/1'''</big>,
<br> and to show the last term as &nbsp; <big>'''1/1'''</big>.
<br> Also, please ''translate'' the &nbsp; '''r''' &nbsp; character to a solidus if possible. <br><br> }}
 
<syntaxhighlight lang="j"> LF joinstring (order , displayFarey@Farey)&.> 1 + i.11 NB. Farey sequences, order 1-11
 
1: 0/0 1/1
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.
2: 0/0 1/2 1/1
 
3: 0/0 1/3 1/2 2/3 1/1
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.
4: 0/0 1/4 1/3 1/2 2/3 3/4 1/1
 
5: 0/0 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
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.)
6: 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
 
7: 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
<lang J>Farey=:3 :0
8: 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
0,/:~~.(#~ <:&1),%/~1x+i.y
9: 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
)</lang>
10: 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
 
11: 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
Required examples:
LF joinstring (order , ":@#@Farey)&.> 100 * 1 + i.10 NB. Count of Farey sequence items, order 100,200,..1000
 
100: 3045
<lang J> Farey 1
200: 12233
0 1
300: 27399
Farey 2
400: 48679
0 1r2 1
500: 76117
Farey 3
600: 109501
0 1r3 1r2 2r3 1
700: 149019
Farey 4
800: 194751
0 1r4 1r3 1r2 2r3 3r4 1
900: 246327
Farey 5
1000: 304193</syntaxhighlight>
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.
 
<strike>Even if it is 20 times faster, the response time is just acceptable.</strike>
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>
 
{{out}}
<pre>
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
</pre>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
This example uses the fact that it generates the fraction candidates from the bottom up as well as <code>Set</code>'s internal duplicate removal (based on <code>Comparable.compareTo</code>) to get rid of un-reduced fractions. It also uses <code>TreeSet</code> to sort based on the value of the fraction.
<langsyntaxhighlight lang="java5">import java.util.TreeSet;
 
public class Farey{
Line 1,637 ⟶ 2,009:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>F1: [0/1, 1/1]
Line 1,660 ⟶ 2,032:
F900: 246327 members
F1000: 304193 members</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
 
This solution uses two jq functions from the [[Arithmetic/Rational#jq]] page;
they can be included using jq's `include` directive as shown here.
<syntaxhighlight lang=jq>
include "rational" ; # actually, only `r/2` and `gcd/2` are actually needed
 
# Emit an ordered stream of the Farey sequence of order $order
# by recursively generating the mediants
def FS($order):
def f($l; $r; $n):
r($l.n + $r.n; $l.d + $r.d) as $m
| select($m.d <= $n)
| f($l; $m; $n), $m, f($m; $r; $n);
 
r(0;1) as $l
| r(1;1) as $r
| $l, f($l; $r; .), $r;
 
# Pretty-print Farey sequences of order $min up to and including order $max
def FareySequences($min; $max):
def rpp: "\(.n)/\(.d)";
def pp(s): [s|rpp] | join(" ");
range($min;$max+1)
| "F(\(.)): " + pp(FS(.));
 
# Use `count/1` for counting to save space
def count(s): reduce s as $_ (0; .+1);
def FareySequenceMembers($N):
count(FS($N));
 
# The tasks:
FareySequences(1;11),
"",
(range(100; 1001; 100) | "F\(.): \(FareySequenceMembers(.)|length) members" )
</syntaxhighlight>
''Invocation'': jq -r
<pre>
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
 
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
</pre>
 
=={{header|Julia}}==
{{trans|Java}}
<langsyntaxhighlight lang="julia">using DataStructures
 
function farey(n::Int)::OrderedSet{Rational}
rst = OrderedSetSortedSet{Rational}(Rational[0, 1])
for den in 1:n, num in 1:den-1
push!(rst, Rational(num, den))
Line 1,683 ⟶ 2,119:
for n in 100:100:1000
println("F_$n has ", length(farey(n)), " fractions")
end</langsyntaxhighlight>
 
{{out}}
Line 1,709 ⟶ 2,145:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1
 
fun farey(n: Int): List<String> {
Line 1,736 ⟶ 2,172:
for (i in 100..1000 step 100)
println("${"%4d".format(i)}: ${"%6d".format(farey(i).size)} fractions")
}</langsyntaxhighlight>
 
{{out}}
Line 1,762 ⟶ 2,198:
900: 246327 fractions
1000: 304193 fractions
</pre>
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .farey = fn(.n) {
var .a, .b, .c, .d = 0, 1, 1, .n
while[=[[0, 1]]] .c <= .n {
val .k = (.n + .b) // .d
.a, .b, .c, .d = .c, .d, .k * .c - .a, .k * .d - .b
_while ~= [[.a, .b]]
}
}
 
val .testFarey = impure fn() {
writeln "Farey sequence for orders 1 through 11"
for .i of 11 {
writeln "{{.i:2}}: ", join " ", map(fn .f: "{{.f[1]}}/{{.f[2]}}", .farey(.i))
}
}
 
.testFarey()
 
writeln()
writeln "count of Farey sequence fractions for 100 to 1000 by hundreds"
for .i = 100; .i <= 1000; .i += 100 {
writeln $"\.i:4;: ", len(.farey(.i))
}</syntaxhighlight>
 
{{out}}
<pre>Farey sequence for orders 1 through 11
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
 
count of Farey sequence fractions for 100 to 1000 by hundreds
100: 3045
200: 12233
300: 27399
400: 48679
500: 76117
600: 109501
700: 149019
800: 194751
900: 246327
1000: 304193
</pre>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Return farey sequence of order n
function farey (n)
local a, b, c, d, k = 0, 1, 1, n
Line 1,783 ⟶ 2,271:
print()
end
for i = 100, 1000, 100 do print(i .. ": " .. #farey(i) .. " items") end</langsyntaxhighlight>
{{out}}
<pre>1: 0/1 1/1
Line 1,808 ⟶ 2,296:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">#Displays terms in Farey_sequence of order n
farey_sequence := proc(n)
local a,b,c,d,k;
a,b,c,d := 0,1,1,n;
printf("%d/%d", a,b);
while( c <= n) do
k := trunc(iquo(n+b)/,d);
a,b,c,d := c,d,c*k-a,d*k-b;
printf(", %d/%d", a,b);
end do;
printf("\n");
end proc;:
 
#Returns the length of a Farey sequence
farey_len := proc(n)
return 1 + add(NumberTheory:-Totient(k), k=1..n);
end proc;
 
for i to 11 do
farey_sequence(i);
end do;
printf("\n");
for j from 100 to 1000 by 100 do
printf("%d\n", farey_len(j));
end do;</langsyntaxhighlight>
 
{{Out|Output}
{{Out|Output}}
<pre>
0/1, 1/1
Line 1,859 ⟶ 2,348:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
FareySequence is a built-in command in the Wolfram Language. However, we have to reformat the output to match the requirements.
<syntaxhighlight lang="mathematica">farey[n_]:=StringJoin@@Riffle[ToString@Numerator[#]<>"/"<>ToString@Denominator[#]&/@FareySequence[n],", "]
TableForm[farey/@Range[11]]
Table[Length[FareySequence[n]], {n, 100, 1000, 100}]</syntaxhighlight>
{{out}}
<pre>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
 
{3045, 12233, 27399, 48679, 76117, 109501, 149019, 194751, 246327, 304193}</pre>
 
=={{header|MATLAB}}==
{{improve|Mathematica| <br><br> The output for the first and last term &nbsp; (as per the task's requirement)
{{trans|Kotlin}}
<br> is to show the first term as &nbsp; <big>'''0/1'''</big>,
<syntaxhighlight lang="MATLAB">
<br> and to show the last term as &nbsp; <big>'''1/1'''</big>. <br><br> }}
clear all;close all;clc;
 
% Print Farey sequences for 1 to 11
for i = 1:11
farey_sequence = farey(i);
fprintf('%2d: %s\n', i, strjoin(farey_sequence, ' '));
end
fprintf('\n');
% Print the number of fractions in Farey sequences for 100 to 1000 step 100
for i = 100:100:1000
farey_sequence = farey(i);
fprintf('%4d: %6d fractions\n', i, length(farey_sequence));
end
 
function farey_sequence = farey(n)
FareySequence is a built-in command in the Wolfram Language
a = 0;
<lang Mathematica>FareySequence /@ Range[11]
b = 1;
Table[Length[FareySequence[n]], {n, 100, 1000, 100}]</lang>
c = 1;
d = n;
farey_sequence = {[num2str(a) '/' num2str(b)]}; % Initialize the sequence with "0/1"
while c <= n
k = fix((n + b) / d);
aa = a;
bb = b;
a = c;
b = d;
c = k * c - aa;
d = k * d - bb;
farey_sequence{end+1} = [num2str(a) '/' num2str(b)]; % Append the fraction to the sequence
end
end
</syntaxhighlight>
{{out}}
<pre>
<pre>{{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},
1: 0/1 1/1
{0, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1},
2: 0/1 1/2 1/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},
3: 0/1 1/3 1/2 2/3 1/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},
4: {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, 41/5, 5/6, 6/7, 7/8, 1},
5: {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, 51/6, 6/7, 7/8, 8/9, 1},
6: {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, 61/7, 7/8, 8/9, 9/10, 1},
7: {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, 101/11,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 fractions
200: 12233 fractions
300: 27399 fractions
400: 48679 fractions
500: 76117 fractions
600: 109501 fractions
700: 149019 fractions
800: 194751 fractions
900: 246327 fractions
1000: 304193 fractions
</pre>
 
 
{3045, 12233, 27399, 48679, 76117, 109501, 149019, 194751, 246327, 304193}</pre>
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
farey(n):=if n=1 then ["0/1","1/1"] else block(
create_list([i,j],i,0,n-1,j,1,n),
map(lambda([x],if x[1]=0 and x[2]#1 then false else if x[1]=x[2] and x[1]#1 then false else if x[1]<=x[2] then x),%%),
delete(false,%%),
unique(map(lambda([x],x[1]/x[2]),%%)),
append(rest(append(["0/1"],rest(%%)),-1),["1/1"])
)$
 
/* Test cases */
/* Sequences from order 1 through 11 */
farey(1);
farey(2);
farey(3);
farey(4);
farey(5);
farey(6);
farey(7);
farey(8);
farey(9);
farey(10);
farey(11);
 
/* Length of sequences from order 100 through, 1000 by hundreds */
length(farey(100));
length(farey(200));
length(farey(300));
length(farey(400));
length(farey(500));
length(farey(600));
length(farey(700));
length(farey(800));
length(farey(900));
length(farey(1000));
</syntaxhighlight>
{{out}}
<pre>
["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"]
 
3045
12233
27399
48679
76117
109501
149019
194751
246327
304193
</pre>
 
=={{header|Nim}}==
{{trans|D}}
<langsyntaxhighlight Nimlang="nim">import strformat
 
proc farey(n: int) =
Line 1,929 ⟶ 2,539:
 
let n = 10_000_000
echo fmt"{n}: {fareyLength(n, cache):14} items"</langsyntaxhighlight>
{{out}}
<pre>
Line 1,957 ⟶ 2,567:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight 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));
fmt(n)=if(denominator(n)>1,n,Str(n,"/1"));
for(n=1,11,print(Farey(n)))
for(n=1,11,print(apply(fmt, Farey(n))))
apply(countFarey, 100*[1..10])</lang>
apply(countFarey, 100*[1..10])</syntaxhighlight>
{{out}}
<pre>["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"]
 
%1 = [3045, 12233, 27399, 48679, 76117, 109501, 149019, 194751, 246327, 304193]</pre>
 
=={{header|Pascal}}==
 
{{incorrect|Pascal| <br><br> Some of the output is missing (as per the task's requirements.
<br><br> Compute and show the Farey sequence for orders 1 through 11 (inclusive).
<br> Compute and display the number of fractions in the Farey sequence for
order 100 through 1,000 (inclusive) by hundreds. <br><br> }}
 
 
Using a function, to get next in Farey sequence. calculated as stated in wikipedia article, see Lua [[http://rosettacode.org/wiki/Farey_sequence#Lua]].
So there is no need to store them in a big array..
<langsyntaxhighlight lang="pascal">program Farey;
{$IFDEF FPC }{$MODE DELPHI}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
Line 1,994 ⟶ 2,598:
nom,dom,n,c,d: longInt;
end;
function InitFarey(maxdom:longINt):tNextFarey;
Begin
Line 2,003 ⟶ 2,607:
end;
end;
 
function NextFarey(var fn:tNextFarey):boolean;
var
Line 2,017 ⟶ 2,621:
end;
 
procedure CheckFareyCount( num: NativeUint);
var
TestF : tNextFarey;
cnt : NativeIntNativeUint;
Begin
TestF:= InitFarey(10num);
cnt := 1;// out of InitFarey
repeat
write(TestF.nom,'/',TestF.dom,',');
inc(cnt);
until NOT(NextFarey(TestF));
writeln(TestF.nom,'/',TestF.dom);
writeln('F(',TestF.n:4,') = ',cnt:7);
end;
 
var
TestF:= InitFarey(10000);
cntTestF := 1tNextFarey;
cnt: NativeInt;
Begin
Writeln('Farey sequence for order 1 through 11 (inclusive): ');
 
For cnt := 1 to 11 do
Begin
TestF:= InitFarey(cnt);
write('F(',cnt:2,') = ');
repeat
write(TestF.nom,'/',TestF.dom,',');
until NOT(NextFarey(TestF));
writeln(TestF.nom,'/',TestF.dom);
end;
writeln;
writeln('Number of fractions in the Farey sequence:');
cnt := 100;
repeat
incCheckFareyCount(cnt);
inc(cnt,100);
until NOT(NextFarey(TestF));
until cnt > 1000;
writeln(TestF.n:10,cnt:16);
end.</langsyntaxhighlight>
{{Out}}
<pre style ="horizontal=85">
<pre> 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
Farey sequence for order 1 through 11 (inclusive):
,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( 1) = 0/1,1/1
33
F( 2) = 0/1,1/2,1/1
10000 30397487
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:
real 0m0.331s</pre>
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</pre>
 
=={{header|Perl}}==
Line 2,050 ⟶ 2,691:
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.
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use warnings;
use strict;
use Math::BigRat;
Line 2,078 ⟶ 2,719:
for (1 .. 10, 100000) {
print "F${_}00: ", farey_count(100*$_), " members\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,106 ⟶ 2,747:
 
=== Mapped Rationals ===
Similar to Pari and Perl6Raku. Same output, quite slow. Using the recursive formula for the count, utilizing the Memoize module, would be a big help.
<langsyntaxhighlight lang="perl">use warnings;
use strict;
use Math::BigRat;
Line 2,133 ⟶ 2,774:
my @f = farey(100*$_);
print "F${_}00: ", scalar(@f), " members\n";
}</langsyntaxhighlight>
 
=={{header|Perl 6}}==
{{Works with|rakudo|2018.10}}
<!-- bug bites at farey(362): sub farey ($order) { unique 0/1, |(1..$order).map: { |(1..$^d).map: { $^n/$d } } } -->
 
<lang perl6>sub farey ($order) {
my @l = 0/1, 1/1;
(2..$order).map: { push @l, |(1..$^d).map: { $_/$d } }
unique @l
}
 
say "Farey sequence order ";
.say for (1..11).hyper(:1batch).map: { "$_: ", .&farey.sort.map: *.nude.join('/') };
.say for (100, 200 ... 1000).race(:1batch).map: { "Farey sequence order $_ has " ~ [.&farey].elems ~ ' elements.' }</lang>
{{out}}
<pre>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.</pre>
 
=={{header|Phix}}==
{{trans|AWK}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function farey(integer n)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer a=0, b=1, c=1, d=n
<span style="color: #008080;">function</span> <span style="color: #000000;">farey</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
integer items=1
<span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">items</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span>
if n<=11 then
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">11</span> <span style="color: #008080;">then</span>
printf(1,"%d: %d/%d",{n,a,b})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d: %d/%d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
while c<=n do
<span style="color: #008080;">while</span> <span style="color: #000000;">c</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
integer k = floor((n+b)/d)
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
{a,b,c,d} = {c,d,k*c-a,k*d-b}
<span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c</span><span style="color: #0000FF;">-</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">*</span><span style="color: #000000;">d</span><span style="color: #0000FF;">-</span><span style="color: #000000;">b</span><span style="color: #0000FF;">}</span>
items += 1
<span style="color: #000000;">items</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if n<=11 then
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">11</span> <span style="color: #008080;">then</span>
printf(1," %d/%d",{a,b})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %d/%d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
return items
<span style="color: #008080;">return</span> <span style="color: #000000;">items</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
printf(1,"Farey sequence for order 1 through 11:\n")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Farey sequence for order 1 through 11:\n"</span><span style="color: #0000FF;">)</span>
for i=1 to 11 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">11</span> <span style="color: #008080;">do</span>
{} = farey(i)
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">farey</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
printf(1,"\n")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"Farey sequence fractions, 100 to 1000 by hundreds:\n")
<span style="color: #004080;">sequence</span> <span style="color: #000000;">nf</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span><span style="color: #000000;">farey</span><span style="color: #0000FF;">)</span>
sequence nf = {}
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Farey sequence fractions, 100 to 1000 by hundreds:\n%v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nf</span><span style="color: #0000FF;">})</span>
for i=100 to 1000 by 100 do
<!--</syntaxhighlight>-->
nf = append(nf,farey(i))
end for
?nf</lang>
{{out}}
<pre>
Line 2,219 ⟶ 2,821:
{3045,12233,27399,48679,76117,109501,149019,194751,246327,304193}
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go ?=>
member(N,1..11),
Farey = farey(N),
println(N=Farey),
fail,
nl.
go => true.
 
farey(N) = M =>
M1 = [0=$(0/1)] ++
[I2/J2=$(I2/J2) : I in 1..N, J in I..N,
GCD=gcd(I,J),I2 =I//GCD,J2=J//GCD].sort_remove_dups(),
M = [ E: _=E in M1]. % extract the rational representation
 
</syntaxhighlight>
 
{{out}}
<pre>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]
 
</pre>
 
The number of fractions of order 100..100..1000:
<syntaxhighlight lang="picat">go2 =>
foreach(N in 100..100..1000)
F = farey(N),
println(N=F.length)
end,
nl.</syntaxhighlight>
 
{{out}}
<pre>100 = 3045
200 = 12233
300 = 27399
400 = 48679
500 = 76117
600 = 109501
700 = 149019
800 = 194751
900 = 246327
1000 = 304193</pre>
 
=={{header|Prolog}}==
The following uses SWI-Prolog's rationals (rdiv(p,q)) and assumes the availability of predsort/3.
The presentation is top-down.
<langsyntaxhighlight Prologlang="prolog">task(1) :-
between(1, 11, I),
farey(I, F),
Line 2,251 ⟶ 2,905:
rcompare(<, A, B) :- A < B, !.
rcompare(>, A, B) :- A > B, !.
rcompare(=, A, B) :- A =< B.</langsyntaxhighlight>
Interactive session:<pre>?- task(1).
1: 0/1, 1/1
Line 2,280 ⟶ 2,934:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">EnableExplicit
 
Structure farey_struc
Line 2,364 ⟶ 3,018:
order+v_step
Wend
Input()</langsyntaxhighlight>
{{out}}
<pre>Input-> start end step [start>=1; end<=1000; step>=1; (start<end)] : 1 12 1
Line 2,479 ⟶ 3,133:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from fractions import Fraction
 
 
Line 2,499 ⟶ 3,153:
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)])</langsyntaxhighlight>
 
{{out}}
Line 2,520 ⟶ 3,174:
And as an alternative to importing the Fraction library, we can also sketch out a Ratio type of our own:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Farey sequence'''
 
from itertools import (chain, count, islice)
Line 2,717 ⟶ 3,371:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Farey sequence for orders 1-11 (inclusive):
Line 2,745 ⟶ 3,399:
900 -> 246327
1000 -> 304193</pre>
 
=={{header|Quackery}}==
 
Uses the BIGnum RATional arithmetic library included with Quackery, ''bigrat.qky''.
 
<syntaxhighlight lang="quackery"> [ $ "bigrat.qky" loadfile ] now!
 
[ rot + dip + reduce ] is mediant ( n/d n/d --> n/d )
 
[ 1+ temp put [] swap
dup size 1 - times
[ dup i^ peek
rot over nested join
unrot over i^ 1+ peek
join do mediant
dup temp share < iff
[ join nested
rot swap join
swap ]
else 2drop ]
drop
' [ [ 1 1 ] ] join
temp release ] is nextfarey ( fy n --> fy )
 
[ witheach
[ unpack vulgar$
echo$ sp ] ] is echofarey ( fy --> )
[ 0 swap dup times
[ i over gcd
1 = rot + swap ]
drop ] is totient ( n --> n )
 
[ 0 swap times
[ i 1+ totient + ] ] is totientsum ( n --> n )
[ totientsum 1+ ] is fareylength ( n --> n )
 
say "First eleven Farey series:" cr
' [ [ 0 1 ] [ 1 1 ] ]
10 times
[ dup echofarey cr
i^ 2 + nextfarey ]
echofarey cr
cr
say "Length of Farey series 100, 200 ... 1000: "
[] 10 times
[ i^ 1+ 100 *
fareylength join ]
echo</syntaxhighlight>
 
{{Out}}
 
<pre>First eleven Farey series:
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
 
Length of Farey series 100, 200 ... 1000: [ 3045 12233 27399 48679 76117 109501 149019 194751 246327 304193 ]</pre>
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">
farey <- function(n, length_only = FALSE) {
a <- 0
Line 2,783 ⟶ 3,504:
farey(i, length_only = TRUE)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,808 ⟶ 3,529:
1000: 304193 items
</pre>
 
=={{header|Racket}}==
 
Once again, racket's ''math/number-theory'' package comes to the rescue!
<langsyntaxhighlight lang="racket">#lang racket
(require math/number-theory)
(define (display-farey-sequence order show-fractions?)
Line 2,830 ⟶ 3,552:
; 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))</langsyntaxhighlight>
 
{{out}}
Line 2,865 ⟶ 3,587:
-- Farey Sequence for order 900 has 246327 fractions
-- Farey Sequence for order 1000 has 304193 fractions</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.10}}
<!-- bug bites at farey(362): sub farey ($order) { unique 0/1, |(1..$order).map: { |(1..$^d).map: { $^n/$d } } } -->
 
<syntaxhighlight lang="raku" line>sub farey ($order) {
my @l = 0/1, 1/1;
(2..$order).map: { push @l, |(1..$^d).map: { $_/$d } }
unique @l
}
 
say "Farey sequence order ";
.say for (1..11).hyper(:1batch).map: { "$_: ", .&farey.sort.map: *.nude.join('/') };
.say for (100, 200 ... 1000).race(:1batch).map: { "Farey sequence order $_ has " ~ [.&farey].elems ~ ' elements.' }</syntaxhighlight>
{{out}}
<pre>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.</pre>
 
=={{header|REXX}}==
<langProgramming rexx>/*REXXnote: program&nbsp; if computesthe and1<sup>st</sup> displaysargument is anegative, &nbsp; Fareythen sequence (oronly the numbercount of the fractions). */is shown.
<syntaxhighlight lang="rexx">/*REXX program computes and displays a Farey sequence (or the number of fractions). */
parse arg L H I . /*obtain optional arguments from the CL*/
ifparse arg LO HI INC . L=='' | L=="," then L=5 /*Notobtain specified?optional arguments Then usefrom the default.CL*/
oldL=L if LO=='' | LO=="," then LO= 1 /*originalNot specified? L Then use (negativity=nothe displaydefault.*/
L=abs(L)if HI=='' | HI=="," then HI= LO /* " " " /*but ··· use" │L│" for all else." */
if HINC=='' | HINC=="," then HINC=L 1 /* " /*Not specified?" Then use the default. " " " " */
if Isw=='' |linesize() I==","- then I=1 /* " " " " " /*obtain the "linesize of the terminal. */
oLO= LO /*stepsave throughoriginal thevalue rangeof bythe increment.the orders*/
do nj=Labs(LO) to Habs(HI) by I INC /*process theeach rangeof (couldthe bespecified only one)numbers*/
@ #= fareyF(nj); #=' 'words(@)" " /*go ye forth and& compute Farey numberssequence.*/
say center('Farey sequence for order ' n j " has " # ' fractions.', 150sw, "═")
if oldL<oLO>=0 then iterate call show /*don't display the Farey fractions. if neg.*/
end /*j*/
say @; say /*show Farey fractions and a blank line*/
exit # end /*n*/ /*stick [↑]a fork build/displayin it, we're Fareyall fractionsdone. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
fareyF: procedure; parse arg x; expose n.1=0; d.1=1; n.2=1;parse arg d.2=x /*some kit parts.*/
$=n.1'/'= 0; d.1= 1; n.2"/"d.2 = 1; d.2= x /*a startersome kit parts for the Fareyfraction sequencelist.*/
do k=1 until n.z>x /*construct [↓]from thirds now, buildand on the starter kit"up". */
do jy= k+1; y=j+1; z=j k+2 /*constructcalculate fromthe thirdsnext K and the onnext "up"Z. */
n.z _= (d.jk + x) % d.y*n.y - n.j /* " /*calculation theused fractionas a numeratorshortcut. */
d n.z= (d.j+x)_ % d.y*dn.y - d.j - /* n.k " /* " " the fraction denominatornumerator. */
if nd.z>x= _ then leave% d.y*d.y - d.k /*Should the construction be stopped" " " denominator. ? */
$=$ if n.z'/'d.z >x then leave /*HeckShould no,the addconstruction fractionbe to party mix.stopped ? */
end /*j*/ /* [↑] construct the Farey sequence. k*/
return $ z - 1 /*return with the count of Farey fractions. */</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: $= '0/1' /*construct the start of the Farey seq.*/
do k=2 for #-1; _= n.k'/'d.k /*build a fraction: numer. / denom. */
if length($ _)>sw then do; say $; $= _; end /*Is new line too wide? Show it*/
else $= $ _ /*No? Keep it & keep building.*/
end /*k*/
if $\=='' then say $; return /*display any residual fractions. */</syntaxhighlight>
This REXX program makes use of &nbsp; '''linesize''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
 
Some REXXes don't have this BIF, so the &nbsp; '''linesize.rex''' &nbsp; REXX program is included here &nbsp; ──► &nbsp; [[LINESIZE.REX]]. <br>
 
{{out|output|text=&nbsp; when using the following for input: &nbsp; &nbsp; <tt> &nbsp; 1 &nbsp; 11 </tt>}}
 
Line 2,949 ⟶ 3,719:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Farey sequence
 
Line 2,997 ⟶ 3,767:
ok
return count
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,022 ⟶ 3,792:
F900 = 246327
F1000 = 304193
</pre>
 
=={{header|RPL}}==
{{trans|Lua}}
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ "'" OVER RE →STR + "/" + SWAP IM + STR→
≫ ''''C→EXP'''' STO
≪ → n
≪ { '0/1' }
0 1 R→C 1 n R→C
'''WHILE''' DUP RE n ≤ '''REPEAT'''
OVER IM n + OVER IM / FLOOR
OVER * ROT -
ROT 3 PICK '''C→EXP''' + ROT ROT
'''END'''
DROP2
≫ ≫ 'FAREY' STO
|
''' C→EXP''' ''( (a,b) -- 'a/b' )''
.
.
'''FAREY''' ''( n -- { f1..fk } ) ''
local farTab = { {0, 1} }
local a, b, c, d, k = 0, 1, 1, n
while c <= n do
k = math.floor((n + b) / d)
a, b, c, d = c, d, k * c - a, k * d - b
table.insert(farTab, {a, b})
end
return farTab - only
.
|}
{{in}}
<pre>
1 FAREY
11 FAREY
100 FAREY SIZE
</pre>
{{out}}
<pre>
3: { '0/1' '1/11' }
2: { '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' }
1: 3045
</pre>
To count the number of fractions above F(100), thus avoiding to handle huge lists in memory, the above code must be slightly changed into:
≪ → n
≪ 1
0 1 R→C 1 n R→C
'''WHILE''' DUP RE n ≤ '''REPEAT'''
OVER IM n + OVER IM / FLOOR
OVER * ROT -
ROT 1 + ROT ROT
'''END'''
DROP2
≫ ≫ '∑FAREY' STO
{{in}}
<pre>
≪ {} 100 700 FOR n n ∑FAREY + 100 STEP ≫ EVAL
≪ {} 800 1000 FOR n n ∑FAREY + 100 STEP ≫ EVAL
</pre>
{{out}}
<pre>
2: { 3045 12233 27399 48679 76117 109501 149019 }
1: { 194751 246327 304193 }
</pre>
 
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">def farey(n, length=false)
if length
(n*(n+3))/2 - (2..n).sum{|k| farey(n/k, true)}
Line 3,041 ⟶ 3,881:
for i in (100..1000).step(100)
puts "F(%4d) =%7d" % [i, farey(i, true)]
end</langsyntaxhighlight>
 
{{out}}
Line 3,068 ⟶ 3,908:
F( 900) = 246327
F(1000) = 304193
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">#[derive(Copy, Clone)]
struct Fraction {
numerator: u32,
denominator: u32,
}
 
use std::fmt;
 
impl fmt::Display for Fraction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.numerator, self.denominator)
}
}
 
impl Fraction {
fn new(n: u32, d: u32) -> Fraction {
Fraction {
numerator: n,
denominator: d,
}
}
}
 
fn farey_sequence(n: u32) -> impl std::iter::Iterator<Item = Fraction> {
let mut a = 0;
let mut b = 1;
let mut c = 1;
let mut d = n;
std::iter::from_fn(move || {
if a > n {
return None;
}
let result = Fraction::new(a, b);
let k = (n + b) / d;
let next_c = k * c - a;
let next_d = k * d - b;
a = c;
b = d;
c = next_c;
d = next_d;
Some(result)
})
}
 
fn main() {
for n in 1..=11 {
print!("{}:", n);
for f in farey_sequence(n) {
print!(" {}", f);
}
println!();
}
for n in (100..=1000).step_by(100) {
println!("{}: {}", n, farey_sequence(n).count());
}
}</syntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">
object FareySequence {
 
Line 3,099 ⟶ 4,022:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,129 ⟶ 4,052:
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme write))
Line 3,185 ⟶ 4,108:
(number->string (farey-sequence i #f))))
(newline))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,216 ⟶ 4,139:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func farey_count(n) { # A005728
1 + sum(1..n, {|k| euler_phi(k) })
}
Line 3,242 ⟶ 4,165:
for n in (100..1000 -> by(100)) {
say ("F(%4d) =%7d" % (n, farey_count(n)))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,273 ⟶ 4,196:
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">mata
function totient(n_) {
n = n_
Line 3,349 ⟶ 4,272:
 
map(&farey_length(),100*(1..10))
end</langsyntaxhighlight>
 
'''Output'''
Line 3,372 ⟶ 4,295:
=={{header|Swift}}==
Class with computed properties:
<langsyntaxhighlight lang="swift">class Farey {
let n: Int
 
Line 3,420 ⟶ 4,343:
let m = n * 100
print("\(m): \(Farey(m).sequence.count)")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,453 ⟶ 4,376:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc farey {n} {
Line 3,479 ⟶ 4,402:
for {set i 100} {$i <= 1000} {incr i 100} {
puts |F($i)|\x20=\x20[llength [farey $i]]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,505 ⟶ 4,428:
</pre>
 
=={{header|uBasic/4tH}}==
{{Trans|BASIC256}}
<syntaxhighlight lang="qbasic">For i = 1 To 11
Print "F"; i; " = ";
Proc _Farey(i, 0)
Next
 
Print
For i = 100 To 1000 Step 100
Print "F"; i;
If i # 1000 Then Print " ";
Print " = ";
Proc _Farey (i, 0)
Next
End
 
_Farey
Param (2)
Local (11)
 
c@ = 0 : d@ = 1 : e@ = 1 : f@ = a@ : g@ = 0
h@ = 0
 
If b@ = 1 Then c@ = 1 : e@ = a@ - 1
 
h@ = h@ + 1
If a@ < 12 Then Print c@; "/"; d@; " ";
Do While (((e@ > a@) = 0) * (b@ = 0)) + ((c@ > 0) * b@)
i@ = c@ : j@ = d@ : k@ = e@ : l@ = f@
m@ = (a@ + d@) / f@
h@ = h@ + 1
c@ = k@ : d@ = l@ : e@ = m@ * k@ - i@ : f@ = m@ * l@ - j@
If a@ < 12 Then Print c@; "/"; d@; " ";
Loop
 
If a@ < 12 Then
Print
Else
Print Using "______#"; h@
EndIf
Return</syntaxhighlight>
=={{header|Vala}}==
{{trans|Nim}}
<langsyntaxhighlight lang="vala">struct Fraction {
public uint d;
public uint n;
Line 3,558 ⟶ 4,524:
for (uint n = 100; n <= 1000; n += 100)
print("%8u: %14u items\n", n, fareyLength(n, cache));
}</langsyntaxhighlight>
 
{{out}}
Line 3,584 ⟶ 4,550:
1000: 304193 items
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="go">struct Frac {
num int
den int
}
fn (f Frac) str() string {
return "$f.num/$f.den"
}
fn f(l Frac, r Frac, n int) {
m := Frac{l.num + r.num, l.den + r.den}
if m.den <= n {
f(l, m, n)
print("$m ")
f(m, r, n)
}
}
fn main() {
// task 1. solution by recursive generation of mediants
for n := 1; n <= 11; n++ {
l := Frac{0, 1}
r := Frac{1, 1}
print("F($n): $l ")
f(l, r, n)
println(r)
}
// task 2. direct solution by summing totient fntion
// 2.1 generate primes to 1000
mut composite := [1001]bool{}
for p in [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
mut tot := [1001]int{init: 1}
for n := 2; n <= 1000; n++ {
if !composite[n] {
tot[n] = n - 1
for a := n * 2; a <= 1000; a += n {
mut 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 {
println("|F($n)|: $sum")
}
}
}</syntaxhighlight>
{{out}}
<pre>Same as Go entry</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-math}}
{{libheader|Wren-iterate}}
{{libheader|Wren-fmt}}
{{libheader|Wren-rat}}
<syntaxhighlight lang="wren">import "./math" for Int
import "./iterate" for Stepped
import "./fmt" for Fmt
import "./rat" for Rat
 
var f //recursive
f = Fn.new { |l, r, n|
var m = Rat.new(l.num + r.num, l.den + r.den)
if (m.den <= n) {
f.call(l, m, n)
System.write("%(m) ")
f.call(m, r, n)
}
}
 
/* Task 1: solution by recursive generation of mediants. */
for (n in 1..11) {
var l = Rat.zero
var r = Rat.one
System.write("F(%(n)): %(l) ")
f.call(l, r, n)
System.print(r)
}
System.print()
 
/* Task 2: direct solution by summing totient function. */
 
// generate primes to 1000
var comp = Int.primeSieve(1001, false)
 
// generate totients to 1000
var tot = List.filled(1001, 1)
for (n in 2..1000) {
if (!comp[n]) {
tot[n] = n - 1
for (a in Stepped.ascend(n*2..1000, n)) {
var f = n - 1
var r = (a/n).floor
while (r%n == 0) {
f = f * n
r = (r/n).floor
}
tot[a] = tot[a] * f
}
}
}
 
// sum totients
var sum = 1
for (n in 1..1000) {
sum = sum + tot[n]
if (n%100 == 0) System.print("F(%(Fmt.d(4, n))): %(Fmt.dc(7, sum))")
}</syntaxhighlight>
 
{{out}}
<pre>
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): 3,045
F( 200): 12,233
F( 300): 27,399
F( 400): 48,679
F( 500): 76,117
F( 600): 109,501
F( 700): 149,019
F( 800): 194,751
F( 900): 246,327
F(1000): 304,193
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">proc Farey(N); \Show Farey sequence for N
\Translation of Python program on Wikipedia:
int N, A, B, C, D, K, T;
[A:= 0; B:= 1; C:= 1; D:= N;
Text(0, "0/1");
while C <= N do
[K:= (N+B)/D;
T:= C;
C:= K*C - A;
A:= T;
T:= D;
D:= K*D - B;
B:= T;
ChOut(0, ^ ); IntOut(0, A);
ChOut(0, ^/); IntOut(0, B);
];
];
 
func GCD(N, D); \Return the greatest common divisor of N and D
int N, D; \numerator and denominator
int R;
[if D > N then
[R:= D; D:= N; N:= R]; \swap D and N
while D > 0 do
[R:= rem(N/D);
N:= D;
D:= R;
];
return N;
]; \GCD
 
func Totient(N); \Return the totient of N
int N, Phi, M;
[Phi:= 0;
for M:= 1 to N do
if GCD(M, N) = 1 then Phi:= Phi+1;
return Phi;
];
 
func FareyLen(N); \Return length of Farey sequence for N
int N, Sum, M;
[Sum:= 1;
for M:= 1 to N do
Sum:= Sum + Totient(M);
return Sum;
];
 
int N;
[for N:= 1 to 11 do
[IntOut(0, N); Text(0, ": ");
Farey(N);
CrLf(0);
];
for N:= 1 to 10 do
[IntOut(0, N); Text(0, "00: ");
IntOut(0, FareyLen(N*100));
CrLf(0);
];
RlOut(0, 3.0 * sq(1000.0) / sq(3.141592654)); CrLf(0);
]</syntaxhighlight>
 
{{out}}
<pre>
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
303963.55085
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Rosetta Code problem: https://rosettacode.org/wiki/Farey_sequence
// by Jjuanhdez, 06/2022
 
for i = 1 to 11
print "F", i, " = ";
farey(i, FALSE)
next i
print
for i = 100 to 1000 step 100
print "F", i;
if i <> 1000 then print " "; else print ""; : fi
print " = ";
farey(i, FALSE)
next i
end
 
sub farey(n, descending)
a = 0 : b = 1 : c = 1 : d = n : k = 0
cont = 0
 
if descending = TRUE then
a = 1 : c = n -1
end if
 
cont = cont + 1
if n < 12 then print a, "/", b, " "; : fi
 
while ((c <= n) and not descending) or ((a > 0) and descending)
aa = a : bb = b : cc = c : dd = d
k = int((n + b) / d)
a = cc : b = dd : c = k * cc - aa : d = k * dd - bb
cont = cont + 1
if n < 12 then print a, "/", b, " "; : fi
end while
 
if n < 12 then print else print cont using("######") : fi
end sub</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">fcn farey(n){
f1,f2:=T(0,1),T(1,n); // fraction is (num,dnom)
print("%d/%d %d/%d".fmt(0,1,1,n));
Line 3,596 ⟶ 4,839:
}
println();
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach n in ([1..11]){ print("%2d: ".fmt(n)); farey(n); }</langsyntaxhighlight>
{{out}}
<pre>
Line 3,612 ⟶ 4,855:
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
</pre>
<langsyntaxhighlight lang="zkl">fcn farey_len(n){
var cache=Dictionary(); // 107 keys to 1,000; 6323@10,000,000
if(z:=cache.find(n)) return(z);
Line 3,623 ⟶ 4,866:
}
cache[n]=len; // len is returned
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach n in ([100..1000,100]){
println("%4d: %7,d items".fmt(n,farey_len(n)));
}
n:=0d10_000_000;
println("\n%,d: %,d items".fmt(n,farey_len(n)));</langsyntaxhighlight>
{{out}}
<pre>
890

edits