# Keeping memory to reduce the running time of recursion [duplicate]

I am using the following recursion in Mathematica to compute W[n, 1, s] for given n and s:

W[1, j_, s_] := B[j, s]
W[n_, j_, s_] := W[n, j, s] = Sum[A[i, s] W[n-1,j+1-i], {i,0,j}]


with functions A and B given. I need the value of W[n, 1, s] for n equal to 2 to some k, and for different values of s. So as you can see I have written the recursion in a way so that it remembers the values it finds with the goal of reducing the time by keeping more memory. However, for higher values of k the computer runs out of memory. Is there a better way of doing this?

## marked as duplicate by J. M. will be back soon♦Jun 10 '13 at 18:14

• Could you please define the functions A and B? – partial81 Jun 10 '13 at 17:55
• They are complicated Laplace transforms (s is the Laplace variable) defined in terms of other LTs (H and X) which are available in closed form. e.g., B[i_, s_] := If[i == 0, ((\[Mu]/(\[Mu] + \[Lambda] + s)) p H[s])/(1 \ - X[s]), ((\[Mu]/(\[Mu] + \[Lambda] + s)) p H[s] \ (X[s])^(i - 1))/((1 - X[s]))^(i + 1)] – Submartingale Jun 10 '13 at 18:02