How many non-negative solutions for $x_{1} + x_{2} + x_{3} + x_{4} = 40$ where $2 \leq x_{1} \leq 8, x_{2} \leq 4, x_{3} \geq 4, x_{4} \leq 5$? My solution:
We have:
$x_{1} + x_{2} + x_{3} + x_{4} = 40$ where $2 \leq x_{1} \leq 8, x_{2} \leq 4, x_{3} \geq 4, x_{4} \leq 5$
$\Leftrightarrow  x_{2} + x_{3} + x_{4} = 40 - x_{1} \quad (*)$
Consider:
$x_{2} + x_{3} + x_{4} = 40 - x_{1}$ where $x_{2} \geq 0, x_{3} \geq 4, x_{4} \geq 0 \quad (**)$
$x_{2} + x_{3} + x_{4} = 40 - x_{1}$ where $x_{2} \geq 5, x_{3} \geq 4, x_{4} \geq 6 \quad (***)$
Let $f$ is the function that compute the number of non-negative solutions of an equation.
$\implies f(*) = f(**) - f(***)$ 
Thus, the number of non-negative solutions of (*) is $\sum_{x_{1} = 2}^{8}( {40 - x_{1} + 3 - 1  \choose 3 - 1} - {25-x_{1}+3 - 1 \choose 3 - 1}) = 3045$
I found that the right answer is 210 by trying some programming script. But I don't know what was wrong with my solution. Please help me. Thank you!
 A: $x_1 + x_2 + x_4 \le 8  + 4 + 5 = 17$ so $x_3=40 - x_1 + s_2 + x_4 \ge 40 -17 \ge 4$ so we can ignore the restriction on $x_3$.
$2 \le x_1 \le 6$ so there are $7$ values that $x_1$ can be, $x\le 4$ so there are $5$ values it can be. $x_4 \le 5$ so there are $6$ values it can be and $x_3$ must be $40 - x_1 - x_2 -x_4$ there is only one option dependant on the other three options.
So there $7*5*6 = 210$ options.
Your solution dosn't take $x_3 \ge 4$ into account (which you can be seting it up so that the some is $36$ and not $40$-- I haven't done the math to figure it out but that will lower you answer significantly.  Also by subtracting you are removing the cases with both $x_2 \ge 5$ and $x_4 \ge 6$ but not removing the cases where one or the other is.)
I think to fix your problem using inclusion exclusion you'd want 
$\sum_{x_1=2}^8({{40 - 4 -x_1 + 3-1}\choose {3-1}} - {{40 - 4-5 -x_1 + 3-1}\choose {3-1}}-{{40 - 4-6 -x_1 + 3-1}\choose {3-1}}+{{40 - 4 -5-6-x_1 + 3-1}\choose {3-1}})=$
And I'm too lazy to finish.
A: It's the coefficient of $x^{40}$ of the product polynomial
$$(x^2+x^3+x^4 +x^5 + x^6 + x^7 + x^8)(1+x^1+x^2+x^3 +x^4)(x^4 + x^5 + \ldots)(1+x^1+x^2+x^3 +x^4 + x^5)$$
Or equivalently the coefficient of $x^{34}$ of
$$(1+x+x^2+x^3+x^4 +x^5 + x^6 )(1+x^1+x^2+x^3 +x^4)(1 + x + x^2 + \ldots)(1+x^1+x^2+x^3 +x^4 + x^5)$$
which can be found using (generalised) binomials etc.
A: Math answer
Note that the given constraints for $x_1, x_2$ and $x_4$ and $\sum\limits_{i = 1}^4 x_i = 40$ allows us to define
$$
\begin{aligned}
x_3 &= 40 - x_1 - x_2 - x_4 \\
&\ge 40 - 8 - 4 - 5 \\
&= 23.
\end{aligned}$$
This renders the constraint $x_3 \ge 4$ redundant.  As a result, the required answer is $(8-2+1) \times (4+1) \times (5+1) = 210$.

Julia Programming Script
x1 = 2:8
x2 = 0:4
x4 = 0:5
x3 = [40 - i - j - k for i in x1 for j in x2 for k in x4]
println(minimum(x3))  # returns 23
println(length(x3))   # returns 210

Test this script on Tutorial's Point's online compiler.
