Farey sequence: Difference between revisions

No edit summary
 
(39 intermediate revisions by 19 users not shown)
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 310 ⟶ 513:
{{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 363 ⟶ 566:
=={{header|C++}}==
===Object-based programming===
<langsyntaxhighlight lang="cpp">#include <iostream>
 
struct fraction {
Line 411 ⟶ 614:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 440 ⟶ 643:
===Object-oriented programming===
{{Works with|C++17}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <list>
#include <utility>
Line 479 ⟶ 682:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
=={{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 504 ⟶ 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 533 ⟶ 736:
{{trans|the function from Lua, the output from Ruby.}}
Slow Version
<langsyntaxhighlight lang="ruby">require "big"
 
def farey(n)
Line 556 ⟶ 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 580 ⟶ 783:
puts "F(%4d) =%7d" % [i, farey(i, true)]
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 611 ⟶ 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 631 ⟶ 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 652 ⟶ 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 704 ⟶ 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 729 ⟶ 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 740 ⟶ 987:
 
 
<langsyntaxhighlight lang="scheme">
(define distinct-divisors
(compose make-set prime-factors))
Line 761 ⟶ 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 792 ⟶ 1,039:
|F(10000)| = 30397487
|F(100000)| = 3039650755
</syntaxhighlight>
</lang>
 
=={{header|EDSAC order code}}==
Line 803 ⟶ 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 929 ⟶ 1,176:
E 27 Z [define start of execution]
P F [start with accumulator = 0]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 947 ⟶ 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,175 ⟶ 1,422:
E 32 Z [define start of execution]
P F [start with accumulator = 0]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,193 ⟶ 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,223 ⟶ 1,470:
: main ( -- ) [ part1 part2 nl ] time ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,254 ⟶ 1,501:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 05-04-2017
' compile with: fbc -s console
 
Line 1,307 ⟶ 1,554:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>F1 = 0/1 1/1
Line 1,334 ⟶ 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,348 ⟶ 1,595:
 
for i <- 100..1000 by 100
println( "$i: ${farey(i).length()}" )</langsyntaxhighlight>
 
{{out}}
Line 1,375 ⟶ 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}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. 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 storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[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>
 
In '''[https://formulae.org/?example=Farey_sequence this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,445 ⟶ 1,835:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>F(1): 0/1 1/1
Line 1,472 ⟶ 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,512 ⟶ 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,544 ⟶ 1,934:
 
'''Solution:'''
<langsyntaxhighlight Jlang="j">Farey=: x:@/:~@(0 , ~.)@(#~ <:&1)@:,@(%/~@(1 + i.)) NB. calculates Farey sequence
displayFarey=: ('r/' charsub '0r' , ,&'r1')@": NB. displaysformat character representation of Farey sequence according to task requirements
order=: ': ' ,~ ": NB. displaydecorate order of Farey sequence</langsyntaxhighlight>
 
'''Required examples:'''
 
<langsyntaxhighlight Jlang="j"> LF joinstring (order , displayFarey@Farey)&.> 1 + i.11 NB. Farey sequences, order 1-11
1: 0/0 1/1
2: 0/0 1/2 1/1
Line 1,572 ⟶ 1,962:
800: 194751
900: 246327
1000: 304193</langsyntaxhighlight>
 
=={{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,619 ⟶ 2,009:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>F1: [0/1, 1/1]
Line 1,642 ⟶ 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,665 ⟶ 2,119:
for n in 100:100:1000
println("F_$n has ", length(farey(n)), " fractions")
end</langsyntaxhighlight>
 
{{out}}
Line 1,691 ⟶ 2,145:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1
 
fun farey(n: Int): List<String> {
Line 1,718 ⟶ 2,172:
for (i in 100..1000 step 100)
println("${"%4d".format(i)}: ${"%6d".format(farey(i).size)} fractions")
}</langsyntaxhighlight>
 
{{out}}
Line 1,747 ⟶ 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()
 
writeln()
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"
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,789 ⟶ 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,814 ⟶ 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;
Line 1,838 ⟶ 2,320:
for j from 100 to 1000 by 100 do
printf("%d\n", farey_len(j));
end do;</langsyntaxhighlight>
 
{{Out|Output}}
Line 1,868 ⟶ 2,350:
=={{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,885 ⟶ 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 1,932 ⟶ 2,539:
 
let n = 10_000_000
echo fmt"{n}: {fareyLength(n, cache):14} items"</langsyntaxhighlight>
{{out}}
<pre>
Line 1,960 ⟶ 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(apply(fmt, Farey(n))))
apply(countFarey, 100*[1..10])</langsyntaxhighlight>
{{out}}
<pre>["0/1", "1/1"]
Line 1,983 ⟶ 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,051 ⟶ 2,658:
inc(cnt,100);
until cnt > 1000;
end.</langsyntaxhighlight>
{{Out}}
<pre style ="horizontal=85">
Line 2,084 ⟶ 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,112 ⟶ 2,719:
for (1 .. 10, 100000) {
print "F${_}00: ", farey_count(100*$_), " members\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,141 ⟶ 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,167 ⟶ 2,774:
my @f = farey(100*$_);
print "F${_}00: ", scalar(@f), " members\n";
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{trans|AWK}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
Line 2,196 ⟶ 2,803:
<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>
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,216 ⟶ 2,823:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">go ?=>
member(N,1..11),
Farey = farey(N),
Line 2,230 ⟶ 2,837:
M = [ E: _=E in M1]. % extract the rational representation
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,248 ⟶ 2,855:
 
The number of fractions of order 100..100..1000:
<langsyntaxhighlight Picatlang="picat">go2 =>
foreach(N in 100..100..1000)
F = farey(N),
println(N=F.length)
end,
nl.</langsyntaxhighlight>
 
{{out}}
Line 2,270 ⟶ 2,877:
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,298 ⟶ 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,327 ⟶ 2,934:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">EnableExplicit
 
Structure farey_struc
Line 2,411 ⟶ 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,526 ⟶ 3,133:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from fractions import Fraction
 
 
Line 2,546 ⟶ 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,567 ⟶ 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,764 ⟶ 3,371:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Farey sequence for orders 1-11 (inclusive):
Line 2,797 ⟶ 3,404:
Uses the BIGnum RATional arithmetic library included with Quackery, ''bigrat.qky''.
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "bigrat.qky" loadfile ] now!
 
[ rot + dip + reduce ] is mediant ( n/d n/d --> n/d )
Line 2,841 ⟶ 3,448:
[ i^ 1+ 100 *
fareylength join ]
echo</langsyntaxhighlight>
 
{{Out}}
Line 2,861 ⟶ 3,468:
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">
farey <- function(n, length_only = FALSE) {
a <- 0
Line 2,897 ⟶ 3,504:
farey(i, length_only = TRUE)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,926 ⟶ 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,945 ⟶ 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,986 ⟶ 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,994 ⟶ 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 3,021 ⟶ 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,051 ⟶ 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,112 ⟶ 3,719:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Farey sequence
 
Line 3,160 ⟶ 3,767:
ok
return count
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,185 ⟶ 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,204 ⟶ 3,881:
for i in (100..1000).step(100)
puts "F(%4d) =%7d" % [i, farey(i, true)]
end</langsyntaxhighlight>
 
{{out}}
Line 3,234 ⟶ 3,911:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">#[derive(Copy, Clone)]
struct Fraction {
numerator: u32,
Line 3,289 ⟶ 3,966:
println!("{}: {}", n, farey_sequence(n).count());
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,317 ⟶ 3,994:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">
object FareySequence {
 
Line 3,345 ⟶ 4,022:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,375 ⟶ 4,052:
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme write))
Line 3,431 ⟶ 4,108:
(number->string (farey-sequence i #f))))
(newline))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,462 ⟶ 4,139:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func farey_count(n) { # A005728
1 + sum(1..n, {|k| euler_phi(k) })
}
Line 3,488 ⟶ 4,165:
for n in (100..1000 -> by(100)) {
say ("F(%4d) =%7d" % (n, farey_count(n)))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,519 ⟶ 4,196:
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">mata
function totient(n_) {
n = n_
Line 3,595 ⟶ 4,272:
 
map(&farey_length(),100*(1..10))
end</langsyntaxhighlight>
 
'''Output'''
Line 3,618 ⟶ 4,295:
=={{header|Swift}}==
Class with computed properties:
<langsyntaxhighlight lang="swift">class Farey {
let n: Int
 
Line 3,666 ⟶ 4,343:
let m = n * 100
print("\(m): \(Farey(m).sequence.count)")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,699 ⟶ 4,376:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc farey {n} {
Line 3,725 ⟶ 4,402:
for {set i 100} {$i <= 1000} {incr i 100} {
puts |F($i)|\x20=\x20[llength [farey $i]]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,751 ⟶ 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,804 ⟶ 4,524:
for (uint n = 100; n <= 1000; n += 100)
print("%8u: %14u items\n", n, fareyLength(n, cache));
}</langsyntaxhighlight>
 
{{out}}
Line 3,831 ⟶ 4,551:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<langsyntaxhighlight lang="go">struct Frac {
num int
den int
Line 3,889 ⟶ 4,609:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Same as Go entry</pre>
Line 3,896 ⟶ 4,616:
{{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,951 ⟶ 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,978 ⟶ 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,990 ⟶ 4,839:
}
println();
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach n in ([1..11]){ print("%2d: ".fmt(n)); farey(n); }</langsyntaxhighlight>
{{out}}
<pre>
Line 4,006 ⟶ 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 4,017 ⟶ 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