Project Euler 205 - Dice game

Official link: https://projecteuler.net/problem=205

Thought Process

First I coded a Monte Carlo simulation to approximate my result, got to the sixth decimal place after ~10 mins of waiting.

After that I had to start thinking smart, my code has 3 functions

  1. colindice()

    • Initialise and array = [0]*37 (This is for 1-36 indexing)

    • Do a 6 times nested loop through the list [1,2,3,4,5,6] each time, at the end you will have the sum of 6 numbers, say sum, between 6-36

    • array[sum] += 1/(6^6), essentially what this is doing is creating an array which stores the probability of colin rolling a number, after everything is done for example array[18] = 0.07353823

  2. peterdice()

    • I believe you can figure out what I did here from above

  3. compute()

    • Goes through a double nested loop with variables x, y, where x goes from 6-37 (Cubic Colin), y goes from 9-37 (Pyramidal Pete), these represent the sum of the dice rolls for each player

    • First we create a temp variable say, temptotal. Now we want to find the probability that Pyramidal Pete beats Cubic Colin, therefore if y > x, that is Pyramidal Pete rolls higher than Cubic Colin, then we add the probability of Pyramidal Pete getting that roll, y, which we can find from function peterdice(). After we've done that for every number we multiply temptotal by the probability of Cubic Colin getting the roll x

Interactive Code

Note: I wasn't really sure what interactive feature to put here so it may not be very useful

Enter an integer (yourinput)

Code will output the probability that Pyramidal Pete wins when Cubic Colin rolls yourinput