Farey sequence: Difference between revisions

(→‎{{header|C++}}: object version)
 
(56 intermediate revisions by 30 users not shown)
Line 34:
 
;See also:
*   OEIS sequence   [http[oeis://oeis.org/A006842 |A006842 numerators of Farey series of order 1, 2, ···]]
*   OEIS sequence   [http[oeis://oeis.org/A006843 |A006843 denominators of Farey series of order 1, 2, ···]]
*   OEIS sequence   [http[oeis://oeis.org/A005728 |A005728 number of fractions in Farey series of order n.]]
*   MathWorld entry   [http://mathworld.wolfram.com/FareySequence.html Farey sequence]
*   Wikipedia   entry   [http[wp://en.wikipedia.org/wiki/Farey_sequence |Farey sequence]]
<br><br>
 
Line 44:
{{trans|Lua}}
 
<langsyntaxhighlight lang="11l">F farey(n)
V a = 0
V b = 1
Line 62:
 
L(i) (100..1000).step(100)
print(i‘: ’farey(i)[1]‘ items’)</langsyntaxhighlight>
 
{{out}}
Line 87:
900: 246327 items
1000: 304193 items
</pre>
 
=={{header|ALGOL 68}}==
Based on...
{{trans|Lua}}... but calculates and optionally prints the sequences without actually storing them.
<syntaxhighlight lang="algol68">BEGIN # construct some Farey Sequences and calculate their lengths #
# prints an element of a Farey Sequence #
PROC print element = ( INT a, b )VOID:
print( ( " ", whole( a, 0 ), "/", whole( b, 0 ) ) );
# returns the length of the Farey Sequence of order n, optionally #
# printing it #
PROC farey sequence length = ( INT n, BOOL print sequence )INT:
IF n < 1 THEN 0
ELSE
INT a := 0, b := 1, c := 1, d := n;
IF print sequence THEN
print( ( whole( n, -2 ), ":" ) );
print element( a, b )
FI;
INT length := 1;
WHILE c <= n DO
INT k = ( n + b ) OVER d;
INT old a = a, old b = b;
a := c;
b := d;
c := ( k * c ) - old a;
d := ( k * d ) - old b;
IF print sequence THEN print element( a, b ) FI;
length +:= 1
OD;
IF print sequence THEN print( ( newline ) ) FI;
length
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
Farey Sequence of order 200 has length: 12233
Farey Sequence of order 300 has length: 27399
Farey Sequence of order 400 has length: 48679
Farey Sequence of order 500 has length: 76117
Farey Sequence of order 600 has length: 109501
Farey Sequence of order 700 has length: 149019
Farey Sequence of order 800 has length: 194751
Farey Sequence of order 900 has length: 246327
Farey Sequence of order 1000 has length: 304193
</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 162 ⟶ 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">
<lang AWK>
# syntax: GAWK -f FAREY_SEQUENCE.AWK
BEGIN {
Line 192 ⟶ 313:
return(1+items)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 217 ⟶ 338:
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 279 ⟶ 482:
printf("\n%d: %llu items\n", n, farey_len(n));
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 307 ⟶ 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 335 ⟶ 538:
return seq;
}
}</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 362 ⟶ 565:
 
=={{header|C++}}==
===ProceduralObject-based styleprogramming===
<langsyntaxhighlight lang="cpp">#include <iostream>
 
struct fraction {
void print_fraction(int a, int b) {
fraction(int n, int d) : numerator(n), denominator(d) {}
std::cout << a << '/' << b;
int numerator;
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 << ": ";
int a = 0, bfraction = 1result(a_, c = 1, d = nb_);
if int k = (!length_onlyn_ + b_)/d_;
print_fraction(a,int b)next_c = k * c_ - a_;
int countnext_d = 1k * d_ - b_;
for (; c < a_ = nc_; ++count) {
int kb_ = (n + b)/dd_;
int next_cc_ = k * c - anext_c;
int next_dd_ = k * d - bnext_d;
areturn = cresult;
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 in = 1; in <= 11; ++in) {
print_farey_sequence(i,farey_sequence falseseq(n);
for (int i = 100;std::cout i<< n <<= 1000;": i" +=<< 100seq.next();
print_farey_sequence(i,while true(seq.has_next());
std::cout << ' ' << seq.next();
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}}
<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>
 
===Object-oriented styleprogramming===
{{Works with|C++17}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <list>
#include <utility>
 
struct farey_sequence: public std::list<std::pair<uint, uint>> {
explicit farey_sequence(uint n) : order(n) {
push_back(std::pair(0, order(n1) {);
uint a = 0, b = 1, c = 1, d = n;
emplace_backwhile (std::pair(a,c b<= n)); {
for (uint count = 1; c <= n; ++count) {
const uint k = (n + b) / d;
const uint next_c = k * c - a;
Line 423 ⟶ 659:
c = next_c;
d = next_d;
emplace_backpush_back(std::pair(a, b));
}
}
Line 446 ⟶ 682:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{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|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 496 ⟶ 707:
(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
Line 525 ⟶ 736:
{{trans|the function from Lua, the output from Ruby.}}
Slow Version
<langsyntaxhighlight lang="ruby">require "big"
 
def farey(n)
Line 548 ⟶ 759:
puts "F(%4d) =%7d" % [i, farey(i).size]
end
</syntaxhighlight>
</lang>
 
{{trans|the function from Python, the output from Ruby.}}
Fast Version
<langsyntaxhighlight lang="ruby">require "big"
 
def farey(n, length = false)
Line 572 ⟶ 783:
puts "F(%4d) =%7d" % [i, farey(i, true)]
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 603 ⟶ 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 623 ⟶ 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 644 ⟶ 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 696 ⟶ 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 721 ⟶ 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}}==
Line 730 ⟶ 987:
 
 
<langsyntaxhighlight lang="scheme">
(define distinct-divisors
(compose make-set prime-factors))
Line 751 ⟶ 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 782 ⟶ 1,039:
|F(10000)| = 30397487
|F(100000)| = 3039650755
</syntaxhighlight>
</lang>
 
=={{header|EDSAC order code}}==
Line 793 ⟶ 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 919 ⟶ 1,176:
E 27 Z [define start of execution]
P F [start with accumulator = 0]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 937 ⟶ 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,165 ⟶ 1,422:
E 32 Z [define start of execution]
P F [start with accumulator = 0]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,183 ⟶ 1,440:
=={{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,213 ⟶ 1,470:
: main ( -- ) [ part1 part2 nl ] time ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,244 ⟶ 1,501:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 05-04-2017
' compile with: fbc -s console
 
Line 1,297 ⟶ 1,554:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>F1 = 0/1 1/1
Line 1,324 ⟶ 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,338 ⟶ 1,595:
 
for i <- 100..1000 by 100
println( "$i: ${farey(i).length()}" )</langsyntaxhighlight>
 
{{out}}
Line 1,365 ⟶ 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æ}}==
 
In [http{{FormulaeEntry|page=https://wiki.formulae.org/?script=examples/Farey_sequence this] page you can see the solution of this task.}}
 
'''Solution'''
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.
 
[[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>
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,435 ⟶ 1,835:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>F(1): 0/1 1/1
Line 1,462 ⟶ 1,862:
=={{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,502 ⟶ 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,533 ⟶ 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,693 ⟶ 2,009:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>F1: [0/1, 1/1]
Line 1,716 ⟶ 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)
Line 1,739 ⟶ 2,119:
for n in 100:100:1000
println("F_$n has ", length(farey(n)), " fractions")
end</langsyntaxhighlight>
 
{{out}}
Line 1,765 ⟶ 2,145:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1
 
fun farey(n: Int): List<String> {
Line 1,792 ⟶ 2,172:
for (i in 100..1000 step 100)
println("${"%4d".format(i)}: ${"%6d".format(farey(i).size)} fractions")
}</langsyntaxhighlight>
 
{{out}}
Line 1,821 ⟶ 2,201:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .farey = fn(.n) {
Prior to 0.10, multi-variable declarations/assignments would use parentheses on the variable names and values.
 
{{works with|langur|0.10}}
<lang langur>val .farey = f(.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 x* .c - .a, .k x* .d - .b
_while ~= [[.a, .b]]
}
}
 
val .testFarey = impure fn() {
writeln "Farey sequence for orders 1 through 11"
writeln "Farey sequence for orders 1 through 11"
for .i of 11 {
for .i of 11 {
writeln $"\.i:2;: ", join " ", map(f $"\.f[1];/\.f[2];", .farey(.i))
writeln "{{.i:2}}: ", join " ", map(fn .f: "{{.f[1]}}/{{.f[2]}}", .farey(.i))
}</lang>
}
}
 
.testFarey()
Theoretically, the following should work, but it's way too SLOW to run in langur 0.10. Maybe another release will be fast enough.
 
<lang langur>writeln "count of Farey sequence fractions for 100 to 1000 by hundreds"
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))
}</langsyntaxhighlight>
 
{{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,863 ⟶ 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,888 ⟶ 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,939 ⟶ 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.
<langsyntaxhighlight Mathematicalang="mathematica">farey[n_]:=StringJoin@@Riffle[ToString@Numerator[#]<>"/"<>ToString@Denominator[#]&/@FareySequence[n],", "]
TableForm[farey/@Range[11]]
Table[Length[FareySequence[n]], {n, 100, 1000, 100}]</langsyntaxhighlight>
{{out}}
<pre>0/1, 1/1
Line 1,958 ⟶ 2,367:
 
{3045, 12233, 27399, 48679, 76117, 109501, 149019, 194751, 246327, 304193}</pre>
 
=={{header|MATLAB}}==
{{trans|Kotlin}}
<syntaxhighlight lang="MATLAB">
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)
a = 0;
b = 1;
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>
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 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>
 
 
=={{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 2,005 ⟶ 2,539:
 
let n = 10_000_000
echo fmt"{n}: {fareyLength(n, cache):14} items"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,033 ⟶ 2,567:
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">Farey(n)=my(v=List()); for(k=1,n,for(i=0,k,listput(v,i/k))); vecsort(Set(v));
 
{{incorrect|PARI/GP| <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> }}
 
 
<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>
Line 2,061 ⟶ 2,590:
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 2,129 ⟶ 2,658:
inc(cnt,100);
until cnt > 1000;
end.</langsyntaxhighlight>
{{Out}}
<pre style ="horizontal=85">
Line 2,162 ⟶ 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,190 ⟶ 2,719:
for (1 .. 10, 100000) {
print "F${_}00: ", farey_count(100*$_), " members\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,219 ⟶ 2,748:
=== Mapped Rationals ===
Similar to Pari and Raku. 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,245 ⟶ 2,774:
my @f = farey(100*$_);
print "F${_}00: ", scalar(@f), " members\n";
}</langsyntaxhighlight>
 
=={{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,294 ⟶ 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,326 ⟶ 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,355 ⟶ 2,934:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">EnableExplicit
 
Structure farey_struc
Line 2,439 ⟶ 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,554 ⟶ 3,133:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from fractions import Fraction
 
 
Line 2,574 ⟶ 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,595 ⟶ 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,792 ⟶ 3,371:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Farey sequence for orders 1-11 (inclusive):
Line 2,820 ⟶ 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,858 ⟶ 3,504:
farey(i, length_only = TRUE)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,887 ⟶ 3,533:
 
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,906 ⟶ 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,947 ⟶ 3,593:
<!-- bug bites at farey(362): sub farey ($order) { unique 0/1, |(1..$order).map: { |(1..$^d).map: { $^n/$d } } } -->
 
<syntaxhighlight lang="raku" perl6line>sub farey ($order) {
my @l = 0/1, 1/1;
(2..$order).map: { push @l, |(1..$^d).map: { $_/$d } }
Line 2,955 ⟶ 3,601:
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.' }</langsyntaxhighlight>
{{out}}
<pre>Farey sequence order
Line 2,982 ⟶ 3,628:
=={{header|REXX}}==
Programming note: &nbsp; if the 1<sup>st</sup> argument is negative, &nbsp; then only the count of the fractions is shown.
<langsyntaxhighlight lang="rexx">/*REXX program computes and displays a Farey sequence (or the number of fractions). */
parse arg LO HI INC . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/
Line 3,012 ⟶ 3,658:
else $= $ _ /*No? Keep it & keep building.*/
end /*k*/
if $\=='' then say $; return /*display any residual fractions. */</langsyntaxhighlight>
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).
 
Line 3,073 ⟶ 3,719:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Farey sequence
 
Line 3,121 ⟶ 3,767:
ok
return count
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,146 ⟶ 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,165 ⟶ 3,881:
for i in (100..1000).step(100)
puts "F(%4d) =%7d" % [i, farey(i, true)]
end</langsyntaxhighlight>
 
{{out}}
Line 3,192 ⟶ 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,223 ⟶ 4,022:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,253 ⟶ 4,052:
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme write))
Line 3,309 ⟶ 4,108:
(number->string (farey-sequence i #f))))
(newline))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,340 ⟶ 4,139:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func farey_count(n) { # A005728
1 + sum(1..n, {|k| euler_phi(k) })
}
Line 3,366 ⟶ 4,165:
for n in (100..1000 -> by(100)) {
say ("F(%4d) =%7d" % (n, farey_count(n)))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,397 ⟶ 4,196:
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">mata
function totient(n_) {
n = n_
Line 3,473 ⟶ 4,272:
 
map(&farey_length(),100*(1..10))
end</langsyntaxhighlight>
 
'''Output'''
Line 3,496 ⟶ 4,295:
=={{header|Swift}}==
Class with computed properties:
<langsyntaxhighlight lang="swift">class Farey {
let n: Int
 
Line 3,544 ⟶ 4,343:
let m = n * 100
print("\(m): \(Farey(m).sequence.count)")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,577 ⟶ 4,376:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc farey {n} {
Line 3,603 ⟶ 4,402:
for {set i 100} {$i <= 1000} {incr i 100} {
puts |F($i)|\x20=\x20[llength [farey $i]]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,629 ⟶ 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,682 ⟶ 4,524:
for (uint n = 100; n <= 1000; n += 100)
print("%8u: %14u items\n", n, fareyLength(n, cache));
}</langsyntaxhighlight>
 
{{out}}
Line 3,708 ⟶ 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-traititerate}}
{{libheader|Wren-fmt}}
{{libheader|Wren-rat}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./traititerate" for Stepped
import "./fmt" for Fmt
import "./rat" for Rat
 
var f //recursive
Line 3,767 ⟶ 4,671:
sum = sum + tot[n]
if (n%100 == 0) System.print("F(%(Fmt.d(4, n))): %(Fmt.dc(7, sum))")
}</langsyntaxhighlight>
 
{{out}}
Line 3,794 ⟶ 4,698:
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,806 ⟶ 4,839:
}
println();
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach n in ([1..11]){ print("%2d: ".fmt(n)); farey(n); }</langsyntaxhighlight>
{{out}}
<pre>
Line 3,822 ⟶ 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,833 ⟶ 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