Learn practical skills, build real-world projects, and advance your career

Recusive and Fibonacci

fib(n)=fib(n1)+fib(n2)fib(n) = fib(n - 1) + fib(n - 2)

def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)
%%time
fib(30) 
CPU times: user 238 ms, sys: 3.59 ms, total: 241 ms Wall time: 240 ms
832040
%%time
#
# can't execute with a large number, because it takes too long.
#
fib(40)
CPU times: user 29.3 s, sys: 7.7 ms, total: 29.3 s Wall time: 29.3 s
102334155

With Memoization