How to solve recursion relations in mathematica efficiently?

Posted by Qiang Li on Stack Overflow See other posts from Stack Overflow or by Qiang Li
Published on 2011-01-12T02:36:18Z Indexed on 2011/01/12 2:53 UTC
Read the original article Hit count: 182

Filed under:

I have a recursion to solve for.

f(m,n)=Sum[f[m - 1, n - 1 - i] + f[m - 3, n - 5 - i], {i, 2, n - 2*m + 2}] + f[m - 1, n - 3] + f[m - 3, n - 7]
f(0,n)=1, f(1,n)=n

However, the following mma code is very inefficient

f[m_, n_] := Module[{},
  If[m < 0, Return[0];];
  If[m == 0, Return[1];];
  If[m == 1, Return[n];];
  Return[Sum[f[m - 1, n - 1 - i] + f[m - 3, n - 5 - i], {i, 2, n - 2*m + 2}] + f[m - 1, n - 3] + f[m - 3, n - 7]];]

It takes unbearably long to compute f[40,20]. Could anyone please suggest an efficient way of doing this? Many thanks!

© Stack Overflow or respective owner

Related posts about mathematica