http://qs321.pair.com?node_id=895892

Microcebus has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I wrote a script to calculate the propability to exceed a defined pips sum throwing a loaded die with x sides n times. The number of pips for each side can also be defined.
$throw_die_ntimes=5; $min_summed_pips=60; $sides_of_die=5; # propability to get a certain side @propabilities=(0.05,0.1,0.2,0.3,0.35); # number of pips for each side @values=(1,5,7,13,17); # generate possible compositions @start_array=("1","2","3","4","5"); foreach(1..$throw_die_ntimes-1) { %hash=(); foreach$composition(@start_array) { foreach(1..$sides_of_die) { $new_composition=$composition.$_; @sort_new_composition=split('',$new_composition); @sort_new_composition=sort@sort_new_composition; $new_composition=join('',@sort_new_composition); push(@new_array,$new_composition); $hash{$new_composition}++; } } @start_array=@new_array; @new_array=(); } # calculate propability of each composition $total_prop_to_exceed_minimum=0; $total_prop_to_deceed_minimum=0; foreach$composition(keys%hash) { $propability_of_composition=1; $value_of_composition=0; @composition=split('',$composition); foreach$step(@composition) { $propability_of_composition=$propability_of_composition*$propa +bilities[$step-1]; $value_of_composition+=$values[$step-1]; } $propability_of_composition=$propability_of_composition*$hash{$com +position}; # add propability to one of two total propabilities (exceed or dec +eed minimum) if($value_of_composition>=$min_summed_pips) {$total_prop_to_exceed_minimum+=$propability_of_composition;} else {$total_prop_to_deceed_minimum+=$propability_of_composition;} } print"p to exceed minimum: $total_prop_to_exceed_minimum\np to deceed +minimum: $total_prop_to_deceed_minimum\n\n"; system("pause"); exit;
This code works but gets very slow when I throw the die more then 10 times. The problem seems to be the exploding array when calculating all possible compositions. I pored over a smarter way to do this but still I do not have a clue.
Any ideas?