Skip to content

Instantly share code, notes, and snippets.

@zporter
Created January 29, 2018 16:06
Show Gist options
  • Save zporter/b1a5757a37a616014b473605010df37b to your computer and use it in GitHub Desktop.
Save zporter/b1a5757a37a616014b473605010df37b to your computer and use it in GitHub Desktop.
Elixir: Fibonacci
defmodule Fibonacci do
@moduledoc """
In mathematics, the Fibonacci numbers are the numbers in the following
integer sequence, called the Fibonacci sequence, and characterized by the
fact that every number after the first two is the sum of the two preceding
ones.
"""
@doc """
Finds the nth fibonacci number.
"""
def nth(0), do: 0
def nth(1), do: 1
def nth(n) do
nth(n - 1) + nth(n - 2)
end
end
iex(1)> c "fibonacci.ex"
[Fibonacci]
iex(2)> Fibonacci.nth 7
13
iex(3)> Fibonacci.nth 15
610
iex(4)> Fibonacci.nth 20
6765
iex(5)> Fibonacci.nth 25
75025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment