Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need to brute force since your first roll determines what values can be used in the second roll, and both first and second roll determine the third roll. Let's take the tens example, suppose you roll a <code>6</code>, so <code>10-6=4</code> meaning you still need <code>4</code>. For the second roll you need at least <code>3</code>, because your third roll should at least count for <code>1</code>. So the second roll goes from <code>1</code> to <code>3</code>. Suppose your second roll is <code>2</code>, you have <code>10-6-2=2</code>, meaning your third roll IS a <code>2</code>, there is no other way.</p> <p>Pseudo code for tens:</p> <pre><code>tens = 0 for i = [1..6] // first roll can freely go from 1 to 6 from_j = max(1, 10 - i - 6) // We have the first roll, best case is we roll a 6 in the third roll top_j = min(6, 10 - i - 1) // we need to sum 10, minus i, minus at least 1 for the third roll for j = [from_j..to_j] tens++ </code></pre> <p>Note that each loop adds 1, so at the end you know this code loops exactly 27 times.</p> <p>Here is the Ruby code for all 18 values (sorry it's not Java, but it can be easily followed). Note the min and max, that determine what values can have each of the dice rolls.</p> <pre><code>counts = [0] * 18 1.upto(18) do |n| from_i = [1, n - 6 - 6].max # best case is we roll 6 in 2nd and 3rd roll top_i = [6, n - 1 -1].min # at least 1 for 2nd and 3rd roll from_i.upto(top_i) do |i| from_j = [1, n - i - 6].max # again we have the 2nd roll defined, best case for 3rd roll is 6 top_j = [6, n - i -1].min # at least 1 for 3rd roll from_j.upto(top_j) do # no need to calculate k since it's already defined being n - first roll - second roll counts[n-1] += 1 end end end print counts </code></pre> <p>For a mathematical approach, take a look at <a href="https://math.stackexchange.com/questions/4632/how-can-i-algorithmically-count-the-number-of-ways-n-m-sided-dice-can-add-up-t">https://math.stackexchange.com/questions/4632/how-can-i-algorithmically-count-the-number-of-ways-n-m-sided-dice-can-add-up-t</a></p>
 

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