Dice Sum That Is a Multiple of m
Problem statement
You roll n fair six-sided dice. Each die shows a number from 1 to 6 with equal probability, and the dice are independent. You win when the sum of the dice is a multiple of m.
Return the probability that you win. Answers within 10^-9 of the exact value are accepted.
Function
diceSumMultipleProbability(n: int, m: int) → doubleExamples
Example 1
n = 10m = 7return = 0.1428571570327186This is the interview case. Ten dice have 6^10 = 60,466,176 equally likely results, and 8,638,026 of them have a sum that is a multiple of 7. The answer is 8,638,026 / 60,466,176, about 0.1428571570. That is very close to 1/7, but not equal to it.
Example 2
n = 2m = 7return = 0.16666666666666666Two dice give sums from 2 to 12, so the only multiple of 7 they can reach is 7. Six of the 36 results add up to 7: 1+6, 2+5, 3+4, 4+3, 5+2 and 6+1. The answer is 6/36 = 1/6.
Example 3
n = 3m = 5return = 0.19907407407407407Three dice give sums from 3 to 18. The multiples of 5 in that range are 5, 10 and 15, reached by 6, 27 and 10 of the 216 results. The answer is 43/216, about 0.1990740741.
Constraints
1 <= n <= 2001 <= m <= 1000