sys.setrecursionlimit is set to 20000, but I get error because of "996 more times", despite setting limit to a bigger number import sys from functools import * sys.setrecursionlimit(20000) @lru_cache(None) def f(n): if n <= 3: return n - 1 if n > 3: if n % 2 == 0: return f(n - 2) + (n / 2) - f(n - 4) else: return f(n - 1) * n + f(n - 2) result = f(4952) + 2 * f(4958) + f(4964) print(result)  error: Traceback (most recent call last): File "-", line 21, in <module> result = f(4952) + 2 * f(4958) + f(4964) ^^^^^^^ File "-", line 14, in f return f(n - 2) + (n / 2) - f(n - 4) ^^^^^^^^ File "-", line 14, in f return f(n - 2) + (n / 2) - f(n - 4) ^^^^^^^^ File "-", line 14, in f return f(n - 2) + (n / 2) - f(n - 4) ^^^^^^^^ [Previous line repeated 996 more times] RecursionError: maximum recursion depth exceeded  the answer should be 9200 Why doesn't sys.setrecursionlimit change the limit?
First, your code, as shown above, works fine on my system. Even when I reduce the recursion limit from 20000 down to 4000. However, if you rewrite your expressions to invoke f() on smaller numbers before larger numbers, then I'm able to run it with a recursion limit around 1250: import sys from functools import lru_cache sys.setrecursionlimit(1250) @lru_cache(None) def f(n): if n <= 3: return n - 1 if n % 2 == 0: return - f(n - 4) + f(n - 2) + n/2 return f(n - 2) + f(n - 1) * n result = f(4952) + 2 * f(4958) + f(4964) print(result)  The problem you describe sounds similar to this issue 3.12 setrecursionlimit is ignored in connection with @functools.cache which may also be Windows specific. Try an older or newer Python, possibly on a different architecture.