Continued fraction: Difference between revisions
Content added Content deleted
(→{{header|Ada}}: -- revised solution using a (generic) package, to become more Ada-typical) |
(→{{header|Perl 6}}: simplification + neglecting extra credit) |
||
Line 737: | Line 737: | ||
<lang Perl 6>sub infix:<⚬>(&f, &g) { -> $x { &f(&g($x)) } } |
<lang Perl 6>sub infix:<⚬>(&f, &g) { -> $x { &f(&g($x)) } } |
||
sub continued-fraction(@a, @b) { |
sub continued-fraction(@a, @b) { |
||
map { $_(Inf) }, [\⚬] |
map { $_(Inf) }, [\⚬] map { take @a[$_] + @b[$_] / * }, 0 .. * |
||
} |
} |
||
Line 745: | Line 745: | ||
The main advantage is that the definition of the function does not need to know for which rank n it is computed. This is arguably closer to the mathematical definition. |
The main advantage is that the definition of the function does not need to know for which rank n it is computed. This is arguably closer to the mathematical definition. |
||
'''Extra credit''': |
|||
The value of <math>\pi/2</math> can be related to a pseudo-continued fraction of the harmonic sequence: |
|||
:<math>\pi/2 = 1 + \cfrac{1}{1 + \cfrac{1}{1/2 + \cfrac{1}{1/3+ \ddots}}}</math> |
|||
With the same code as above, this could be written: |
|||
<lang Perl 6>printf "π/2 ≈ %.9f\n", continued-fraction((1 X/ 1, 1 .. *), (1 xx *))[1000];</lang> |
|||
=={{header|PL/I}}== |
=={{header|PL/I}}== |