# Sum of sum of a function

I want to evaluate $\sum^{30-b}_{a=2}\sum^{28-c}_{b=2}\sum^{26-d}_{c=2}\sum^{24}_{d=2}1$. But the command

Sum[Sum[Sum[Sum[1, {d, 2, 24}], {c, 2, 26 - d}], {b, 2, 28 - c}], {a, 2, 30 - b}]

only gives 23 (29 - b) (27 - c) (25 - d)

How can I write a code to generate the correct output?

• The sequence of your sums is wrong. Change the order for it to make sense. E.g. the outermost sum goes from a=2 to 30-b but b is already summed over in the sum next to it so it has no value. – AndreasP Oct 19 '16 at 16:13
• My intention is to get the number of solutions of $x_1+x_2+x_3+x_4=30$ where$x_i\geq 2$. How should I construct the correct summation? – user122049 Oct 19 '16 at 16:16
• Maybe someone else can look at doing it your way, but you can do IntegerPartitions[30, {4}] // Select[#, AllTrue[#, # >= 2 &] &] & and look what the functions do in the documentation. Basically you ask mathematica for all sums of 4 integers summing to 30 and filter out those where all the summands are greater than 2. – AndreasP Oct 19 '16 at 16:20

The sum itself should be

Sum[1, {d, 2, 24}, {c, 2, 26 - d}, {b, 2, 28 - c}, {a, 2, 30 - b}]


86802

although I don't know what does it have to do with your comment: "get the number of solutions of $x_1+x_2+x_3+x_4=30$ where $x_i\geq 2$". For this purpose, use

IntegerPartitions[30, {4}, Range[2, 30]] // Length


136

If you want to treat, e.g., {24, 2, 2, 2} as a different solution than {2, 24, 2, 2}:

Permutations /@ IntegerPartitions[30, {4}, Range[2, 30]] //
Flatten[#, 1] & // Length


2300

This is close to what you appear to be trying to do:

Count[Flatten[
Table[Total[{a, b, c, d}],
{d, 2, 24}, {c, 2, 26 - d}, {b, 2, 28 - c}, {a, 2, 30 - b}]], 30]


2300

( @corey979's approach is undoubtedly way faster )

The OP seems to be asking 2 different questions:

For the number of solutions to $x_1+x_2+x_3+x_4=30$ with $x_i\geq 2$

SeriesCoefficient[Series[((x^2 - x^31)/(1 - x))^4, {x, 0, 31}], 30]

(* 2300 *)


Hi george;

You are of course familiar with the concept of a generating function as a tool to solve many combinatorics problems. And many combinatorics problems come down to Diophantine equations just like this one:

$x_1+x_2+x_3+x_4=30$

The number of solutions to the above equation can be represented as the multiplication of 4 polynomials.

$g(x)=\left(\frac{x^2-x^{31}}{1-x}\right)^4= (x^2 + x^3 +x^4 + x^5 + ... x^{30})^4$

Where the rational function on the LHS is called the generating function of the term on the RHS.

Expand[(Table[x^k, {k, 2, 30}] // Total)^4]


If you check the coefficient of x^30 you will see 2300 which is the answer we seek.

All of this can be done better by using the series and series coefficient commands.

SeriesCoefficient[Series[((x^2 - x^31)/(1 - x))^4, {x, 0, 31}], 30]


The above commands are very fast because Mma is very good at multiplying polynomials and getting series for us. The book "Applied Combinatorics" Alan S. Tucker, Chapter 6 is all that is necessary.

• this maybe needs some explanation. – george2079 Oct 19 '16 at 18:13
• @george2079 Will do, right away! – bobbym Oct 19 '16 at 18:26