PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#https://www.sololearn.com/Discuss/3317728/?ref=app
"""
How can a recursive function works perfectly in a for loop with large numbers while calling it too many times raises an error?
https://sololearn.com/compiler-playground/ctNkN4ckuEjK/?ref=app
"""
from sys import setrecursionlimit
from functools import lru_cache
@lru_cache
def fib(n):
return n if n<2 else (
fib(n - 1) + fib(n - 2))
n = 1000
setrecursionlimit((n+1)*2)
print(f"{n = }\n{fib(n) = }\n")
#-- after previous cached fib() call, the such for_loop calling fib() w/o recursion and show all results w/hin SL time.
#for x in range(1, 880):
# print(f"{x = }, {fib(x) = }")
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run