2
$\begingroup$

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]

$\endgroup$
  • 1
    $\begingroup$ IntegerPartitions[2, {2}, Range[6]]? $\endgroup$ – kglr Mar 28 '15 at 16:05
  • $\begingroup$ 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 %\ $\endgroup$ – Raksha Mar 28 '15 at 16:08
  • 1
    $\begingroup$ 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. $\endgroup$ – kglr Mar 28 '15 at 16:13
  • $\begingroup$ I don't believe IntegerPartitions is the right tool for this. Have you tried a brute force approach yet? $\endgroup$ – Mr.Wizard Mar 28 '15 at 16:13
  • 1
    $\begingroup$ 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. $\endgroup$ – Mr.Wizard Mar 28 '15 at 19:25
2
$\begingroup$

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];

Counting the answer:

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"}]

enter image description here

I have not put my answer deliberately.

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.