2018-01-12 01:27:36 -06:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from functools import lru_cache
|
|
|
|
|
2018-01-12 01:40:46 -06:00
|
|
|
|
2018-01-12 01:27:36 -06:00
|
|
|
@lru_cache(maxsize = 1000)
|
|
|
|
def fibonacci(n):
|
2018-01-12 01:40:46 -06:00
|
|
|
# check input type. must be positive integer.
|
|
|
|
if type(n) != int:
|
|
|
|
raise TypeError("n must be a positive int")
|
|
|
|
if n < 1:
|
|
|
|
raise ValueError("n must be a positive int")
|
|
|
|
|
|
|
|
# compute the nth term
|
2018-01-12 01:27:36 -06:00
|
|
|
if n == 1:
|
|
|
|
return 1
|
|
|
|
elif n == 2:
|
|
|
|
return 1
|
|
|
|
elif n > 2:
|
|
|
|
return fibonacci(n-1) + fibonacci(n-2)
|
|
|
|
|
|
|
|
|
|
|
|
for n in range(1, 501):
|
|
|
|
print(n, ":", fibonacci(n))
|