Bootstrapping Fibonacci

What happens when we run this code?

1
2
3
4
5
6
7
8
fib_history = {1:1};

def fib_memo(n):
    if n not in fib_history:
        fib_history[n] = fib_memo(n - 2) + fib_memo(n - 1)
    return fib_history[n]

print fib_memo(10)   

Due to zero indexing, it will print out 55 Due to zero indexing, it will print out 34 Nothing happens It will print out 89

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Hasmik Garyaka
Sep 25, 2017

Let call fib memo(2) it will call fib memo(0) and it will call infinite recursion.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...