Powered by AppSignal & Oban Pro

Lucas Numbers

exercises/lucas_numbers.livemd

Lucas Numbers

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

Navigation

Return Home Report An Issue

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

Mark As Completed

file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")

save_name =
  case Path.basename(__DIR__) do
    "reading" -> "lucas_numbers_reading"
    "exercises" -> "lucas_numbers_exercise"
  end

progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()

default = Map.get(existing_progress, save_name, false)

form =
  Kino.Control.form(
    [
      completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
    ],
    report_changes: true
  )

Task.async(fn ->
  for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
    File.write!(
      progress_path,
      Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
    )
  end
end)

form

Commit Your Progress

Run the following in your command line from the curriculum folder to track and save your progress in a Git commit. Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.

$ git checkout -b lucas-numbers-exercise
$ git add .
$ git commit -m "finish lucas numbers exercise"
$ git push origin lucas-numbers-exercise

Create a pull request from your lucas-numbers-exercise branch to your solutions branch. Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.

DockYard Academy Students Only:

Notify your teacher by including @BrooklinJazz in your PR description to get feedback. You (or your teacher) may merge your PR into your solutions branch after review.

If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.

Up Next

Previous Next
Fibonacci Factorial