Fibonacci Sequence
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"},
{:benchee, "~> 1.1"}
])
Navigation
Home Report An Issue RecursionLucas NumbersFibonacci Sequence
The Fibonacci Sequence is a series of the following numbers.
flowchart LR
a[0] --> b[1] --> c[1] --> d[2] --> e[3] --> f[5] --> g[8] --> h[13] --> i[21] --> k[34] --> ...
It’s created by taking the sum of the previous to numbers to get the next number.
So $fib(n) = fib(n-1) + fib(n - 2)$
You’ll notice that this is a recursive function. You’re going to create a Fibonacci
module that can generate the $nth$ fibonacci number.
Example Solution
defmodule Fibonacci do
def of(0), do: 0
def of(1), do: 1
def of(n) do
of(n - 1) + of(n - 2)
end
end
Implement the Fibonacci
module as documented.
defmodule Fibonacci do
@moduledoc """
Documentation for the `Fibonacci` module.
"""
@doc """
Generate the nth fibonacci number.
## Examples
iex> Fibonacci.of(0)
0
iex> Fibonacci.of(1)
1
iex> Fibonacci.of(2)
1
iex> Fibonacci.of(3)
2
iex> Fibonacci.of(4)
3
iex> Fibonacci.of(5)
5
iex> Fibonacci.of(6)
8
iex> Fibonacci.of(20)
6765
"""
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 Fibonacci Sequence 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.