Editor

Dice Sum Probability

#406 · Math · Advanced · 150 XP · about 30 min

You roll dice fair dice, each with faces numbered 1 to sides. What is the probability that the faces add up to exactly total?

Write dice_probability(dice, sides, total) that returns the exact probability as a string 'p/q' in lowest terms. An impossible total gives '0/1', and a certain one gives '1/1'.

Examples

dice_probability(2, 6, 7)     # returns '1/6'
dice_probability(3, 6, 10)    # returns '1/8'
dice_probability(1, 6, 7)     # returns '0/1'

Two ordinary dice make 7 in 6 of their 36 equally likely outcomes, so the answer is 6/36 = 1/6.

Constraints

  • 1 <= dice <= 100
  • 1 <= sides <= 100
  • 0 <= total <= 10,000
  • With 100 dice there are far too many outcomes to list, and the top and bottom of the fraction can have hundreds of digits.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.