Farey sequence: Difference between revisions

Line 715:
 
 
{{improve|D| <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> }}
 
This should import a slightly modified version of the module from the Arithmetic/Rational task renamed as arithmetic_rational2.d and where toString() is redefined as follows
 
<lang d> string toString() const /*pure nothrow*/ {
This imports the module from the Arithmetic/Rational task.
if (den != 0)
<lang d>import std.stdio, std.algorithm, std.range, arithmetic_rational;
//return num.text ~ (den == 1 ? "" : "/" ~ den.text);
return num.text ~ "/" ~ den.text;
if (num == 0)
return "NaRat";
else
return ((num < 0) ? "-" : "+") ~ "infRat";
}
</lang>
 
 
 
 
 
<lang d>import std.stdio, std.algorithm, std.range, arithmetic_rationalarithmetic_rational2;
 
auto farey(in int n) pure nothrow @safe {
Line 738 ⟶ 750:
{{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 833 ⟶ 845:
 
10000000: 30396356427243 items</pre>
 
=={{header|Delphi}}==
See [https://www.rosettacode.org/wiki/Farey_sequence#Pascal Pascal].
121

edits