Skip to content

Latest commit

 

History

History
202 lines (151 loc) · 5.78 KB

lucas_numbers.livemd

File metadata and controls

202 lines (151 loc) · 5.78 KB

Lucas Numbers

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

Navigation

Lucas Numbers

This exercise was inspired by Exercism.io. It's a fantastic platform for learning many languages including Elixir.

Lucas numbers are much like the fibonacci sequence where $fib(n) = fib(n - 1) + fib(n - 2)$ Numberphile has a great video that explains them in further detail,

YouTube.new("https://www.youtube.com/watch?v=PeUbRXnbmms")

Unlike Fibonacci, they start with a different initial 2 numbers.

n 0 1 2 3 4 5 6 7 8 9 10
$Fibonacci(n)$ 0 1 1 2 3 5 8 13 21 34 55
$Lucas(n)$ 2 1 3 4 7 11 18 29 47 76 123

Other than starting with two different numbers, the formula for a lucas number is still $L(n)=L(n-1)+L(n - 2)$.

In addition to generating the $nth$ lucas number, you're also going to generate a sequence of lucas numbers.

Example Solution
defmodule Lucas do

  def number(0), do: 2
  def number(1), do: 1

  def number(n) do
    number(n - 1) + number(n - 2)
  end

  def sequence(length) do
    Enum.map(1..length, &number(&1 - 1)
  end
end

Lucas.sequence(10)

To avoid recomputing lucas numbers over and over, we can implement sequence/1 using Enum.reduce/3 instead.

def sequence(length) when length == 1 do
  [2]
end

def sequence(length) when length == 2 do
  [2, 1]
end

def sequence(length) when length > 2 do
  {_, _, list} =
    Enum.reduce(2..length-1, {2, 1, [1, 2]}, fn each, {prev2, prev1, list} ->
      current = prev2 + prev1
      {prev1, current, [current | list]}
    end)

  Enum.reverse(list)
end

Implement the Lucas module as documented.

defmodule Lucas do
  @doc """
  return the nth lucas number.

  ## Examples

    iex> Lucas.number(0)
    2

    iex> Lucas.number(1)
    1

    iex> Lucas.number(2)
    3

    iex> Lucas.number(3)
    4

    iex> Lucas.number(4)
    7
    
    iex> Lucas.number(5)
    11

    iex> Lucas.number(6)
    18

    iex> Lucas.number(20)
    15127
  """
  def number(n) do
  end

  @doc """
  Generate a list of lucas numbers with the given length.

  ## Examples

    iex> Lucas.sequence(1)
    [2]

    iex> Lucas.sequence(2)
    [2, 1]

    iex> Lucas.sequence(3)
    [2, 1, 3]

    iex> Lucas.sequence(4)
    [2, 1, 3, 4]

    iex> Lucas.sequence(10)
    [2, 1, 3, 4, 7, 11, 18, 29, 47, 76]
  """

  def sequence(length) 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 Lucas Numbers 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