summaryrefslogtreecommitdiff
path: root/src/code/2/fib_itr.exs
blob: 2eafcee5c4d934a4501676bf435370ff244c97cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
defmodule Fib do
  def seq(0), do: 0
  def seq(1), do: 1
  def seq(n) when n > 1, do: compute_seq(n, 1, [0, 1])

  defp compute_seq(n, i, acc) when n == i do
    Enum.at(acc, length(acc) - 1)
  end
  defp compute_seq(n, i, acc) do
    len = length(acc)
    compute_seq(n, i + 1, acc++[Enum.at(acc, len-1) + Enum.at(acc, len-2)])
  end
end

IO.puts Fib.seq(50)