# Integer partitions and dice result probabilities

This is a Project Euler question, so no spoilers :] I was going to try to solve it by first finding how many ways a certain sum can be acquired from a number of dice using IntegerPartitions . For example, using two 6-sided dice, there is only one way to get a sum of 2.

IntegerPartitions[2, {2}, {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6}]

I expected the answer to be {1,1}, but the output was {{1,1},{1,1},{1,1}}. I'm really confused now. Am I not using the function properly? If so, is there some other way that I could achieve the required result?

Bam XD

s = 0; For[i = 9, i <= 36, i++, n1 = Length@ Flatten[(Permutations /@ IntegerPartitions[i, {9}, Range[4]]), 1]; For[j = 6, j < i, j++, n2 = Length@ Flatten[(Permutations /@ IntegerPartitions[j, {6}, Range[6]]), 1]; p = n1/4^9*n2/6^6; s = s + p]]; N[s]

• IntegerPartitions[2, {2}, Range[6]]?
– kglr
Mar 28, 2015 at 16:05
• This doesn't consider the number of dice. If there are more than two, then there is no way of getting a sum of 2. I suspect that will also affect the results of getting higher sums, but maybe I'm wrong, lemme think about it >.> I think you're right <.< ... posted a stoopid question too soon, sorry ... shouldn't try to math before breakfast %\ Mar 28, 2015 at 16:08
• solarmew, the second argument is the number of dice, no? You are right you can't get a sum of 2 with 3 dice and IntegerPartitions[2, {3}, Range[6]] does give {} as required.
– kglr
Mar 28, 2015 at 16:13
• I don't believe IntegerPartitions is the right tool for this. Have you tried a brute force approach yet? Mar 28, 2015 at 16:13
• Nicely done, and happy to be proven wrong. Thanks regarding #67; I still like that bit of code. I keep meaning to return to Project Euler (I stopped when I started participating on Stack Overflow) but I never do. By brute force I meant Tally, which I what I used five years ago when I solved it. (I can't believe it's been so long!) You now have access to the forum solutions and many wonderful methods. I am partial to the coefficient based methods which as I recall I learned as a result of this problem or one like it. Mar 28, 2015 at 19:25

I like this puzzle:

peter = Total /@ Tuples[Range[4], 9];
colin = Total /@ Tuples[Range[6], 6];
{sp, wp} = Transpose[Tally[peter]];
{sc, wc} = Transpose[Tally[colin]];
edp = EmpiricalDistribution[wp -> sp];
edc = EmpiricalDistribution[wc -> sc];


r = Tuples[{sp, sc}];
p = Pick[r, First@# > Last@# & /@ r];
probp = MapThread[#1 -> N[#2/Total[wp]] &, {sp, wp}];
probc = MapThread[#1 -> N[#2/Total[wc]] &, {sc, wc}];
NumberForm[Total[(#1 /. probp) (#2 /. probc) & @@@ p], 7]


Using probability functionality:

N[Probability[s > u, {s \[Distributed] edp, u \[Distributed] edc}]]


Or simulation:

pt = DiscreteUniformDistribution[{1, 4}];
cl = DiscreteUniformDistribution[{1, 6}];
fun[n_] :=
N[Total[Table[
Boole[Total@RandomVariate[pt, 9] >
Total@RandomVariate[cl, 6]], {n}]]/n]


and evaluate,e.g. fun[1000000]

Some visualization:

DiscretePlot[{PDF[edp, x], PDF[edc, x]}, {x, 2, 40},
PlotStyle -> {Red, Black},
PlotLegends -> {"Pyramidal Peter", " Cubic Colin"}]


I have not put my answer deliberately.