From: Eric DUMINIL Date: 2007-10-02T15:59:37+09:00 Subject: Re: [QUIZ] Probable Iterations (#141) Hi, my solution came a bit too late and had nothing to offer in comparison to the previous answers, so here is my slightly modified cheating solution (I do not check anything, I actually use counting calculations....) ################################################################## n=8 #Dice number d=3 #At least d fives f=50000 #Display step (0: never, 1:always, f:every f lines) class Fixnum def fact return 1 if self<2 self*(self-1).fact end def cnp(p) self.fact/(p.fact*(self-p).fact) end end @desirable=(d..n).inject(0){|mem,p| mem+n.cnp(p)*5**(n-p)} N=6**n (0...N).step(f){|outcome_id| outcome=outcome_id.to_s(6).rjust(n,'0').split('').collect{|dice| dice.to_i+1} print "\n#{(outcome_id+1).to_s.rjust(n)} : #{outcome.inspect}" print " <==" unless outcome.select{|dice| dice==5}.size wrote: > Hi, > > Here is my quick solution : > > dice.rb : > > dice, n = ARGV.grep(/\d+/) > verbose = true if ARGV.include?("-v") > sample = true if ARGV.include?("-s") > > max=("5"*dice.to_i).to_i(6) > n=n.to_i > resultat= (0..max).inject(0) do |sum,i| > output = verbose || (sample && i % 50_000 == 0) > print i+1,"\t",("%#{dice}s" % i.to_s(6)).split(//).map{|e| e.to_i > +1}.inspect if output > if i.to_s(6).scan(/4/).size > n-1 > puts "<=" if output > sum+1 > else > puts "" if output > sum > end > end > > puts "Number of desirable outcomes is #{resultat}" > puts "Number of possible outcomes is #{max+1}" > puts Float(resultat) / Float(max+1) > > > > > > >