Skip to content

Instantly share code, notes, and snippets.

@StarWar
Forked from dhc02/fibonacci.ex
Created October 29, 2018 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StarWar/fa8698e1ed0e9f18baaf64ad0ecb37d7 to your computer and use it in GitHub Desktop.
Save StarWar/fa8698e1ed0e9f18baaf64ad0ecb37d7 to your computer and use it in GitHub Desktop.
Fibonacci sequence in Elixir
defmodule Fibonacci do
def find(nth) do
list = [1, 1]
fib(list, nth)
end
def fib(list, 2) do
Enum.reverse(list)
end
def fib(list, n) do
fib([hd(list) + hd(tl(list))] ++ list, n - 1)
end
end
# Usage:
# > find 7
# [1, 1, 2, 3, 5, 8, 13]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment