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
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 "
▲ 1_000;</lang>
{{out}}
<pre>√2 ≈ 1.414213562
e ≈ 2.718281828
=={{header|Perl 6}}==
|