MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / DiceThrow

Function DiceThrow

dynamic/dicethrow.go:10–33  ·  view source on GitHub ↗

DiceThrow returns the number of ways to get sum `sum` using `m` dice with `n` faces

(m, n, sum int)

Source from the content-addressed store, hash-verified

8
9// DiceThrow returns the number of ways to get sum `sum` using `m` dice with `n` faces
10func DiceThrow(m, n, sum int) int {
11 dp := make([][]int, m+1)
12 for i := range dp {
13 dp[i] = make([]int, sum+1)
14 }
15
16 for i := 1; i <= n; i++ {
17 if i <= sum {
18 dp[1][i] = 1
19 }
20 }
21
22 for i := 2; i <= m; i++ {
23 for j := 1; j <= sum; j++ {
24 for k := 1; k <= n; k++ {
25 if j-k >= 0 {
26 dp[i][j] += dp[i-1][j-k]
27 }
28 }
29 }
30 }
31
32 return dp[m][sum]
33}

Callers 1

TestDiceThrowFunction · 0.92

Calls

no outgoing calls

Tested by 1

TestDiceThrowFunction · 0.74