-
-
Save elbow-jason/3658e80c2b44274f16a9 to your computer and use it in GitHub Desktop.
do fib
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 SimpleFib do | |
def fib(0), do: 0 | |
def fib(1), do: 1 | |
def fib(n) do | |
do_fib(n, 2, 1, 0) | |
end | |
defp do_fib(x, x, prev, prevprev) do | |
prev + prevprev | |
end | |
defp do_fib(n, acc, prev, prevprev) do | |
do_fib(n, acc + 1, prev + prevprev, prev) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that's
104442
microseconds (~0.1 seconds) for the 100_000 (one hundred thousandth fibonacci number)