2

I have been asking this question to $\tt AI$ ( I usually ask math questions to $\tt AI$) but its so frustratingly stubborn with not giving me the answer i want. I want to know:

  • If you roll a $m$-sided dice $n$ times, what is the total amount of combinations for it to sum up to $k$ ?, where the order does not matter.
  • Now, the important thing is i do NOT want to count the partitions of this.
  • I want to know if there is a formula to do this.
  • So far the only answers i have been able to find is where the dice are not identical.
  • Or where you have to list out every possibility.
  • I just want to know if there is a formula for this, no matter how complicated it might be.
Felix Marin
  • 94,079
Mappaya
  • 21
  • 2
    I take it your dice have numbers on the sides, the numbers $1,2,3,\dotsc,m$ on each die. So you want to know the number of solutions of $a_1+a_2+\dotsb+a_n=k$, subject to $1\le a_i\le m$ for all $i$. This sort of question has been asked and answered on this website multiple times. Please conduct a search for it here. – Gerry Myerson Nov 21 '24 at 11:52
  • 1
    @GerryMyerson I disagree. The $~a_1 + a_2 + \cdots + a_n = k~$ enumeration regards each of the $~n~$ variables as distinct. This analogizes to regarding the order that each of the $~n~$ die throws occur as being relevant. The original poster has specifically specified that the order that the $~n~$ die throws occur is to be regarded as not relevant. – user2661923 Nov 21 '24 at 14:25
  • 1
    In other words, OP is asking for number of partitions of $k$ using $n$ parts where each part $\in { 1, 2, \ldots m } $. – MyMolecules Nov 21 '24 at 15:56
  • @user, OK, maybe OP can clear this up: suppose $m=6$, $n=2$, and $k=7$. Are you looking for the answer six, or for the answer three? – Gerry Myerson Nov 21 '24 at 21:03
  • @MyMol, please see my most recent comment. – Gerry Myerson Nov 21 '24 at 21:03
  • If, Mappaya, you are looking for the number of partitions of $k$ into $n$ parts, no part exceeding $m$, I think you'll find that there are no general formulas, only formulas for some small values of the parameters. Also, let me call your attention to several questions listed on this page under Related. – Gerry Myerson Nov 21 '24 at 21:07
  • 2
    @GerryMyerson Good question to ask. While we wait a few days for the original poster to respond, if $~(m,n,k) = (6,2,7)~$ then my interpretation of the problem, consistent with what the OP (i.e. original poster) wrote, but perhaps not what they intended, is the answer $~3,~$ represented by $~{ ~{ ~1,6~}, ~{ ~2,5 ~}, ~{ ~3,4 ~} ~}.~$ Also, for what it's worth, I ended up deleting my answer. When I found and tried to correct analytical errors in my answer, I concluded that the algorithm was simply too complicated to publish. – user2661923 Nov 21 '24 at 22:16
  • @GerryMyerson im looking for the answer 3. I know (barely) how to calculate this when order matters (if the dice are unique for example). I have looked it up further and it seems the ways to calculate it are very difficult :(. – Mappaya Nov 22 '24 at 09:06
  • OK, then you are indeed looking for partitions of $k$ into $n$ parts, no part exceeding $m$, and, yes, it's complicated. – Gerry Myerson Nov 22 '24 at 09:18
  • @GerryMyerson Convoluted answer given. – user2661923 Nov 23 '24 at 06:45
  • @MyMolecules Convoluted answer given. – user2661923 Nov 23 '24 at 06:45
  • Why does this question mention dice? I can imagine that there are reasons to count this kind of partition of $k$, but dice-rolling seems to demand different questions. – David K Nov 23 '24 at 07:19
  • @DavidK Apparently, the dice-rolling analogy was the easiest for the original poster to visualize, when they were constructing the problem. – user2661923 Nov 23 '24 at 07:28
  • @user2661923 OK, I can imagine arranging dice to add up to $k$. Rolling is an incredibly inefficient way to do that, and evokes thoughts of probability (which this question had better not be about). Just a word to the wise to OP: if you want to count arrangements, don't use the word "roll." – David K Nov 23 '24 at 07:33

2 Answers2

2

$\underline{\text{Overview}}$

The best that I can do is provide an algorithm that can routinely be converted into a computer program that takes the values of $~m, ~n, ~$ and $~k~$ as parameters. Then, you can run the program to perform any $~(m,n,k)~$ enumeration.

You want the number of non-negative integer solutions to

  • $x_1 + x_2 + x_3 + \cdots + x_m = n.$

  • $[1 \times x_1] + [2 \times x_2] + [3 \times x_3] + \cdots + [m \times x_m] = k.$

For $~i \in \{1,2,\cdots,m\},~$ I am construing the variable $~x_i~$ to represent the number of die throws that showed the value $~i.~$

As will be discussed in the next section, a solution will be possible if and only if:

$$n \leq k \leq (m \times n). \tag1 $$

That is, the minimum sum possible of $~n~$ die throws occurs if each number shows $~1,~$ and the maximum sum possible of $~n~$ die throws occurs if each number shows $~m.$

So, I will assume, without loss of generality that the inequalities in (1) above hold.


$\underline{\text{When is a Solution Possible ?}}$

Continuing the discussion from the end of the previous section, if $~n = k~$ or $~k = (m \times n),~$ then a solution is clearly possible.

So, what happens if $~n < k < (m \times n)~?$

Choose $~i \in \{1,2,\cdots, (m-1)\},~$ such that

$$[n \times i] \leq k < [n \times (i+1)].$$

Then, set

$$x_i = [n - (k - ni)], ~x_{i+1} = (k - ni) \implies $$

$$x_i + x_{i+1} = n, ~~[i ~x_i] + [(i+1) ~x_{i+1}] = k.$$

Therefore, a solution will be possible if and only if the inequalities in (1) above hold.


$\underline{\text{Nested Summations Strategy}}$

Assume a range of values has been determined, so that a solution will be possible if and only if $~x_m~$ is in this range.

Then, assuming that $~x_m~$ is some arbitrary specific value in this range, assume that a corresponding range of values has been determined for $~x_{m-1},~$ based on the specific value of $~x_m.$

Continuing in this manner, for each $~i \in \{2,3,\cdots,(m-2)\},~$ assume that specific in range values have been assigned for each of $~x_{i+1}, ~x_{i+2}, ~\cdots, ~x_m.~$

Now assume that based on the values assigned to each of $~x_{i+1}, ~x_{i+2}, ~\cdots, ~x_m,~$ a specific range of values is determined for $~x_i.~$

Then, once the variables $~x_m, ~x_{m-1}, \cdots, ~x_2~$ are assigned specific values, then the variable $~x_1~$ is determined.

So, the result is an enumeration that looks like:

$$\sum_{x_m ~\text{in range}} \left\{ ~\sum_{x_{m-1} ~\text{in range}} \left[ ~\sum_{x_{m-2} ~\text{in range}} \cdots \left( ~\sum_{x_2 ~\text{in range}} ~1 ~\right) \cdots ~\right] ~\right\}. \tag2 $$

So, the entire problem is reduced to determining the specific range of values allowable for $~x_m,~$ and then, for each $~i \in \{2,3,\cdots,m-1\},~$ determining the allowable range of values for $~x_i,~$ based on the assigned in range values of $~x_{i+1}, ~x_{i+2}, \cdots, x_m.$


$\underline{\text{Minimum and Maximum Allowable Values}}$
$\underline{\text{for} ~x_i ~: ~i \in \{2,3,\cdots,m\}}$

A specific value for $~x_m~$ will be allowable if and only if there exists at least one solution to

  • $x_1 + x_2 + \cdots + x_{m-1} = n - x_m.$

  • $[1 \times x_1] + [2 \times x_2] + [3 \times x_3] + \cdots + [(m-1) \times x_{m-1}] = k - (m \times x_m).$

Based on the previous analysis, which verified the inequalities in (1) above, a solution will exist if and only if

$$(n - x_m) \leq [k - (m \times x_m)] \leq [(m-1) \times (n - x_m) \iff $$

$$k - [(m-1)n] \leq x_m \leq \left\lfloor \frac{k - n}{m-1}\right\rfloor. \tag3 $$

Note
If $~k < [(m-1)n],~$ then the lower bound for $~x_m~$ is $~0.$

Similarly, for $~i \in \{2,3,\cdots,m-1\},~$ assume that specific in range values have been assigned to the variables $~x_{i+1}, ~x_{i+2}, \cdots, x_{m-1}, ~x_m.$

Let

$$S = [ ~n - (x_m + x_{m-1} + \cdots + x_{i+1}) ~],$$

and let

$$T = k - \{ ~[m \times x_m] \\+ [(m-1) \times x_{m-1}] + \cdots + [(i+1) \times x_{i+1}] ~\}.$$

Then, a specific value for $~x_i~$ will be allowable if and only if there exists at least one solution to

  • $x_1 + x_2 + \cdots + x_{i-1} = S - x_i.$

  • $[1 \times x_1] + [2 \times x_2] + [3 \times x_3] + \cdots + [(i-1) \times x_{i-1}] = T - (i \times x_i).$

From the previous analysis in this section, that led to the result in (3) above, the allowable range for $~x_i~$ is

$$T - [(i-1)S] \leq x_i \leq \left\lfloor \frac{T - S}{i-1}\right\rfloor. \tag4 $$

Note
If $~T < [(i-1)S],~$ then the lower bound for $~x_i~$ is $~0.$

user2661923
  • 42,303
  • 3
  • 21
  • 46
  • There are cases where it is possible to derive a formula. See, e.g., https://math.stackexchange.com/questions/240427/partitions-of-an-integer-into-k-parts/241797#241797 – Gerry Myerson Nov 23 '24 at 12:12
0

We can use a generating function approach to derive such a formula for the number of combinations, based on this blog post, where the number of dice combinations for $k$ is equal to the coefficient of $z^k$ in the function's power series.

We generally consider the unnormalized generating function of a single die to be:

$$F(z)=\sum_{i=1}^mz^i$$

Which we can rewrite:

$$F(z)=\sum_{i=1}^mz^i=\frac{z(z^m-1)}{z-1}=M(z)N(z)O(z)$$

for $n$ dice, we take this to the $n$th power:

$$[F(z)]^n=\frac{z^n(z^m-1)^n}{(z-1)^n}=[M(z)]^n[N(z)]^n[O(z)]^n$$

At which point we can start breaking this up into it's constituent functions. Let's start with the simplest, $[M(z)]^n$:

$$[M(z)]^n=z^n$$

Then let's look at $[N(z)]^n$, a clear binomial if ever I've seen one:

$$\begin{align*}[N(z)]^n&=(z^m-1)^n\\ &=\sum_{k=0}^{n}\binom{n}{k}z^{mk}(-1)^{n-k} \end{align*}$$

We can multiply $[M(z)]^n$ and $[N(z)]^n$ together:

$$[M(z)]^n[N(z)]^n=\sum_{k=0}^{n}\binom{n}{k}z^{mk+n}(-1)^{n-k}$$

Now, we can look at the denominator, $[O(z)]^n$, which if we look closely is a negative binomial:

$$\begin{align*}[O(z)]^n&=\frac{1}{(z-1)^n}=(z-1)^{-n}\\ &=(-1)^{-n}(1-z)^{-n}\\ &=(-1)^{-n}\sum_{j=0}^{\infty}\binom{n+j-1}{n-1}z^j \end{align*}$$

Thus (with a little simplification around the common$(-1)$):

$$[F(z)]^n=\left(\sum_{k=0}^{n}\binom{n}{k}(-1)^{k}z^{mk+n}\right)\left(\sum_{j=0}^{\infty}\binom{n+j-1}{n-1}z^j\right)$$

Because the two sums have differing parameters, we normally wouldn't be able to combine these, but since the binomial expansion can be considered an infinite sum (just one where it will equal zero where $k > n$), we can take the Cauchy product of the two series to get the coefficients (which is our goal here). As such, we can view the final result as:

$$[F(z)]^n=\sum_{k=0}^{\infty}\left ( \sum_{j=0}^{\left \lfloor \frac{k-n}{m}\right \rfloor}(-1)^j\binom{n}{j}\binom{k-nj-1}{n-1} \right )z^k$$

Since we're only looking for the coefficient, we can break that out of our power series as:

$$f(n,m,k)=\sum_{j=0}^{\left \lfloor \frac{k-n}{m}\right \rfloor}(-1)^j\binom{n}{j}\binom{k-nj-1}{n-1}$$

And to turn this into a probability mass function, we just need to normalise by dividing the result by $\frac{1}{m^n}$:

$$P(K=k)=\frac{1}{m^n}\sum_{j=0}^{\left \lfloor \frac{k-n}{m}\right \rfloor}(-1)^j\binom{n}{j}\binom{k-nj-1}{n-1}$$