Find the Factorial of a number by tabulation using decorators
def factorial(n):
if n<1:
return 1
else:
f=[0]*(n+1) #creation of the array/list#
f[0]=1 #initialization#
for i in range(1,n+1):#the loop#
f[i]=i*f[i-1] #the recursion + adding in the list#
return f[n]
Now How do i use a decorator to make this work? Following is my attempt ''' #tabulation (down to up) with decorator
import time
def tabulation(func):
def inner(*args,**kwargs):
n=str(args)+str(kwargs)
n=int(n[1])
m=[0]*(n+1)
if n not in m:
m[n]=func(*args,**kwargs)
print(m)
return(m[n])
return inner
@tabulation
def fact(n):
if n<=1:
return n
else:
return n*fact(n-1)
start=time.time()
f=fact(5)
end=time.time()
start=time.time()
g=fact(10)
end=time.time()
print(f," ",end-start)
print(g," ",end-start)
print(m)
'''
The table should be defined ouside the inner function so that it can be shared across calls. However, with the table defined outside the inner function, it can't be initialized with any items since
nis not known until the inner function is called. Instead, grow the table dynamically with thelist.extendmethod as needed:so that:
outputs:
Demo: Try it online!