Continued fraction/Arithmetic/Construct from rational number: Difference between revisions

Content added Content deleted
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 135: Line 135:
For N = 314285714, D = 100000000 : 3 7 7142857
For N = 314285714, D = 100000000 : 3 7 7142857
</pre>
</pre>

=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections.Generic;

class Program
{
static IEnumerable<int> r2cf(int n1, int n2)
{
while (Math.Abs(n2) > 0)
{
int t1 = n1 / n2;
int t2 = n2;
n2 = n1 - t1 * n2;
n1 = t2;
yield return t1;
}
}

static void spit(IEnumerable<int> f)
{
foreach (int n in f) Console.Write(" {0}", n);
Console.WriteLine();
}

static void Main(string[] args)
{
spit(r2cf(1, 2));
spit(r2cf(3, 1));
spit(r2cf(23, 8));
spit(r2cf(13, 11));
spit(r2cf(22, 7));
spit(r2cf(-151, 77));
for (int scale = 10; scale <= 10000000; scale *= 10)
{
spit(r2cf((int)(Math.Sqrt(2) * scale), scale));
}
spit(r2cf(31, 10));
spit(r2cf(314, 100));
spit(r2cf(3142, 1000));
spit(r2cf(31428, 10000));
spit(r2cf(314285, 100000));
spit(r2cf(3142857, 1000000));
spit(r2cf(31428571, 10000000));
spit(r2cf(314285714, 100000000));
}
}
</lang>
Output
<pre>
0 2
3
2 1 7
1 5 2
3 7
-1 -1 -24 -1 -2
1 2 2
1 2 2 3 1 1 2
1 2 2 2 2 5 3
1 2 2 2 2 2 1 1 29
1 2 2 2 2 2 2 3 1 1 3 1 7 2
1 2 2 2 2 2 2 2 1 1 4 1 1 1 1 1 2 1 6
1 2 2 2 2 2 2 2 2 2 1 594
3 10
3 7 7
3 7 23 1 2
3 7 357
3 7 2857
3 7 142857
3 7 476190 3
3 7 7142857</pre>


=={{header|C++}}==
=={{header|C++}}==
Line 243: Line 314:
3 7 7142857
3 7 7142857
</pre>
</pre>

=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections.Generic;

class Program
{
static IEnumerable<int> r2cf(int n1, int n2)
{
while (Math.Abs(n2) > 0)
{
int t1 = n1 / n2;
int t2 = n2;
n2 = n1 - t1 * n2;
n1 = t2;
yield return t1;
}
}

static void spit(IEnumerable<int> f)
{
foreach (int n in f) Console.Write(" {0}", n);
Console.WriteLine();
}

static void Main(string[] args)
{
spit(r2cf(1, 2));
spit(r2cf(3, 1));
spit(r2cf(23, 8));
spit(r2cf(13, 11));
spit(r2cf(22, 7));
spit(r2cf(-151, 77));
for (int scale = 10; scale <= 10000000; scale *= 10)
{
spit(r2cf((int)(Math.Sqrt(2) * scale), scale));
}
spit(r2cf(31, 10));
spit(r2cf(314, 100));
spit(r2cf(3142, 1000));
spit(r2cf(31428, 10000));
spit(r2cf(314285, 100000));
spit(r2cf(3142857, 1000000));
spit(r2cf(31428571, 10000000));
spit(r2cf(314285714, 100000000));
}
}
</lang>
Output
<pre>
0 2
3
2 1 7
1 5 2
3 7
-1 -1 -24 -1 -2
1 2 2
1 2 2 3 1 1 2
1 2 2 2 2 5 3
1 2 2 2 2 2 1 1 29
1 2 2 2 2 2 2 3 1 1 3 1 7 2
1 2 2 2 2 2 2 2 1 1 4 1 1 1 1 1 2 1 6
1 2 2 2 2 2 2 2 2 2 1 594
3 10
3 7 7
3 7 23 1 2
3 7 357
3 7 2857
3 7 142857
3 7 476190 3
3 7 7142857</pre>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 1,631: Line 1,631:
[3; 7; 7142857]
[3; 7; 7142857]
</pre>
</pre>

=={{header|Perl 6}}==
Straightforward implementation:
<lang perl6>sub r2cf(Rat $x is copy) {
gather loop {
$x -= take $x.floor;
last unless $x > 0;
$x = 1 / $x;
}
}

say r2cf(.Rat) for <1/2 3 23/8 13/11 22/7 1.41 1.4142136>;</lang>
{{out}}
<pre>(0 2)
(3)
(2 1 7)
(1 5 2)
(3 7)
(1 2 2 3 1 1 2)
(1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2)</pre>
As a silly one-liner:
<lang perl6>sub r2cf(Rat $x is copy) { gather $x [R/]= 1 while ($x -= take $x.floor) > 0 }</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,780: Line 1,758:
'(3 7 15 1 292 1 1 6 2 13 3 1 12 3)
'(3 7 15 1 292 1 1 6 2 13 3 1 12 3)
</pre>
</pre>

=={{header|Raku}}==
(formerly Perl 6)
Straightforward implementation:
<lang perl6>sub r2cf(Rat $x is copy) {
gather loop {
$x -= take $x.floor;
last unless $x > 0;
$x = 1 / $x;
}
}

say r2cf(.Rat) for <1/2 3 23/8 13/11 22/7 1.41 1.4142136>;</lang>
{{out}}
<pre>(0 2)
(3)
(2 1 7)
(1 5 2)
(3 7)
(1 2 2 3 1 1 2)
(1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2)</pre>
As a silly one-liner:
<lang perl6>sub r2cf(Rat $x is copy) { gather $x [R/]= 1 while ($x -= take $x.floor) > 0 }</lang>


=={{header|REXX}}==
=={{header|REXX}}==