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

Added C# implementation
(added real to CF for haskell)
(Added C# implementation)
Line 141:
3 7 7142857
</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|Haskell}}==
Anonymous user