I have a function that is defined in this way:
F(n) = nif n<=3
F(n) = F(n-1) + 2 * F(n-2) + 3 * F(n-3)if n>3
Now, I've written it as a recursive function and it works fine. I'm trying to write it as an iterative function but i cant seem to make it happen.
The output should be, for example:
print(FRec(5)) => 22
print(FRec(10)) => 1657
print(FRec(15)) => 124905
Any tips?
Here is my recursive implementation:
def FRec(n):
if(n <= 3):
return n
if(n > 3):
return FRec(n - 1) + 2 * FRec(n - 2) + 3 * FRec(n - 3)