Project Euler

Problem #205

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

Erlang: Running time = 2.722s
+%pow

+%cartesian_product

%
% This is more of a brute force method, but it seemed a lot easier than thinking
% of a more principled approach, and it runs fast enough
%

p205()->
	DictC=dict:from_list(lists:map(fun(X)->{X,0} end,lists:seq(6,36))),
	Colin=p205(cartesian_product(lists:duplicate(6,lists:seq(1,6))),DictC),
	DictP=dict:from_list(lists:map(fun(X)->{X,0} end,lists:seq(4,36))),
	Peter=p205(cartesian_product(lists:duplicate(9,lists:seq(1,4))),DictP),
	Trials=cartesian_product([dict:to_list(Colin),dict:to_list(Peter)]),
	put(6,pow(6,6)),
	put(4,pow(4,9)),
	p205(process,Trials,0).

p205(process,[],Prob)->
	Ans=round(10000000*Prob)/10000000,
	io:format("~w~n",[Ans]);
p205(process,[[{CK,CV},{PK,PV}]|Tail],Prob) when PK > CK->
	P=(CV/get(6))*(PV/get(4)),
	p205(process,Tail,Prob+P);
p205(process,[_|Tail],Prob)->p205(process,Tail,Prob).

p205([],Dict)->Dict;
p205([Next|List],Dict)->
	S=lists:sum(Next),
	p205(List,dict:store(S,1+dict:fetch(S,Dict),Dict)).