0

Following are two different dynamic programming codes for fibonacci series. I cannot figure out the time complexity of these. Results of both are the same. Any help is appreciated.

#Code 1
def fibo(n) :
    if mem[n] is not None:
        return mem[n]

    if n == 1 or n == 2 :
        res = 1
    else :
        res = fibo(n - 1) + fibo(n - 2)

    mem[n] = res
    return res

n = int(input("Enter position :"))
mem = [None] * (n + 1)
fibo(n)

Code 2

def fibo(n) :
    if len(mem) == n-1 :
        return mem[n-1]

    if n == 1 or n == 2 :
        res = 1
    else :
        res = fibo(n - 1) + fibo(n - 2)

    mem.append(res)
    return res

n = int(input("Enter position :"))
mem = []
fibo(n)

0 Answers0