Find all possible arrangements of numbers, but keeping the sum constant

422 Views Asked by At

How would I find all possible combinations of $n$ natural numbers from 1 to 100, in such a way the sum of the $n$ numbers is always 100.

For example if $n = 3$, possible answers would be: $(1, 2, 97)$, $(2, 2, 96)$, and so on.

1

There are 1 best solutions below

0
On BEST ANSWER

Can we repeat numbers?

If we can't the we can do the following way. We can assume $x_1\leq x_2\leq ...\leq x_n$ are the numbers.

Let us count instead the numbers $y_1:=x_1, y_2:=x_2-x_1, ... y_n:=x_n-x_{n-1}$. These are any positive numbers with $y_1+y_2+...+y_n\leq 100$.

We know how to compute the number $S_{k,n}$ of positive solutions of $y_1+y_2+...+y_n=k$.

Then we need $\sum_{k=1}^{100}S_{k,n}$.

If we can repeat numbers then we consider number $S_{k,n}^{0}$ of non-negative solutions instead.

Let us compute $S_{k,n}$ and $S_{k,n}^0$.

To compute $S_{k,n}$ put $k$ sticks on a table one after another.

$$ |\ |\ |\ ...\ |$$

We can divide the sticks into $n$ groups by choosing $n-1$ out of the $k-1$ spaces between the sticks and draw a line in these $n-1$ spaces. The amount in each group is the value of $y_i$.

We have $\binom{k-1}{n-1}$ choices to draw the separating lines. So,

$$S_{k,n}=\binom{k-1}{n-1}.$$

In order to get $S_{k,n}^0$ let us use the that if $y_1,y_2,...,y_n$ is a non-negative solution of $$y_1+y_2+...y_n=k$$ then $(y_1+1),(y_2+1),...,(y_n+1)$ is a positive solution of $$z_1+z_2+...+z_n=k+n$$ The number of positive solutions of this equation is $S_{k+n,n}=\binom{k+n-1}{n-1}$. Therefore

$$S_{k,n}^0=\binom{k+n-1}{n-1}.$$