Course of Raku / Essentials / Creating and calling functions / Exercises / The value of e

Solution: The value of e


In Raku, there is a built-in constant e that gives the immediate answer to the task. The solution shown below is designed for education.


The formula in this task contains a factorial, and we already have at least two solutions to compute it (more to follow). The program below defines two functions: one to compute factorial, another one for the partial sum according to the formula.

As it is required that the function can be called with no arguments, in which case it runs a predefined number of iterations, we can add a default value of the parameter: sub e-approx($max = 100).

In the code, notice the similarities in how the result values are accumulated in a postfix for loop over a range.

Here is the solution:

sub factorial($n) {
    my $f = 1;
    $f *= $_ for 1..$n;

    return $f;

sub e-approx($max = 100) {
    my $e = 1;
    $e += 1 / factorial($_) for 1..$max;

    return $e;

say e-approx(10);
say e-approx();

🦋 Find the program in the file the-value-of-e.raku.


With 10 and 100 iterations, we get the following results. Additionally, try other values of $max.

$ raku exercises/functions/the-value-of-e.raku

Course navigation

Associative data types / Interpolating hashes   |   More about functions