# Functions behaving weirdly

The following code

f[k_, n_] :=
Sum[2*(1 + Sum[f[k - j, n - m - j], {j, 1, k - 1}]), {m, 1,
n - k + 1}]
f[4, 7]
f[4, n]


gives the output:

192
-(2/3) (261 - 342 n + 157 n^2 - 30 n^3 + 2 n^4)


But if I then define the function

g[n_] := -(2/3) (261 - 342 n + 157 n^2 - 30 n^3 + 2 n^4)


And evaluate it at $n=7$ I get a different answer:

g


gives the output:

-48


What is going on here?

• Your definition for f seems to be missing some boundary conditions. Jun 8, 2015 at 13:17
• I thought that the fact that the empty sum is assigned the value 0 would be enough to take care of boundary conditions, but perhaps I'm wrong. For example f[1,n] correctly gives the output $2n$. Jun 8, 2015 at 13:35

I think that in the simplification of your sum, Mathematica is assuming that $n - k + 1\geq1$. But that is not true, if you include a Sow:

f[k_, n_] :=
Sum[2*(1 +Sum[Sow[{n - k + 1}]; f[k - j, n - m - j], {j, 1, k - 1}]), {m,1, n - k + 1}]


Then the issue probably starts at $n=4$, as that includes a negative endpoint.

Reap[f[4, n]] /. m -> (n - 4 + 1) /. n -> 7
{-48, {{{4}, {0}, {-4}, {0}, {4}, {0}, {4}}}}

• I see. So how would I go about calculating the polynomials $f[k,n]$ for fixed $k$, and at the same time avoiding this problem? I would like to be able to input a $k$ (e.g. $k=4$) and get $f[k,n]$ as a polynomial in $n$ as an output. Is that at all possible? Jun 8, 2015 at 14:13
• I don't know the answer to that immediately but I suspect that one of the experts could come along and help. I don't have the rep to comment yet (which seems silly to me). Jun 8, 2015 at 14:16