Farey sequence: Difference between revisions

Added QBasic
imported>Maxima enthusiast
No edit summary
(Added QBasic)
(12 intermediate revisions by 6 users not shown)
Line 375:
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>
Line 904 ⟶ 947:
=={{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 2,127 ⟶ 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}}
<syntaxhighlight 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 = fimpure fn() {
writeln "Farey sequence for orders 1 through 11"
for .i of 11 {
writeln $"\.i:2;: ", join " ", map(fn(.f) $"\.f[1];/\.f[2];", .farey(.i))
}
}
Line 2,296 ⟶ 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}}==
Line 4,294 ⟶ 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}}
Line 4,442 ⟶ 4,619:
{{libheader|Wren-fmt}}
{{libheader|Wren-rat}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./iterate" for Stepped
import "./fmt" for Fmt
import "./rat" for Rat
 
var f //recursive
2,122

edits