Continued fraction: Difference between revisions

→‎{{header|Perl}}: shortening a bit + computing 2*pi instead of pi to make things less boring
m (→‎{{header|Perl}}: shortening a bit)
(→‎{{header|Perl}}: shortening a bit + computing 2*pi instead of pi to make things less boring)
Line 645:
my ($a, $b, $n) = @_;
$n //= 100;
my @a = map &$a, 0 .. $n;
my @b = map &$b, 1 .. $n;
 
my $x = pop @a;
$x = pop(@a) + pop(@b)/$x forwhile 1 .. $n@a;
return $x;
}
 
printf "√2 ≈ %.9f\n", continued_fraction do { my $n; sub { $n++ ? 2 : 1 } }, sub { 1 };
printf "e ≈ %.9f\n", continued_fraction do { my $n; sub { $n++ ? $n-1 : 2 } }, do { my $n; sub { $n++ ? $n-1 : 1 } };
printf "πτ ≈ %.9f\n", continued_fraction sub { 6 }, do { my $n; sub { $n++ ? (2*$n - 1)**2 : 2 } }, 1_000;
1_000;</lang>
do { my $n; sub { $n++ ? 6 : 3 } },
do { my $n; sub { (2*++$n - 1)**2 } },
1_000;</lang>
{{out}}
<pre>√2 ≈ 1.414213562
e ≈ 2.718281828
πτ36.141592653283185307</pre>
 
=={{header|Perl 6}}==
1,934

edits