Eric DUMINIL
10/2/2007 7:00:00 AM
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<d
} unless f==0
puts "\nNumber of desirable outcomes is #{@desirable}"
puts "Number of possible outcomes is #{N}"
puts "Probability is #{@desirable.to_f/N}"
##################################################################
time ruby dice.rb
1 : [1, 1, 1, 1, 1, 1, 1, 1]
50001 : [1, 2, 1, 3, 4, 3, 6, 3]
100001 : [1, 3, 1, 6, 1, 6, 5, 5]
150001 : [1, 4, 2, 2, 5, 3, 5, 1]
200001 : [1, 5, 2, 5, 2, 6, 4, 3]
250001 : [1, 6, 3, 1, 6, 3, 3, 5]
300001 : [2, 1, 3, 4, 3, 6, 3, 1]
350001 : [2, 2, 4, 1, 1, 3, 2, 3]
400001 : [2, 3, 4, 3, 4, 6, 1, 5]
450001 : [2, 4, 4, 6, 2, 3, 1, 1]
500001 : [2, 5, 5, 2, 5, 5, 6, 3] <==
550001 : [2, 6, 5, 5, 3, 2, 5, 5] <==
600001 : [3, 1, 6, 1, 6, 5, 5, 1]
650001 : [3, 2, 6, 4, 4, 2, 4, 3]
700001 : [3, 4, 1, 1, 1, 5, 3, 5]
750001 : [3, 5, 1, 3, 5, 2, 3, 1]
800001 : [3, 6, 1, 6, 2, 5, 2, 3]
850001 : [4, 1, 2, 2, 6, 2, 1, 5]
900001 : [4, 2, 2, 5, 3, 5, 1, 1]
950001 : [4, 3, 3, 2, 1, 1, 6, 3]
1000001 : [4, 4, 3, 4, 4, 4, 5, 5]
1050001 : [4, 5, 4, 1, 2, 1, 5, 1]
1100001 : [4, 6, 4, 3, 5, 4, 4, 3]
1150001 : [5, 1, 4, 6, 3, 1, 3, 5]
1200001 : [5, 2, 5, 2, 6, 4, 3, 1]
1250001 : [5, 3, 5, 5, 4, 1, 2, 3] <==
1300001 : [5, 4, 6, 2, 1, 4, 1, 5]
1350001 : [5, 5, 6, 4, 5, 1, 1, 1] <==
1400001 : [6, 1, 1, 1, 2, 3, 6, 3]
1450001 : [6, 2, 1, 3, 5, 6, 5, 5] <==
1500001 : [6, 3, 1, 6, 3, 3, 5, 1]
1550001 : [6, 4, 2, 2, 6, 6, 4, 3]
1600001 : [6, 5, 2, 5, 4, 3, 3, 5] <==
1650001 : [6, 6, 3, 2, 1, 6, 3, 1]
Number of desirable outcomes is 226491
Number of possible outcomes is 1679616
Probability is 0.134846893575674
real 0m0.010s
user 0m0.000s
sys 0m0.008s
Thanks for the quiz!
On 01/10/2007, come <come.news@free.fr> 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)
>
>
>
>
>
>
>