PageRenderTime 150ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/fibonacci.exs

http://github.com/josevalim/elixir
Elixir | 19 lines | 16 code | 3 blank | 0 comment | 0 complexity | 0fa6578f95a0df08507002ba57e54a18 MD5 | raw file
  1. % A tail call optimized fibonacci.
  2. module Math
  3. def fibonacci(n)
  4. fibonacci(n, 1, 0)
  5. end
  6. def fibonacci(0, _, result)
  7. result
  8. end
  9. def fibonacci(n, next, result)
  10. fibonacci(n - 1, next + result, next)
  11. end
  12. end
  13. Math.fibonacci(0) % => 0
  14. Math.fibonacci(1) % => 1
  15. Math.fibonacci(3) % => 2
  16. Math.fibonacci(10) % => 55