Every combination of $14$ numbers that would add up to exactly $28$. Numbers can only be between $0$ and $3$.

60 Views Asked by At

I'm trying to find every combination of $14$ numbers that would add up to exactly $28$. Numbers can only be $0$, $1$, $2$, or $3$.

I'm sorry if my wording is poor. I didn't study as much math as I would have liked too prior to getting into programming.

If your interested in the details. I'm trying to come up with an easier way to create spreadsheets to help with an rpg rule set I'm designing. Each possible combination represents a character class. The player allocates $28$ skill points among $14$ skills with no more than $3$ in each skill.

2

There are 2 best solutions below

2
On BEST ANSWER

Here is an algorithm in C, I had a snippet on hand to generate dynamic loops with any number of variables, so here it is:

Try it at tio.run

However are you sure you want to generate that many configurations ?

It turns out there are $6\,472\,168$ of them.

0
On

Have a look to this related post where it is explained that $$N_{\,b} (s,r,m) = \text{No}\text{. of solutions to}\;\left\{ \begin{gathered} 0 \leqslant \text{integer }x_{\,j} \leqslant r \hfill \\ x_{\,1} + x_{\,2} + \cdots + x_{\,m} = s \hfill \\ \end{gathered} \right.$$

is given by $$ N_b (s,r,m)\quad \left| {\;0 \leqslant \text{integers }s,m,r} \right.\quad = \sum\limits_{\left( {0\, \leqslant } \right)\,\,k\,\,\left( { \leqslant \,\frac{s} {r}\, \leqslant \,m} \right)} {\left( { - 1} \right)^k \left( \begin{gathered} m \hfill \\ k \hfill \\ \end{gathered} \right)\left( \begin{gathered} s + m - 1 - k\left( {r + 1} \right) \\ s - k\left( {r + 1} \right) \\ \end{gathered} \right)} $$ You will find there also a recurrence formula.