Python 3.12.5
The algorithm for calculating the value of the function F(n), where n is a natural number, is given
by the following ratios:
F(n) = 1 for n = 1;
F(n) = 2 for n = 2;
F(n) = n * (n - 1) + F(n - 1) + F(n - 2) if n > 2.
What is the value of the expression F(2024) - F(2022) - 2 * F(2021) - F(2020)?
At first I just used recursion with the big limit, but I found out that the program runs TOOOOOOO slow (because I didn't use @lru_cache), I watched a video with the solution of this task and used this code
from functools import lru_cache
import sys
sys.setrecursionlimit(50000000)
@lru_cache
def F(n):
if n == 1:
return 1
if n == 2:
return 2
return n*(n-1) + F(n-1) + F(n-2)
print(F(2024) - F(2022) - 2*F(2021) - F(2020))
It worked for the author of the video. When I had tried it in an online interpretator, it worked too and I got the correct answer. But it doesn't work on my PC. I get RecursionError although I set the recursion limit to 50000000:
return n*(n-1) + F(n-1) + F(n-2)
^^^^^^
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
What do I do? Maybe there are some other solutions?