6

For example, if $n = 2$, and $s=7$ one could have $(1,6), (2,5), (3,3), (4,3), (5, 2), (6,1)$, for six ways.

If $n = 10$ and $s = 30$, one possible combination could be $(1, 2, 3, 4, 3, 2, 1, 5, 6, 3)$.

Mike Earnest
  • 84,902
user2316667
  • 173
  • 6

4 Answers4

5

The "easy" way of solving problems like this is with generating functions. If you're unfamiliar with them, there's a great introduction by Herb Wilf that shows a lot of their versatility and applicability. Honestly, it's one of the greatest books of all time.

In your problem, let's say you roll $n$ dice and want their sum to be $s$. Then, the generating-function way to represent a roll is the polynomial $r(x)=x+x^2+x^3+x^4+x^5+x^6$. Each of the six terms has a coefficient of 1, meaning that with a single die there is exactly one way to roll each value from one to six. Then, the way to represent a series of $n$ rolls is with a product of $n$ factors of $r(x)$--- in the sense that, for any sum $s$, the coefficient of $x^s$ in the polynomial $(r(x))^n$ is the number of ways to roll $n$ dice with a sum of $s$. By the elegance of generating functions, your entire problem is now reduced to finding the coefficient of a polynomial.

Granted, finding this coefficient takes some work. Notation-wise, let $[x^a]f(x)$ denote the coefficient of $x^a$ in the polynomial or Taylor series $f(x)$. You're interested in $[x^s]r^n(x)$. The polynomial $r(x)$ can be factored as $\frac{x(1-x^6)}{1-x}$. The $n$ factors of $x$ can be removed to get $[x^s]r^n(x)=[x^{s-n}]\frac{1-x^6}{1-x}$. Now the usual rule for multiplying polynomials applies to generating functions, as well: $$[x^a]f(x)\cdot g(x)=\sum_i [x^i]f(x)\cdot[x^{a-i}]g(x).$$ So, with this multiplication rule we have $$[x^{s-n}]\left(\frac{1-x^6}{1-x}\right)^n=\sum_i[x^{6i}](1-x^6)^n\cdot [x^{s-n-6i}]\frac1{(1-x)^n}.$$ By the binomial theorem we have $[x^a](1-x)^b=(-1)^a\binom ba$, and by differentiating a geometric series we have $[x^a]\frac1{(1-x)^{b+1}}=\binom{a+b}a$. Putting these together, we get the number of ways to get a sum of $s$ with $n$ rolls as $$\sum_i(-1)^i\binom ni\binom {s-1-6i}{n-1}.$$

That should shed some light on the answer that Mike Earnest posted above. Numerically, Maple got $4,\!395,\!456$ ways to get a sum of 35 with ten dice.

Rus May
  • 2,357
4

The general formula is this: $$ \mathbb P(\text{$n$ dice numbered $\{1,\dots,d\}$ sum to $t$})=d^{-n}\sum_{i=0}^{\lfloor (t-n)/d\rfloor}(-1)^i\binom{n}{i}\binom{t-di-1}{n-1} $$

Counting the number of ways to have the sum of $n$ dice equal $t$ is equivalent to counting the number of solutions to this integer equation with constraints:

\begin{align} x_1+x_2+\dots+x_n &= t\\ 1\le x_i&\le d\hspace{2cm} \text{ for }i=1,2,\dots,n \end{align} Let us first count solutions without the constraint $x_i\le d$. The number of positive integer sequences $(x_1,\dots,x_n)$ which sum to $t$ is $\binom{t-1}{n-1}$. To see this, imagine a line of $t$ dots, with $t-1$ spaces between the dots. Choose $n-1$ of those spaces, and put a divider in the chosen spaces. These dividers break the blocks into $n$ contiguous groups, corresponding to the values of the $n$ variables $x_1,\dots,x_n$.

Now, subtract the bad solutions where some variable $x_i$ is at least $ d+1$. If we take such a bad variable and subtract $d$, we get a list of positive integers summing to $t-d$. By the same logic as before, there are $\binom{t-d-1}{n-1}$ such bad solutions. We must subtract this quantity for each variable, so we subtract $n\binom{t-d-1}{n-1}$.

However, solutions with two bad variables have been doubly subtracted, so we must add these back in. To account for overlaps, you use the principle of inclusion exclusion. When counting solutions where a particular set of $i$ variables are bad, subtracting $d$ from each of these variables leaves a list summing to $t-di$, the number of which is $\binom{t-di-1}{n-1}$. This leads to the claimed formula.

