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

Factorial

factorial.livemd

Factorial

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue Lucas NumbersPascal’s Triangle

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)$

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

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Factorial exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue Lucas NumbersPascal’s Triangle