Mix.install([
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"},
{:tested_cell, github: "brooklinjazz/tested_cell"},
{:utils, path: "#{__DIR__}/../utils"}
])
The factorial of a
flowchart LR
n --*--> n1[n - 1] --*--> n2[n - 2] --*--> n3[n - 3] --*--> 2 --*--> 1
Meaning the factorial of 5
is 5 * 4 * 3 * 2 * 1
which equals 120
.
So
Example Solution
defmodule Factorial do
def of(1), do: 1
def of(n) do
n * of(n - 1)
end
end
Our factorial function only needs to handle inputs above 1
.
Implement the Factorial
module as documented below.
defmodule Factorial do
@doc """
Find the factorial of an integer.
## Examples
iex> Factorial.of(1)
1
iex> Factorial.of(2)
2
iex> Factorial.of(3)
6
iex> Factorial.of(4)
24
iex> Factorial.of(5)
120
iex> Factorial.of(10)
3628800
"""
def of(n) do
end
end
Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish factorial exercise"
Previous | Next |
---|---|
Lucas Numbers | Pascals Triangle |