Simply printing the 37th term of Fibonacci using recursion took me 10 seconds(8 GB, Core i5). Tried to find the 50th term, was waiting for like eternity.
def fib(n): if n<=2: f = 1 else: f = fib(n-1) + fib(n-2) return f print fib(37) #10sec, 24157817But,
def fibo(n): fib = {} for k in range(1, n+1): if k<=2: f = 1 else: f = fib[k-1] + fib[k-2] fib[k] = f return fib[k] print fibo(100)#instant, 354224848179261915075Even the 100th Term comes along instantaneously!
No comments:
Post a Comment