TITLE: Count the number of positive solutions for a linear diophantine equation
QUESTION [18 upvotes]: Given a linear Diophantine equation, how can I count the number of positive solutions?
More specifically, I am interested in the number of positive solutions for the following linear Diophantine equation:
$3w + 2x + y + z = 47$
Update: I am only interested in non-zero solutions.

REPLY [6 votes]: For the number of solutions of:
$$ \sum_{k=1}^{m}p_k a_k = M, p_k,a_k \in \mathbb{N} $$
Make a generating function:
$$ P(x) = \prod_{k=1}^{m}\frac{x^{a_k}}{1-x^{a_k}} $$
Notice that the coefficient of the expansion of $P(x)$ standard notation $[x^n]P(x)$ are giving the number of solutions of:
$$ \sum_{k=1}^{m}p_ka_k = n $$
Therefore the number of solutions of
$$ \sum_{k=1}^{m}p_ka_k=M, p_k,a_k \in \mathbb{N} $$
is given by $M$-th derivative at $0$ divided by $M!$
$$\frac{1}{M!}\frac{\mathrm{d^M}P(x) }{\mathrm{d} x^M} (0)$$
Now you can use one of the methods for calculating higher derivative:
$$f^{(n)}=\lim_{h \to 0} \frac{1}{h^n}\sum_{k=0}^{n}(-1)^{k+n}\binom{n}{k}f(x+kh)$$
$$f^{(n)}=\frac{n!}{2\pi i}\oint\limits_\gamma \frac{f(z)}{z^{n+1}} \mathrm{d}z$$
Where $\gamma$ is a circle around the origin.
Notice that the second gives a great way to directly calculate the number of the solutions:
$$S(M)=\frac{1}{2\pi i}\oint\limits_\gamma \frac{P(z)}{z^{M+1}} \mathrm{d}z$$
In your case that is then:
$$S(M)=\frac{1}{2\pi i}\oint\limits_\gamma \frac{1}{z^{41}(1-z^3)(1-z^2)(1-z)^2} \mathrm{d}z$$
Replacing (we have a pole at $1$):
$$z=\frac{1}{2}e^{i\theta}$$
we have:
$$S(47)=\frac{2^{39}}{\pi}\int_{0}^{2\pi} \frac{1}{e^{40i\theta}(1-\frac{1}{8}e^{3i\theta})(1-\frac{1}{4}e^{2i\theta})(1-e^{i\theta})^2} \mathrm{d}\theta$$
(The integral is solved by partial fractions if we are to do it manually or numerically, or for larger values only asymptotically.)
Notice that out of all partial fractions the form $\frac{1}{z^n}$ or $\frac{1}{z^{2n}+z^{n}+1}$ does not count in (the integral being equal to $0$). So we are left with:
$$z=\frac{1}{2}e^{i\theta}$$
$$-\frac{306425}{144(z-1)}+\frac{10577}{72(z-1)^2}-\frac{83}{12(z-1)^3}+\frac{1}{6(z-1)^4}+\frac{1}{16(z+1)}$$
which gives the integral evaluation (counting that odd exponents are changing the sign)
$$\frac{306425}{144}+\frac{10577}{72}+\frac{83}{12}+\frac{1}{6}+\frac{1}{16}=2282$$