Пример рекурсивных фибоначчи до предела
def fib(limit, cache = None):
if cache == None:
cache = [1, 1]
cache.append(sum(cache[-2:]))
if cache[-1] > limit:
# Done-- don't include last item in list
# since its over the limit
return cache[:-1]
else:
# Extend cache with recursive call to
# generate next fibonacci
return fib(limit, cache)
# Test generation
for k in range(1, 10):
print(k, fib(k))
Вывод
1 [1, 1]
2 [1, 1, 2]
3 [1, 1, 2, 3]
4 [1, 1, 2, 3]
5 [1, 1, 2, 3, 5]
6 [1, 1, 2, 3, 5]
7 [1, 1, 2, 3, 5]
8 [1, 1, 2, 3, 5, 8]
9 [1, 1, 2, 3, 5, 8]