Created
January 29, 2018 16:06
-
-
Save zporter/b1a5757a37a616014b473605010df37b to your computer and use it in GitHub Desktop.
Elixir: Fibonacci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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