Skip to main content
Fixed an obvious typo
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

I feel like there is a recursive answer here that I can't put my finger on that would work for different numbers of dice.

Sure. Let's work with an example, let's say the one in the question: how many ways can we roll 5 dice to get 21. The answer could be computed as the sum of answers of the following questions:

  • How many ways are there to roll 4 dice to reach 20? (first roll was 1)
  • How many ways are there to roll 4 dice to reach 19? (first roll was 12)
  • ...
  • How many ways are there to roll 4 dice to reach 15? (first roll was 6)

And each of these questions can be further broken down:

  • How many ways are there to roll 3 dice to reach 19? (first roll was 1, 2nd was 1)
  • How many ways are there to roll 3 dice to reach 20? (first roll was 1, 2nd was 2)
  • ...
  • How many ways are there to roll 3 dice to reach 9? (first roll was 6, 2nd was 6)

I hope you see how the pattern will reach a point where we are actually able to give an answer, for example:

  • How many ways are there to roll 0 dice to reach 0 -> 1 :-)
  • How many ways are there to roll 0 dice to reach 1 -> 0, not possible to reach non-zero without dice
  • How many ways are there to roll 2 dice to reach 0 -> 0, not possible, the remaining rolls will be greater than 0
  • ...

In other words, the recursive function must first check the terminating condition, as usual. If we know the answer given the input (0 or 1), we return it. Otherwise, we compute the sum of calling the function for each possible path (dice value), with one less dice, and reduced target to reach.

I feel like there is a recursive answer here that I can't put my finger on that would work for different numbers of dice.

Sure. Let's work with an example, let's say the one in the question: how many ways can we roll 5 dice to get 21. The answer could be computed as the sum of answers of the following questions:

  • How many ways are there to roll 4 dice to reach 20? (first roll was 1)
  • How many ways are there to roll 4 dice to reach 19? (first roll was 1)
  • ...
  • How many ways are there to roll 4 dice to reach 15? (first roll was 6)

And each of these questions can be further broken down:

  • How many ways are there to roll 3 dice to reach 19? (first roll was 1, 2nd was 1)
  • How many ways are there to roll 3 dice to reach 20? (first roll was 1, 2nd was 2)
  • ...
  • How many ways are there to roll 3 dice to reach 9? (first roll was 6, 2nd was 6)

I hope you see how the pattern will reach a point where we are actually able to give an answer, for example:

  • How many ways are there to roll 0 dice to reach 0 -> 1 :-)
  • How many ways are there to roll 0 dice to reach 1 -> 0, not possible to reach non-zero without dice
  • How many ways are there to roll 2 dice to reach 0 -> 0, not possible, the remaining rolls will be greater than 0
  • ...

In other words, the recursive function must first check the terminating condition, as usual. If we know the answer given the input (0 or 1), we return it. Otherwise, we compute the sum of calling the function for each possible path (dice value), with one less dice, and reduced target to reach.

I feel like there is a recursive answer here that I can't put my finger on that would work for different numbers of dice.

Sure. Let's work with an example, let's say the one in the question: how many ways can we roll 5 dice to get 21. The answer could be computed as the sum of answers of the following questions:

  • How many ways are there to roll 4 dice to reach 20? (first roll was 1)
  • How many ways are there to roll 4 dice to reach 19? (first roll was 2)
  • ...
  • How many ways are there to roll 4 dice to reach 15? (first roll was 6)

And each of these questions can be further broken down:

  • How many ways are there to roll 3 dice to reach 19? (first roll was 1, 2nd was 1)
  • How many ways are there to roll 3 dice to reach 20? (first roll was 1, 2nd was 2)
  • ...
  • How many ways are there to roll 3 dice to reach 9? (first roll was 6, 2nd was 6)

I hope you see how the pattern will reach a point where we are actually able to give an answer, for example:

  • How many ways are there to roll 0 dice to reach 0 -> 1 :-)
  • How many ways are there to roll 0 dice to reach 1 -> 0, not possible to reach non-zero without dice
  • How many ways are there to roll 2 dice to reach 0 -> 0, not possible, the remaining rolls will be greater than 0
  • ...

In other words, the recursive function must first check the terminating condition, as usual. If we know the answer given the input (0 or 1), we return it. Otherwise, we compute the sum of calling the function for each possible path (dice value), with one less dice, and reduced target to reach.

Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

I feel like there is a recursive answer here that I can't put my finger on that would work for different numbers of dice.

Sure. Let's work with an example, let's say the one in the question: how many ways can we roll 5 dice to get 21. The answer could be computed as the sum of answers of the following questions:

  • How many ways are there to roll 4 dice to reach 20? (first roll was 1)
  • How many ways are there to roll 4 dice to reach 19? (first roll was 1)
  • ...
  • How many ways are there to roll 4 dice to reach 15? (first roll was 6)

And each of these questions can be further broken down:

  • How many ways are there to roll 3 dice to reach 19? (first roll was 1, 2nd was 1)
  • How many ways are there to roll 3 dice to reach 20? (first roll was 1, 2nd was 2)
  • ...
  • How many ways are there to roll 3 dice to reach 9? (first roll was 6, 2nd was 6)

I hope you see how the pattern will reach a point where we are actually able to give an answer, for example:

  • How many ways are there to roll 0 dice to reach 0 -> 1 :-)
  • How many ways are there to roll 0 dice to reach 1 -> 0, not possible to reach non-zero without dice
  • How many ways are there to roll 2 dice to reach 0 -> 0, not possible, the remaining rolls will be greater than 0
  • ...

In other words, the recursive function must first check the terminating condition, as usual. If we know the answer given the input (0 or 1), we return it. Otherwise, we compute the sum of calling the function for each possible path (dice value), with one less dice, and reduced target to reach.