$loss=0; $turn=1; $bet=1000; $target=10000; $multiplier=10; $p=1/18; $q=17/18; $limit=shift; while ($turn<=$limit) { $l=int($loss+0.5); $b=int($bet+0.5); print "At turn $turn you have lost $l lire so far and must bet $b\n"; $turn++; $loss+=$bet; $bet=($loss+$target)/$multiplier; } $l=int($loss+0.5); $loss_prob=$q**$limit; print "If you lose all the above, you've lost $l\n"; print "The probability of this happening is $loss_prob\n"; print "The rest of the time you win $target\n"; $average_loss=int(-$target*(1-$loss_prob)+$loss*$loss_prob+0.5); print "So on average you lose $average_loss lire.\n";