Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Factorial

factorial.livemd

Factorial

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"},
  {:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])

Navigation

Return Home Report An Issue

Factorial

The factorial of a $n$ is the sum of the sequence of numbers leading up to $n$.

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 $factorial(n) = n * factorial(n - 1)$

In the Elixir cell below, implement the Factorial.of/1 function.

Factorial.of(1)
1

Factorial.of(2)
2

Factorial.of(5)
120

Factorial.of(10)
3628800
defmodule Factorial do
  def of(n) do
  end
end

Validate Your Solution

To test your solution, ensure the following code does not crash with a MatchError.

1 = Factorial.of(1)

2 = Factorial.of(2)

120 = Factorial.of(5)

3_628_800 = Factorial.of(10)

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish factorial exercise"