Continued fraction: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
mNo edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 19:
;See also<nowiki>:</nowiki>
* [[Continued fraction/Arithmetic]] for tasks that do arithmetic over continued fractions.
 
 
=={{header|11l}}==
Line 507 ⟶ 506:
2.718281828
3.141592653</pre>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<lang csharp>using System;
using System.Collections.Generic;
 
namespace ContinuedFraction {
class Program {
static double Calc(Func<int, int[]> f, int n) {
double temp = 0.0;
for (int ni = n; ni >= 1; ni--) {
int[] p = f(ni);
temp = p[1] / (p[0] + temp);
}
return f(0)[0] + temp;
}
 
static void Main(string[] args) {
List<Func<int, int[]>> fList = new List<Func<int, int[]>>();
fList.Add(n => new int[] { n > 0 ? 2 : 1, 1 });
fList.Add(n => new int[] { n > 0 ? n : 2, n > 1 ? (n - 1) : 1 });
fList.Add(n => new int[] { n > 0 ? 6 : 3, (int) Math.Pow(2 * n - 1, 2) });
 
foreach (var f in fList) {
Console.WriteLine(Calc(f, 200));
}
}
}
}</lang>
{{out}}
<pre>1.4142135623731
2.71828182845905
3.14159262280485</pre>
 
=={{header|C++}}==
Line 558 ⟶ 590:
</pre>
 
