Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculate the number of ways to roll a certain number
    primarykey
    data
    text
    <p>I'm a high school Computer Science student, and today I was given a problem to:</p> <blockquote> <p>Program Description: There is a belief among dice players that in throwing three dice a ten is easier to get than a nine. Can you write a program that proves or disproves this belief?</p> <p>Have the computer compute all the possible ways three dice can be thrown: 1 + 1 + 1, 1 + 1 + 2, 1 + 1 + 3, etc. Add up each of these possibilities and see how many give nine as the result and how many give ten. If more give ten, then the belief is proven.</p> </blockquote> <p>I quickly worked out a brute force solution, as such</p> <pre><code>int sum,tens,nines; tens=nines=0; for(int i=1;i&lt;=6;i++){ for(int j=1;j&lt;=6;j++){ for(int k=1;k&lt;=6;k++){ sum=i+j+k; //Ternary operators are fun! tens+=((sum==10)?1:0); nines+=((sum==9)?1:0); } } } System.out.println("There are "+tens+" ways to roll a 10"); System.out.println("There are "+nines+" ways to roll a 9"); </code></pre> <p>Which works just fine, and a brute force solution is what the teacher wanted us to do. However, it doesn't scale, and I am trying to find a way to make an algorithm that can calculate the number of ways to roll <i>n</i> dice to get a specific number. Therefore, I started generating the number of ways to get each sum with <i>n</i> dice. With 1 die, there is obviously 1 solution for each. I then calculated, through brute force, the combinations with 2 and 3 dice. These are for two: </p> <blockquote> <p>There are 1 ways to roll a 2<br> There are 2 ways to roll a 3<br> There are 3 ways to roll a 4<br> There are 4 ways to roll a 5<br> There are 5 ways to roll a 6<br> There are 6 ways to roll a 7<br> There are 5 ways to roll a 8<br> There are 4 ways to roll a 9<br> There are 3 ways to roll a 10<br> There are 2 ways to roll a 11<br> There are 1 ways to roll a 12<br></p> </blockquote> <p>Which looks straightforward enough; it can be calculated with a simple linear absolute value function. But then things start getting trickier. With 3:</p> <blockquote> <p>There are 1 ways to roll a 3<br> There are 3 ways to roll a 4<br> There are 6 ways to roll a 5<br> There are 10 ways to roll a 6<br> There are 15 ways to roll a 7<br> There are 21 ways to roll a 8<br> There are 25 ways to roll a 9<br> There are 27 ways to roll a 10<br> There are 27 ways to roll a 11<br> There are 25 ways to roll a 12<br> There are 21 ways to roll a 13<br> There are 15 ways to roll a 14<br> There are 10 ways to roll a 15<br> There are 6 ways to roll a 16<br> There are 3 ways to roll a 17<br> There are 1 ways to roll a 18<br></p> </blockquote> <p>So I look at that, and I think: Cool, Triangular numbers! However, then I notice those pesky 25s and 27s. So it's obviously not triangular numbers, but still some polynomial expansion, since it's symmetric. <br> So I take to Google, and I come across <a href="https://math.stackexchange.com/questions/68045/rolling-dice-such-that-they-add-up-to-13-is-there-a-more-elegant-way-to-solve">this page</a> that goes into some detail about how to do this with math. It is fairly easy(albeit long) to find this using repeated derivatives or expansion, but it would be much harder to program that for me. I didn't quite understand the second and third answers, since I have never encountered that notation or those concepts in my math studies before. Could someone please explain how I could write a program to do this, or explain the solutions given on that page, for my own understanding of combinatorics?</p> <p>EDIT: I'm looking for a mathematical way to solve this, that gives an exact theoretical number, not by simulating dice</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload