# Error solving “stars and bars” type problem

I have what I thought is a fairly simple problem: Count non-negative integer solutions to the equation

$$x_1 + x_2 + x_3 + x_4 + x_5 = 23$$

such that $0 \leq x_1 \leq 9$.

Not too hard, right? Simply ignore the upper-bound, count the $$\begin{pmatrix}23 + (5-1) \\ (5-1)\end{pmatrix} = \begin{pmatrix}27 \\ 4\end{pmatrix} = 17550$$ solutions. Subtract from this all (non-negative integer) solutions to the equation $$y_1 + 10 + x_2 + x_3 + x_4 + x_5 = 23,$$ and there are $\begin{pmatrix}17 \\ 4\end{pmatrix} = 2380$ of these "bad" solutions we shouldn't have counted earlier, but did. Thus we find $17550 - 2380 = 15170$ solutions.

Since this is a prohibitively large number of solutions to check by hand, I wrote a simple Python program to verify whether this answer is correct. It does indeed say there are $17550$ solutions without upper bounds, and $2380$ solutions to the equation for counting "bad" solutions.

However, when I ask it throw away all solutions to the non-upper-bounded problem for which $x_1 \geq 10$, it tells me it's found $15730$ solutions.

My question is: do I not understand the combinatorial calculation so that there are not actually $\begin{pmatrix}27\\4\end{pmatrix}-\begin{pmatrix}17\\4\end{pmatrix}$ solutions, or have I made some kind of programming mistake? Of course, both are also possible.

Your method is correct, there are 15170 solutions. It seems like your python script is doing something wrong at the end.

I wrote a C++ program myself to confirm this:

#include <stdio.h>

int main()
{
int a,b,c,d,e,sum=0;

for (a=0; a<10; a++)
for (b=0; b<24; b++)
for (c=0; c<24; c++)
for (d=0; d<24; d++)
for (e=0; e<24; e++)
{
if (a+b+c+d+e == 23)
sum++;
}

printf("sum=%d",sum);
}


And it prints out sum=15170.

• Thank you, I should have suspected the program, given that it took me longer than expected to get it up and running. I'll have to take some time and see what's gone wrong there. – pjs36 Apr 16 '14 at 19:12

A nice way to mathematically check your work is with a generating function. We have $x_{1} \in \{0, ..., 9\}$. So our generating function for an $x_{1}$ is $\sum_{i=0}^{9} x^{i} = \dfrac{1-x^{10}}{1-x}$. The other terms don't have this restriction, so their generating functions are simply $\dfrac{1}{1-x}$.

So we multiply this result for each $x_{i}$ to get our generating function $f(x) = \dfrac{1-x^{10}}{(1-x)^{5}}$.

Using our binomial identities, we get $(1-x^{10}) = \sum_{i=0}^{1} \binom{1}{i} (-1)^{i} x^{10i} = (1 - x^{10})$

We then expand out the bottom term $\dfrac{1}{(1-x)^{5}} = \sum_{i=0}^{\infty} \binom{i + 5 - 1}{i} x^{i}$. And we multiply this with $(1 - x^{10})$, taking only terms $x^{23}$.

So that gives us $\binom{23 + 5 - 1}{23} - \binom{13 + 5 - 1}{13} = \binom{27}{4} - \binom{17}{4}$ as our answer.

Generating functions are nice, because they make it much easier to model the problem, and you don't have to crank out code and guess if it's right. Of course, seeing the stars and bars solution is nice, but takes more practice. Generating functions are more mechanical once you've figured out how to deal with them.

• I've seen generating functions used for these problems, and it looks like I'd better bite the bullet and figure them out. I dealt with them years ago, but never fully "got it" or figured out how to work with them successfully. Clearly, they're worth the effort! – pjs36 Apr 16 '14 at 19:16
• There is a good tutorial on ordinary generating functions- dreamincode.net/forums/topic/… – ml0105 Apr 16 '14 at 19:56

$\displaystyle S=\sum_{x_1=0}^9 \{x_2+x_3+x_4+x_5=23-x_1\}=\sum_{x_1=0}^9 {26-x_1\choose 3}=\sum_{x_1=0}^9 {17+x_1\choose 3}$

$\displaystyle\quad= \sum_{x_1=0}^9 \left[{18+x_1\choose 4}-{17+x_1\choose 4}\right]={27\choose 4}-{17\choose 4}$