Tag Info

New answers tagged

4

Here's a way using BlankNullSequence: Clear[p]; p[k_] := p[k] = a[k] pk : p[k0___, k1_, k2_] := pk = p[k0, k1] + b^Length[{k0, k1}] p[k1 + k2] We can test this on an example: p[k1, k2, k3, k4, k5] (* a[k1] + b a[k1 + k2] + b^2 a[k2 + k3] + b^3 a[k3 + k4] + b^4 a[k4 + k5] *) And then check Definition[p] to make sure the values were correctly memoized: ...


3

Here's one way to go about it. Clear[p]; p[k_] := p[k] = If[Length[k] > 1, p[Drop[k, -1]] + b^(Length[k] - 1) a[k[[-2]] + k[[-1]]], a[First[k]]]; For example: p[{k1, k2, k3, k4, k5}] a[k1] + b a[k1 + k2] + b^2 a[k2 + k3] + b^3 a[k3 + k4] + b^4 a[k4 + k5]



Top 50 recent answers are included