fib_with_memoizing

Ahmed Khaled · @nemoload · almost 3 years

Calculating the n-th number in Fibonacci sequence with dynamic programming.

def fib_with_memoizing(n, mem)
return n if [0, 1].include? n
return mem[n] if mem[n]
mem[n] = fib(n - 2, mem) + fib(n - 1, mem)
end
4 · 1 · 1