Hi all,

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=[0]*(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=[0]*(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

Thank you!

Recommended Answers

All 4 Replies

I do not want to give out solution code to you but the counts of different outcomes from my correct solution (I am tonyjv in projecteuler also, 131 solved problems) are for debugging:

Draws 865512042, pwins 7009890480, cwins 4355187942

pwins means Pete wins.

And no extra modules were needed for me, even some other correct solutions in forum used decimal module.
Time taken in my old machine (Sempron3100+, 1800 MHz, WindowsXP) was 1.195 ms with Python 2.7 (29 lines of code excluding comments and empty lines).

I do not want to give out solution code to you but the counts of different outcomes from my correct solution (I am tonyjv in projecteuler also, 131 solved problems) are for debugging:

Draws 865512042, pwins 7009890480, cwins 4355187942

pwins means Pete wins.

Thank you!

Really helpfull, and interesting things arise

Draws 865512042, pwins 6115295232, cwins 4355187942

It looks like the only wrong one is the answer to the problem.

Ok it is done

the problem was in line 56
# sol += prob * sum(prob66[0:index-1])

I was using wrong that sublist property, it should have been until index instead until index-1

Thank you!

Concratulations, your code produce the right solution corrected in time around 300 ms in my computer. Little slower than mine, but fast enough. Also I have general function which can be used for any number of throws of any kind of dice instead of hard coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.