=={{header|C#|C sharpChapel}}==
{{trans|Java}}
<lang csharp>using System;
using System.Collections.Generic;
 
Functions don't take other functions as arguments, so I wrapped them in a dummy record each.
namespace ContinuedFraction {
<lang chapel>proc calc(f, n) {
class Program {
var r = 0.0;
static double Calc(Func<int, int[]> f, int n) {
 
double temp = 0.0;
for (intk ni =in 1..n; ni >=by -1; ni--) {
int[]var pv = f.pair(nik);
tempr = p[1]v(2) / (p[0]v(1) + tempr);
}
return f(0)[0] + temp;
}
 
staticreturn void Mainf.pair(string[] args0)(1) + {r;
}
List<Func<int, int[]>> fList = new List<Func<int, int[]>>();
fList.Add(n => new int[] { n > 0 ? 2 : 1, 1 });
fList.Add(n => new int[] { n > 0 ? n : 2, n > 1 ? (n - 1) : 1 });
fList.Add(n => new int[] { n > 0 ? 6 : 3, (int) Math.Pow(2 * n - 1, 2) });
 
record Sqrt2 {
foreach (var f in fList) {
proc Console.WriteLinepair(Calc(f,n) 200));{
} return (if n == 0 then 1 else 2,
1);
}
}
}
 
}</lang>
record Napier {
{{out}}
proc pair(n) {
<pre>1.4142135623731
return (if n == 0 then 2 else n,
2.71828182845905
if n == 1 then 1 else n - 1);
3.14159262280485</pre>
}
}
record Pi {
proc pair(n) {
return (if n == 0 then 3 else 6,
(2*n - 1)**2);
}
}
 
config const n = 200;
writeln(calc(new Sqrt2(), n));
writeln(calc(new Napier(), n));
writeln(calc(new Pi(), n));</lang>
 
=={{header|Clojure}}==
Line 886 ⟶ 924:
napier's = 2.7182818284590464d0
pi = 3.141592653589543d0</pre>
 
=={{header|Chapel}}==
 
Functions don't take other functions as arguments, so I wrapped them in a dummy record each.
<lang chapel>proc calc(f, n) {
var r = 0.0;
 
for k in 1..n by -1 {
var v = f.pair(k);
r = v(2) / (v(1) + r);
}
 
return f.pair(0)(1) + r;
}
 
record Sqrt2 {
proc pair(n) {
return (if n == 0 then 1 else 2,
1);
}
}
 
record Napier {
proc pair(n) {
return (if n == 0 then 2 else n,
if n == 1 then 1 else n - 1);
}
}
record Pi {
proc pair(n) {
return (if n == 0 then 3 else 6,
(2*n - 1)**2);
}
}
 
config const n = 200;
writeln(calc(new Sqrt2(), n));
writeln(calc(new Napier(), n));
writeln(calc(new Pi(), n));</lang>
 
=={{header|D}}==
Line 1,236 ⟶ 1,235:
println$ cf_iter 200 napier; // => 2.71818
println$ cf_iter 1000 pi; // => 3.14159</lang>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Continued_fraction this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). 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 transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Forth}}==
Line 1,372 ⟶ 1,363:
3.1415926535895435
</pre>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Continued_fraction this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). 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 transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
Line 1,961 ⟶ 1,960:
π ≈ 3.141592653
π/2 ≈ 1.570717797</pre>
 
=={{header|Perl 6}}==
{{Works with|rakudo|2015-10-31}}
<lang perl6>sub continued-fraction(:@a, :@b, Int :$n = 100)
{
my $x = @a[$n - 1];
$x = @a[$_ - 1] + @b[$_] / $x for reverse 1 ..^ $n;
$x;
}
 
printf "√2 ≈%.9f\n", continued-fraction(:a(1, |(2 xx *)), :b(Nil, |(1 xx *)));
printf "e ≈%.9f\n", continued-fraction(:a(2, |(1 .. *)), :b(Nil, 1, |(1 .. *)));
printf "π ≈%.9f\n", continued-fraction(:a(3, |(6 xx *)), :b(Nil, |((1, 3, 5 ... *) X** 2)));</lang>
{{out}}
<pre>√2 ≈ 1.414213562
e ≈ 2.718281828
π ≈ 3.141592654</pre>
 
A more original and a bit more abstract method would consist in viewing a continued fraction on rank n as a function of a variable x:
:<math>\mathrm{CF}_3(x) = a_0 + \cfrac{b_1}{a_1 + \cfrac{b_2}{a_2 + \cfrac{b_3}{a_3 + x}}}</math>
Or, more consistently:
:<math>\mathrm{CF}_3(x) = a_0 + \cfrac{b_0}{a_1 + \cfrac{b_1}{a_2 + \cfrac{b_2}{a_3 + \cfrac{b_3}{x}}}}</math>
Viewed as such, <math>\mathrm{CF}_n(x)</math> could be written recursively:
:<math>\mathrm{CF}_n(x) = \mathrm{CF}_{n-1}(a_n + \frac{b_n}{x})</math>
Or in other words:
:<math>\mathrm{CF}_n= \mathrm{CF}_{n-1}\circ f_n = \mathrm{CF}_{n-2}\circ f_{n-1}\circ f_n=\ldots=f_0\circ f_1 \ldots \circ f_n</math>
where <math>f_n(x) = a_n + \frac{b_n}{x}</math>
 
Perl6 has a builtin composition operator. We can use it with the triangular reduction metaoperator, and evaluate each resulting function at infinity (any value would do actually, but infinite makes it consistent with this particular task).
<lang Perl 6>sub continued-fraction(@a, @b) {
map { .(Inf) }, [\o] map { @a[$_] + @b[$_] / * }, ^Inf
}
printf "√2 ≈ %.9f\n", continued-fraction((1, |(2 xx *)), (1 xx *))[10];
printf "e ≈ %.9f\n", continued-fraction((2, |(1 .. *)), (1, |(1 .. *)))[10];
printf "π ≈ %.9f\n", continued-fraction((3, |(6 xx *)), ((1, 3, 5 ... *) X** 2))[100];</lang>
{{out}}
<pre>√2 ≈ 1.414213552
e ≈ 2.718281827
π ≈ 3.141592411</pre>
 
=={{header|Phix}}==
Line 2,370 ⟶ 2,329:
(bf #e3.14159268391980626493420192940831754203350026401337226640663040854412059241988978103217808449508253393479795573626200366332733859609651462659489470805432281782785922056335606047700127154963266242144951481397480765182268219697420028007903565511884267297358842935537138583640066772149177226656227031792115896439889412205871076985598822285367358003457939603015797225018209619662200081521930463480571130673429337524564941105654923909951299948539893933654293161126559643573974163405197696633200469475250152247413175932572922175467223988860975105100904322239324381097207835036465269418118204894206705789759765527734394105147)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2015-10-31}}
<lang perl6>sub continued-fraction(:@a, :@b, Int :$n = 100)
{
my $x = @a[$n - 1];
$x = @a[$_ - 1] + @b[$_] / $x for reverse 1 ..^ $n;
$x;
}
 
printf "√2 ≈%.9f\n", continued-fraction(:a(1, |(2 xx *)), :b(Nil, |(1 xx *)));
printf "e ≈%.9f\n", continued-fraction(:a(2, |(1 .. *)), :b(Nil, 1, |(1 .. *)));
printf "π ≈%.9f\n", continued-fraction(:a(3, |(6 xx *)), :b(Nil, |((1, 3, 5 ... *) X** 2)));</lang>
{{out}}
<pre>√2 ≈ 1.414213562
e ≈ 2.718281828
π ≈ 3.141592654</pre>
 
A more original and a bit more abstract method would consist in viewing a continued fraction on rank n as a function of a variable x:
:<math>\mathrm{CF}_3(x) = a_0 + \cfrac{b_1}{a_1 + \cfrac{b_2}{a_2 + \cfrac{b_3}{a_3 + x}}}</math>
Or, more consistently:
:<math>\mathrm{CF}_3(x) = a_0 + \cfrac{b_0}{a_1 + \cfrac{b_1}{a_2 + \cfrac{b_2}{a_3 + \cfrac{b_3}{x}}}}</math>
Viewed as such, <math>\mathrm{CF}_n(x)</math> could be written recursively:
:<math>\mathrm{CF}_n(x) = \mathrm{CF}_{n-1}(a_n + \frac{b_n}{x})</math>
Or in other words:
:<math>\mathrm{CF}_n= \mathrm{CF}_{n-1}\circ f_n = \mathrm{CF}_{n-2}\circ f_{n-1}\circ f_n=\ldots=f_0\circ f_1 \ldots \circ f_n</math>
where <math>f_n(x) = a_n + \frac{b_n}{x}</math>
 
Perl6 has a builtin composition operator. We can use it with the triangular reduction metaoperator, and evaluate each resulting function at infinity (any value would do actually, but infinite makes it consistent with this particular task).
<lang perl6>sub continued-fraction(@a, @b) {
map { .(Inf) }, [\o] map { @a[$_] + @b[$_] / * }, ^Inf
}
printf "√2 ≈ %.9f\n", continued-fraction((1, |(2 xx *)), (1 xx *))[10];
printf "e ≈ %.9f\n", continued-fraction((2, |(1 .. *)), (1, |(1 .. *)))[10];
printf "π ≈ %.9f\n", continued-fraction((3, |(6 xx *)), ((1, 3, 5 ... *) X** 2))[100];</lang>
{{out}}
<pre>√2 ≈ 1.414213552
e ≈ 2.718281827
π ≈ 3.141592411</pre>
 
=={{header|REXX}}==
Line 2,895:
> (cf->real pi)
3.141592653589794</lang>
 
 
=={{header|Sidef}}==
Line 3,137 ⟶ 3,136:
Napier: 2,71828182845905
Pi: 3,14159265358954 </pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
10,327

edits