Making Change for a Dollar (and other number partitioning problems)
I was trying to solve a problem similar to the "how many ways are there to make change for a dollar" problem. I ran across a site that said I could use a generating function similar to the one quoted below:
The answer to our problem (293) is the coefficient of $x^{100}$ in the reciprocal of the following:
$(1-x)(1-x^5)(1-x^{10})(1-x^{25})(1-x^{50})(1-x^{100})$
But I must be missing something, as I can't figure out how they get from that to $293$. Any help on this would be appreciated.
For the record, I'll copy a snippet form this answer to a question that was closed as a duplicate to this question, as it explains exactly how to compute the given coefficient explicitly, which is really the same as the method given in the answer by ray in a more algorithmic formulation. I just give the procedure here, for more explanations see the answer linked to.
Let $c$ denote an array of $101$ integers indexed from $0$ to $100$.
- Initialise your array so that $c[0]=1$ and $c[i]=0$ for all $i>0$.
- For $k=1,5,10,25,50,100$ (in any order) do:
- for $i=0,1,\ldots,100-k$ (in this order) do:
- add $c[i]$ to $c[i+k]$.
- for $i=0,1,\ldots,100-k$ (in this order) do:
- Now $c[100]$ gives your answer.
This computation gives you the coefficient of $x^{100}$ in the power series for $1/((1-x)(1-x^5)(1-x^{10})(1-x^{25})(1-x^{50})(1-x^{100}))$, which equals $293$.
In this particular case there is a trick that will save time and memory, exploiting the fact that all values of $k$ except $k=1$ are divisible by$~5$. Since we are free to choose the order of the$~k$, we can keep $k=1$ for the end, and observe that only the values $c[i]$ with $i$ divisible by$~5$ will be nonzero when we get to the final loop instance for $k=1$. But then we might as well set up the initial loops to compute only them (so do only those $i$ in the inner loop that are divisible by $5$). Also the final loop for $k=1$ is just computing the sum of all entries into $c[100]$, so we might as well take only the sum of the nonzero entries. Thus one gets a procedure that involves only the $21$ multiples of $5$ up to $100$, and can be done even by hand.
I'll add that this is not the asymptotically fastest method to compute coefficient of $x^n$ in the given series for large $n$. It can be done in a constant number of arithmetic operations by making the denominator a power of the least common multiple of the factors written, which in this case would be the sixth power of $1-x^{100}$ as it is divisible by the other factors, for instance $1-x^{100}=(1-x^{25})(1+x^{25}+x^{50}+x^{75})$. The quotients involved to transform all factors into this least common multiple must of course also be multiplied into the numerator to keep the expression equivalent. The numerator now is a polynomial that can be explicitly computed once and for all, and this is to be multiplied by a negative power, here $(1-x^{100})^{-6}$, whose coefficients can be computed (on demand!) using binomial coefficients. To find the coefficient in the result of a single monomial $x^n$ requires multiplying each of the fixed number of the coefficients in the numerator factor by one coefficient from the denominator factor and adding up; all this gives a constant number of operations. This requires more preparation and programming to be done then the method sketched above, which for value like $n=100$ does not really pay off. However, if the question were how many ways there are to make change for a billion (in the sense of thousand million) dollars, using only the same coins (supposing enough existed), one would have $n=10^{11}$ and the correct coefficient 13333333398333333445333333413833333354500000001 for that case would have been hard to compute differently. Bonus question, explain why there are so many repeated digits in this coefficient.
You should be able to compute it using a Partial Fraction representation (involving complex numbers). For instance see this previous answer: Minimum multi-subset sum to a target
Note, this partial fraction expansion needs to be calculated only one time. Once you have that, you can compute the way to make change for an arbitrary amount pretty quickly.
In this case, I doubt they really did that for finding the coefficient of $x^{100}$. It is probably quicker to just multiply out, ignoring the terms which would not contribute to the coefficient of $x^{100}$. Or you could try computing the partial fraction representation of only some of the terms and then multiply out.
Note, if you are multiplying out to find the coefficient of $x^{100}$, it would be easier not to go to the reciprocal, which arises from considering an infinite number of terms.
You just need to multiply out
$$ (\sum_{j=0}^{100} x^j)\ (\sum_{j=0}^{20} x^{5j})\ (\sum_{j=0}^{10} x^{10j})\ (\sum_{j=0}^{4} x^{25j})\ (1 + x^{100})$$
which would amount to enumerating the different ways to make the change (and in fact is the the way we come up with the generating function in the first place).
You could potentially do other things, like computing the $100^{th}$ derivative at $0$, or computing a contour integral of the generating function divided by $x^{100}$, but I doubt they went that route either.
Hope that helps.
We can ease the calculation by noting that the number of ways of changing 100 equals the number of ways of representing the numbers less than or equal to $100$ as the sum of the numbers $5, 10, 25, 50$ and $100$, since the pennies can make up any remaining difference.
Noting that all these number are divisible by $5$ we can conclude that the number of ways of representing $100$ in units of $1, 5, 10, 25, 50$ and $100$ is the sum of the coefficients up to and including the term in $x^{20}$ in the expansion of
$$ \frac{1}{(1-x)(1-x^2)(1-x^5)(1-x^{10})(1-x^{20})} . $$
You "just" have to follow the prescription: find the formal power series (no need to think about convergence) that is defined and check the number that multiplies x^100. There's a reason I put just in quotes. There is no obvious route to 293 that I can see. Mathematica can do it with just one command, but I can't get Alpha to do it.