# Weird results of calculating a sum of binomials?

In[1]:= Sum[(-1)^k*Binomial[n, k]*Binomial[k, j], {k, 0, n}]

Out[1]= (j Binomial[0, j])/(j - n)


According to this output, for any j,n, such as j=n, the result of this sum should be indeterminate, thanks to the division by zero. And indeed:

In[2]:= Sum[(-1)^k*Binomial[n, k]*Binomial[k, j], {k, 0, n}] /. {j -> 2, n -> 2}

During evaluation of In[2]:= Power::infy: Infinite expression 1/0 encountered. >>

During evaluation of In[2]:= Infinity::indet: Indeterminate expression 0 ComplexInfinity encountered. >>

Out[2]= Indeterminate


This seems wrong. Because, if I manually substitute the j's and n's with 2's, Mathematica itself claims that:

In[3]:= Sum[(-1)^k*Binomial[2, k]*Binomial[k, 2], {k, 0, 2}]

Out[3]= 1


Well... 1 is far enough from Indeterminate, isn't it? Actually, if my calculations are right, the correct result is like this: $\left(-1\right)^{\left[2\nmid j\right]}\left[n=j\right]$ (the brackets here are the Iverson brackets)

I suspect a bug. However, I'd really like to ascertain I'm not wrong. Too many times Mathematica has been giving apparently wrong results to me because of my own lack of understanding its peculiarities.

So, could you kindly tell me, if the above results are because of my errors, or is it a bug that should be reported to WRI? Thanks!

• Put a GenerateConditions -> True in your sum and it will tell you when the answer is valid. Mar 11, 2015 at 23:50

Expanding on the comment by @wxffles

Sum[(-1)^k*Binomial[n, k]*Binomial[k, j], {k, 0, n},
GenerateConditions -> True]


For the case when j == n,

Sum[(-1)^k*Binomial[n, k]*Binomial[k, n], {k, 0, n},
GenerateConditions -> True]


Combining these results

sum[j_, n_] =
Piecewise[{{Sum[(-1)^k*Binomial[n, k]*Binomial[k, j], {k, 0, n}],
j != n && Element[n, Integers] &&
n >= 0}, {Sum[(-1)^k*Binomial[n, k]*Binomial[k, n], {k, 0, n}],
j == n && Element[n, Integers] && n >= 0}}]


Table[sum[j, n], {j, 0, 5}, {n, 0, 5}] // TableForm


However, j need not necessarily be an integer

Plot[Evaluate[Table[sum[j, n],
{n, 0, 5}]], {j, 0, 5},
PlotLegends -> Range[0, 5]]


• If MMA's output is correct only for some special cases... why doesn't MMA automatically test the separable cases and return a Piecewise by itself...? Mar 12, 2015 at 11:51
• @gaazkam - presumably in the trade-off between overall execution efficiency versus explicitly handling every possible special case when providing results, WRI chose overall efficiency. Mar 12, 2015 at 21:37