Project Euler – Problem 205 Solution

Yan Cui

I help clients go faster for less using serverless technologies.

Problem

Peter has nine four-sided (pyramidal) dice, each with faces numbered 1, 2, 3, 4.
Colin has six six-sided (cubic) dice, each with faces numbered 1, 2, 3, 4, 5, 6.

Peter and Colin roll their dice and compare totals: the highest total wins. The result is a draw if the totals are equal.

What is the probability that Pyramidal Pete beats Cubic Colin? Give your answer rounded to seven decimal places in the form 0.abcdefg

Solution

// recursive function that'll return all the possible scores
let rec getScores numbers acc iter max =
if iter < max
then numbers |> List.collect (fun n -> getScores numbers (acc + n) (iter + 1) max)
else numbers |> List.map (fun n -> n + acc)
// gets the possible scores and the associated probability of achieving that score
let getScoreProbs numbers count =
// given the set of possible numbers on each dice and the number of dice
// what's the prob of getting each permutation (1, 1, 2, 3)
let permutProb = 1.0 / float(numbers |> List.length) ** float(count)
// get all the permutation of the dices and the the corresponding score
// and then group the occurance of each possible score and work out
// the possibility of achieving each score
(getScores numbers 0 1 count)
|> Seq.groupBy (fun n -> n)
|> Seq.map (fun gr -> (fst gr, permutProb * float((snd gr) |> Seq.length)))
|> Seq.toList
// work out the probability of peter and colin getting each of the possible scores
let peterScoreProbs = getScoreProbs [1..4] 9
let colinScoreProbs = getScoreProbs [1..6] 6
let answer =
peterScoreProbs
|> List.map (fun gr ->
colinScoreProbs
|> List.filter (fun gr' -> (fst gr') < (fst gr))
|> List.map (fun gr' -> (snd gr') * (snd gr))
|> List.sum)
|> List.sum

Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

Leave a Comment

Your email address will not be published. Required fields are marked *