Mike Earnest
  • 84,902
  • I've revised the page you commented about as "not very clear". I hope it's somewhat better now. – Luca M Feb 20 '15 at 14:06
  • @LucaM I'm very sorry I said that! I understood your explanation when I read it, and thought it was well written, and now it's even better than before. I guess I meant that the OP might not find it helpful, because I didn't have evidence that they had a strong math background; sometimes no matter how good the argument, if you are adverse to math it won't be helpful. I meant no offense; it's easy to forget that all these websites are written by real people who dedicated their time to help the world understand math, and I appreciate what you've done. – Mike Earnest Feb 20 '15 at 18:54
  • 1
    Thanks Mike. No needs of feeling sorry. As said in my answer your comment was a spur to make me try to improve the page and to make it more accessible to "curious" people. Sure it took much time to write (and re-write) it and to try to be as smooth and clear as possible. I just hope the line of explanation I followed can be useful for someone else. – Luca M Feb 20 '15 at 20:43
3

To avoid having to use the infinite symbol in the sum, the probability to have a sum of all dice equal to $k$ when rolling $n$ dice can also be written as: $$P\left( {n,k} \right) =\frac{1}{{{6^n}}} \cdot \sum\limits_{i = 0}^{{i_{\max }}} {{{\left( { - 1} \right)}^i}\left( {\begin{array}{*{20}{c}} n\\ i \end{array}} \right)\left( {\begin{array}{*{20}{c}} {k - 6i - 1}\\ {k - 6i - n} \end{array}} \right)}$$ where $\displaystyle {i_{\max }} = \left[ {\frac{{k - n}}{6}} \right]$ and $\left[ x \right]$ represents the "floor" function. For instance, the probability to have a sum $k=31$ when rolling $n=10$ dice is: $$P\left( {10,31} \right) = \frac{1}{{{6^{10}}}} \cdot \sum\limits_{i = 0}^3 {{{\left( { - 1} \right)}^i}\left( {\begin{array}{*{20}{c}} {10}\\ i \end{array}} \right)\left( {\begin{array}{*{20}{c}} {31 - 6i - 1}\\ {31 - 6i - 10} \end{array}} \right)} =$$ $$ \frac{1}{{{6^{10}}}} \cdot \left[ {\left( {\begin{array}{*{20}{c}} {30}\\ {21} \end{array}} \right) - \left( {\begin{array}{*{20}{c}} {10}\\ 1 \end{array}} \right)\left( {\begin{array}{*{20}{c}} {24}\\ {15} \end{array}} \right) + \left( {\begin{array}{*{20}{c}} {10}\\ 2 \end{array}} \right)\left( {\begin{array}{*{20}{c}} {18}\\ 9 \end{array}} \right) - \left( {\begin{array}{*{20}{c}} {10}\\ 3 \end{array}} \right)\left( {\begin{array}{*{20}{c}} {12}\\ 3 \end{array}} \right)} \right] =$$ $$\frac{{3393610}}{{60466176}} = 5.612\% $$ I tried to explain "step-by-step" the inner workings of that formula , using pure combinatorics arguments in my site, at this link, previously cited by Mike Earnest and this proved to be not an easy task.

Mike's comment about that page was not positive but I must admit he was right about the (previous) explanation not being very clear.

So I've recently completely rewritten the page at lucamoroni.it, trying to improve it.

Certainly the answer by Rus May and the content of this article by mathworld are a much more simple and easy way to justify the formula, but they are mainly based on algebraic methods and I looked for another kind of insight.

Luca M
  • 405
0

So I came up with two algorithmic solutions. I will accept any formulaic answers though, if they come up.

The first is a naive solution by permutating through all options. Running time: O(6^n):

private static int counter = 0;
public static int calculateNumberOfWaysNDiceAverageToANaive(int N, double A, int sum, int currentDice) {
    if (N==currentDice) {
        if ( sum == A*N ) counter++;
        return 0;
    }
    for (int i = 1; i <=6; i++) calculateNumberOfWaysNDiceAverageToANaive(N, A, sum+i, currentDice+1);
    return counter;
}

The second uses dynamic programming. Running Time: O(A*N^2):

public static int calculateNumberOfWaysNDiceAverageToADynamic(int N, double A) {
    double desiredSum = A*N;
    if ((int) desiredSum != desiredSum ) {
        System.out.println("Invalid Average");
        return 0;
    }
    int[][] S = new int[N+1][(int) desiredSum + 1];
    // Let S[i][j] represent the number of ways i dice can sum to j
    for ( int i = 1; i <= desiredSum; i++) {
        if ( i <=6) {
            S[1][i] = 1;
        }else{
            S[1][i] = 0;
        }
    }

    for ( int i = 2; i <= N; i++ ) {
        for ( int j = i; j <= desiredSum; j++) {
            for ( int k = 1; k<=6; k++ ) {
                if ( j-k >= i-1 ) { // the sum j-k must at least be equal to i-1 dice, if all of them give 1s
                    S[i][j] += S[i-1][j-k]; 
                }
            }
        }
    }
    return S[N][(int) desiredSum];
}

Disclaimer: I'm not a computer scientist and these were not thoroughly tested at all. I'm not even sure if the running times are accurate. Still, they worked out for all that I tested including n = 10, A = 3.5 and n = 10 and A=2.5

user2316667
  • 173
  • 6