9

I have a set of number, and I want to know in how many ways from that set with each number being used zero, once or more times can a certain sum if at all, be achieved. The order doesn't matter. For example, I have a sum of '10' and set of [1,2] and I want to know in how many ways can 1 and 2 be added up to reach 10. Yes, '1' and '2' have to be used more than once.

Example

10 = 1 + 1 + 2 + 2 + 2 + 2

10 = 1 + 1 + 1 + 1 + 2 + 2 + 2

10 = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1

...

There are more possibilities for the example sum. I don't need the way in which the sum is reached i.e (10 = 1 + 1 + 2 + 2 + 2 + 2 or 10 = 1 + 1 + 2 + 2 + 2 + 2 ...) just how many combinations of '1' and '2' give me 10. I am working with larger sets and numbers, so simple approach will be very useful. Thank-you

No the order does not matter for the sum. Whether it is 10 = 2*one + 4*two or 4*two + 2*one or any other permutation of them. The attempt I made involves the use of a computer. I was thinking about something like this.

for (i = 0; i < sum/i; i++):
    for (j = 0; j<sum/j; j++):
        if (num1*i + num2*j == sum):
            numberOfWays +=1

But this is not useful for larger sets as several nested for loops will be cumbersome. I am looking for an elegant solution.

UPDATE A number can be used 0 times.

2 Answers2

9

We can use generating functions.

Take your example of writing n as a sum of $1$s and $2$s, represent the number of possible $1$s as

$$x^{0\cdot 1}+x^{1\cdot 1}+x^{2\cdot 1}+x^{3\cdot 1}+\cdots$$

What is $x$? You might ask, well it isn't anything, it has no value beyond the formality of allowing us to write out a list of possible $1$s in our sum. E.g. $x^{3\cdot 1}$ denotes the possibility of a sum having three $1$s.

We can do the same with the number of possible $2$s by listing

$$x^{0\cdot 2}+x^{1\cdot 2}+x^{2\cdot 2}+x^{3\cdot 2}+\cdots$$

Now since we are interested in all combinations of $1$ and $2$ added, if we multiply these two lists as if they were series then each resulting term will represent a possible sum, this is called a generating function

$$(x^{0\cdot 1}+x^{1\cdot 1}+x^{2\cdot 1}+x^{3\cdot 1}+\cdots)(x^{0\cdot 2}+x^{1\cdot 2}+x^{2\cdot 2}+x^{3\cdot 2}+\cdots)\tag{1}$$

So the expansion will have terms like your first example $x^{2\cdot 1+4\cdot 2}=x^{10}$ and the coefficient in front of this term will be the number of times the sum of $10$ can be achieved using $1$s and $2$s.

We are able to use the expansion $(1-u)^{-1}=1+u+u^2+u^3+\cdots$ and the difference between two squares $a^2-b^2=(a-b)(a+b)$ to help us express the product $(1)$ as

$$\frac{1}{(1-x)(1-x^2)}=\frac{1}{(1-x)^2(1+x)}\tag{2}$$

which can be written using partial fractions

$$\begin{align}\frac{1}{(1-x)^2(1+x)}&=\frac{1}{2(1-x)^2}-\frac{1}{4(1-x)}+\frac{1}{4(1+x)}\\[1ex] &=\frac{1}{2}\sum_{n\ge 0}\binom{n+1}{1}x^n - \frac{1}{4}\sum_{n\ge 0}x^n+\frac{1}{4}\sum_{n\ge 0}(-1)^nx^n\\[1ex] &=\sum_{n\ge 0}\frac{2(n+1)+1+(-1)^n}{4}x^n\end{align}$$

Hence in the simple case of $1$s and $2$s we can derive a formula

$$\text{required count using $1$s and $2$s}=\frac{2(n+1)+1+(-1)^n}{4}$$

So for example for a sum of $n=10$ there are $(2\cdot 11+1+1)/4=24/4=6$ possible sums.

It is possible to derive formulas for larger sets of possible parts (not just parts of size $1$ and $2$) but the maths becomes more long-winded. In general it is better to use the series products like $(1)$ to derive a $2$ dimensional recurrence (a bit like Pascal's Triangle) to calculate relevant coefficients.

An example of the generating function for sums involving $1$s, $2$s and $3$s is

$$\dfrac{1}{(1-x)(1-x^2)(1-x^3)}$$

derived in the same way as for your example.

N. Shales
  • 3,683
7

This is a classic problem for generating functions. Represent the number of ways to represent $n$ as the coefficient of $n$ in a formal power series. If you just had $1$ available, there would be one way to represent each number and the generating function would be $1+x+x^2+x^3+\ldots,$ which we can formally sum to $\frac 1{1-x}$. Similarly if you just had $2$ available you could only represent even numbers and the series would be $1+x^2+x^4+\ldots=\frac 1{1-x^2}$ If you have both $1$s and $2$s available, you multiply them, so the number of ways to write $10$ is the coefficient of $x^{10}$ in the series for $\frac 1{(1-x)(1-x^2)}$, which is $6$ per Alpha When you have only two addends it is much easier because you can choose the number of twos in $6$ ways, from $0$ to $5$, then fill with ones.

Ross Millikan
  • 374,822