Created
November 26, 2024 13:19
-
-
Save MikaelFangel/1b7776a9e3f292e0bf050ce971a18611 to your computer and use it in GitHub Desktop.
Tailcall optimized fibonacci in Elixir
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 | |
def fibonacci(n) when n >= 0, do: fibonacci(n, 0, 1) | |
defp fibonacci(0, current, _next), do: current | |
defp fibonacci(n, current, next), do: fibonacci(n - 1, next, current + next) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment