Here is the program to generate the Fibonacci series up to the number provided as a command-line argument.
import sys
def fibonacci(n):
if n < 2:
return n
else:
return fibonacci(n - 2) + fibonacci(n - 1)
number = int(sys.argv[1])
print([fibonacci(x) for x in range(number)])
Output, when this code is executed for input number 10, is as below:
rana@brahma:others$ python3 fibonacci.py 10 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
rana@brahma:others$ python3 fibonacci.py 100 ^CTraceback (most recent call last): File "fibonacci.py", line 12, in <module> print([fibonacci(x) for x in range(number)]) File "fibonacci.py", line 12, in <listcomp> print([fibonacci(x) for x in range(number)]) File "fibonacci.py", line 8, in fibonacci return fibonacci(n - 2) + fibonacci(n - 1) File "fibonacci.py", line 8, in fibonacci return fibonacci(n - 2) + fibonacci(n - 1) .... output removed intentionally ...... return fibonacci(n - 2) + fibonacci(n - 1) File "fibonacci.py", line 8, in fibonacci return fibonacci(n - 2) + fibonacci(n - 1) File "fibonacci.py", line 8, in fibonacci return fibonacci(n - 2) + fibonacci(n - 1) File "fibonacci.py", line 8, in fibonacci return fibonacci(n - 2) + fibonacci(n - 1) File "fibonacci.py", line 8, in fibonacci return fibonacci(n - 2) + fibonacci(n - 1) KeyboardInterrupt rana@brahma:others$
rana@brahma:others$ time python3 fibonacci.py 35 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887] real 0m4.098s user 0m4.090s sys 0m0.008s rana@brahma:others$
lru_cache
decorator from functools
.import sys
from functools import lru_cache
@lru_cache()
def fibonacci(n):
if n < 2:
return n
else:
return fibonacci(n - 2) + fibonacci(n - 1)
number = int(sys.argv[1])
print([fibonacci(x) for x in range(number)])
rana@brahma:others$ time python3 fibonacci.py 35 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887] real 0m0.035s user 0m0.026s sys 0m0.009s rana@brahma:others$
lru_cache
decorator wraps the function with memoization callable which saves the most recent calls. So basically it stores the already computed values and reuses them whenever required.maxsize
in lru_cache
decorator. If specified, it will store only maxsize
recent calls.cache_info
function on wrapped function.import sys
from functools import lru_cache
@lru_cache(maxsize=64)
def fibonacci(n):
if n < 2:
return n
else:
return fibonacci(n - 2) + fibonacci(n - 1)
number = int(sys.argv[1])
print([fibonacci(x) for x in range(number)]) # cache effectiveness
print(fibonacci.cache_info())
rana@brahma:others$ time python3 fibonacci.py 35 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887] CacheInfo(hits=66, misses=35, maxsize=64, currsize=35) real 0m0.034s user 0m0.034s sys 0m0.000s rana@brahma:others$
KeyboardInterrupt
exception. Read more about it in the references section below.