Last week we computed the distance between two points in the cartesian plane, today we want to compute the** factorial of a number and the fibonacci serie.**

Then we want to add both the function in the calculus source code.

**Factorial:**

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

The value of 0! is 1.

The value of 1! is 1.

The value of 3! is 3*2*1 = 6.

So we can write:

`fac(0) -> 1;`

`fac(N) -> N *fac(N-1).`

The fac() function accepts 1 parameter, so we should have an export like the following:

`-export([fac/1]).`

Since we want to add the fac() function to the calculus program the export will be:

`-export([distance/4, fac/1]).`

and the final code for will be:

`$cat calculus.erl`

`-module(calculus).`

`-export([distance/4, fac/1]).`

`% distance function`

`distance(X0, Y0, X1, Y1) -> math:sqrt(((X1-X0)*(X1-X0)+(Y1-Y0)*(Y1-Y0))).`

`% factorial function`

`fac(0)-> 1;`

`fac(N) -> N * fac(N-1).`

The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

So in code:

`fib(N) -> fib(N, 0, 1).`

`fib(0, _, B) -> B;`

`fib(N, A, B) -> io:format("~p~n", [B]), fib(N-1, B, A+B).`

Like the factorial, the fibonacci function accepts 1 parameter and then the export will look like:

`-export([fib/1]).`

and in the final calculus program:

`-export([distance/4, fac/1, fib/1]).`

so the complete calculus.erl source will be:

`$cat calculus.erl`

`-module(calculus).`

`-export([distance/4, fac/1, fib/1]).`

`% distance function`

`distance(X0, Y0, X1, Y1) -> math:sqrt(((X1-X0)*(X1-X0)+(Y1-Y0)*(Y1-Y0))).`

`% factorial function`

`fac(0)-> 1;`

`fac(N) -> N * fac(N-1).`

`% fibonacci function`

`fib(N) ->`

` fib(N, 0, 1).`

`fib(0, _, B) -> B;`

`fib(N, A, B) ->`

` io:format("~p~n", [B]),`

` fib(N-1, B, A+B).`

Gg1.