I have been doing for fun this problem in projecteuler.net. The problem is as follows:
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
#!/usr/bin/python from datetime import datetime from decimal import * def getProb49(): probabilities=*(4*9+1) for d1 in range(1,5): for d2 in range(1,5): for d3 in range(1,5): for d4 in range(1,5): for d5 in range(1,5): for d6 in range(1,5): for d7 in range(1,5): for d8 in range(1,5): for d9 in range(1,5): probabilities[d1+d2+d3+d4+d5+d6+d7+d8+d9] += 1 return probabilities def getProb66(): probabilities=*(6*6+1) for d1 in range(1,7): for d2 in range(1,7): for d3 in range(1,7): for d4 in range(1,7): for d5 in range(1,7): for d6 in range(1,7): probabilities[d1+d2+d3+d4+d5+d6] += 1 return probabilities def main(): stime=datetime.now() prob66 = getProb66() prob49 = getProb49() """ sumprob = 0 index = 0 for prob in prob49: print index, float(prob)/4**9, prob sumprob += prob index += 1 print sumprob,4**9 """ # Probability for prob49 > prob66 tot49 = 4**9 tot66 = 6**6 getcontext().prec = 7 sol = 0 index = 0 for prob in prob49: sol += prob * sum(prob66[0:index-1]) print index, Decimal(prob)/Decimal(tot49), Decimal(sum(prob66[0:index-1]))/Decimal(tot66),float(sol)/(tot49*tot66),sol index += 1 print Decimal(sol)/Decimal(tot66*tot49) print datetime.now()-stime if __name__ == "__main__": main()
What I basically do is calculating all the possibilities for obtaining 0,1,..,36 with the dices and write it in a list.
Then I iterate over the two lists and I try to sum all the cases where the 4 side dice win
My problem is that even it runs pretty fast I don't get the right solution.
I know that when index=0 the prob66[0:-1] does not works as I expected but since it is multiplied by 0 in that point is not the problem, (though is not a really good coded)
I can't figure out what is wrong