user
A number may be represented as a continued fraction (see Mathworld for more information) as follows:

:   a

    0
    +

    b

    1

    a

    1
    +

    b

    2

    a

    2
    +

    b

    3

    a

    3
    +
    ⋱
    {\displaystyle {\displaystyle a\_{0}+{\cfrac {b\_{1}}{a\_{1}+{\cfrac {b\_{2}}{a\_{2}+{\cfrac {b\_{3}}{a\_{3}+\ddots }}}}}}}}

The task is to write a program which generates such a number and prints a real representation of it. The code should be tested by calculating and printing the square root of 2, Napier's Constant, and Pi, using the following coefficients:

For the square root of 2, use 

a

0
=
1
{\displaystyle {\displaystyle a\_{0}=1}}
 then 

a

N
=
2
{\displaystyle {\displaystyle a\_{N}=2}}
. 

b

N
{\displaystyle {\displaystyle b\_{N}}}
 is always 

1
{\displaystyle {\displaystyle 1}}
.

:   2
    =
    1
    +

    1

    2
    +

    1

    2
    +

    1

    2
    +
    ⋱
    {\displaystyle {\displaystyle {\sqrt {2}}=1+{\cfrac {1}{2+{\cfrac {1}{2+{\cfrac {1}{2+\ddots }}}}}}}}

For Napier's Constant, use 

a

0
=
2
{\displaystyle {\displaystyle a\_{0}=2}}
, then 

a

N
=
N
{\displaystyle {\displaystyle a\_{N}=N}}
. 

b

1
=
1
{\displaystyle {\displaystyle b\_{1}=1}}
 then 

b

N
=
N
−
1
{\displaystyle {\displaystyle b\_{N}=N-1}}
.

:   e
    =
    2
    +

    1

    1
    +

    1

    2
    +

    2

    3
    +

    3

    4
    +
    ⋱
    {\displaystyle {\displaystyle e=2+{\cfrac {1}{1+{\cfrac {1}{2+{\cfrac {2}{3+{\cfrac {3}{4+\ddots }}}}}}}}}}

For Pi, use 

a

0
=
3
{\displaystyle {\displaystyle a\_{0}=3}}
 then 

a

N
=
6
{\displaystyle {\displaystyle a\_{N}=6}}
. 

b

N
=
(
2
N
−
1

)

2
{\displaystyle {\displaystyle b\_{N}=(2N-1)^{2}}}
.

:   π
    =
    3
    +

    1

    6
    +

    9

    6
    +

    25

    6
    +
    ⋱
    {\displaystyle {\displaystyle \pi =3+{\cfrac {1}{6+{\cfrac {9}{6+{\cfrac {25}{6+\ddots }}}}}}}}

See also
:   * Continued fraction/Arithmetic for tasks that do arithmetic over continued fractions.
assistant
(formerly Perl 6)

**Works with**: rakudo version 2015-10-31

```
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)));

```

Output:

```
√2 ≈ 1.414213562
e  ≈ 2.718281828
π  ≈ 3.141592654
```

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:

:   C
    F

    3
    (
    x
    )
    =

    a

    0
    +

    b

    1

    a

    1
    +

    b

    2

    a

    2
    +

    b

    3

    a

    3
    +
    x
    {\displaystyle {\displaystyle \mathrm {CF} \_{3}(x)=a\_{0}+{\cfrac {b\_{1}}{a\_{1}+{\cfrac {b\_{2}}{a\_{2}+{\cfrac {b\_{3}}{a\_{3}+x}}}}}}}}

Or, more consistently:

:   C
    F

    3
    (
    x
    )
    =

    a

    0
    +

    b

    0

    a

    1
    +

    b

    1

    a

    2
    +

    b

    2

    a

    3
    +

    b

    3

    x
    {\displaystyle {\displaystyle \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}}}}}}}}}}

Viewed as such, 

C
F

n
(
x
)
{\displaystyle {\displaystyle \mathrm {CF} \_{n}(x)}}
 could be written recursively:

:   C
    F

    n
    (
    x
    )
    =


    C
    F

    n
    −
    1
    (

    a

    n
    +

    b

    n
    x
    )
    {\displaystyle {\displaystyle \mathrm {CF} \_{n}(x)=\mathrm {CF} \_{n-1}(a\_{n}+{\frac {b\_{n}}{x}})}}

Or in other words:

:   C
    F

    n
    =


    C
    F

    n
    −
    1
    ∘

    f

    n
    =


    C
    F

    n
    −
    2
    ∘

    f

    n
    −
    1
    ∘

    f

    n
    =
    …
    =

    f

    0
    ∘

    f

    1
    …
    ∘

    f

    n
    {\displaystyle {\displaystyle \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}}}

where 

f

n
(
x
)
=

a

n
+

b

n
x
{\displaystyle {\displaystyle f\_{n}(x)=a\_{n}+{\frac {b\_{n}}{x}}}}

Raku 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).

```
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];

```

Output:

```
√2 ≈ 1.414213552
e  ≈ 2.718281827
π  ≈ 3.141592411
```