Skip to content

Latest commit

 

History

History
90 lines (65 loc) · 1.96 KB

factorial.livemd

File metadata and controls

90 lines (65 loc) · 1.96 KB

Factorial

Mix.install([
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:tested_cell, github: "brooklinjazz/tested_cell"},
  {:utils, path: "#{__DIR__}/../utils"}
])

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
Loading

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

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"

Up Next

Previous Next
Lucas Numbers Pascals Triangle