Problem 205

[quote]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[/quote]
Rozwiazanie polega na zliczaniu. Na przykład dla 2 rzutów K4 mamy, że dwojka moze wypasc tylko w jednej kombinacji (1,1), trojka tylko dla dwoch kombinacji (1,2),(2,1), etc. Przetrzymywane w zmiennej hash { 2 => 1, 3 => 2, …} a zliczane przez funkcje compute. A potem test prawdopodobienstwa vs inna kostka;)

[code=ruby]class Dice
attr_accessor :hash, :count, :size, :all_count
def initialize(count, size)
@all_count, @hash, @count, @size = 0, {}, count, size
(count…(count*size)).each {|i| @hash[i]=0 }
compute(count,0)
end

def compute(count,last)
for i in 1…@size
if count == 1
@hash[i+last] +=1
@all_count+=1
else
compute(count-1,i+last)
end
end
end

ilosc kombinacji ktore wygrywaja z X

def bigger_or_draw_than(x)
sum = 0
@hash.each_pair do |k,v|
sum += v if k > x
end
sum
end

prawdopodobienstwo ze kostka self przegra z other_dice

def against(other_dice)
all_combinations = 0
losers = 0
pro = 0
@hash.each_pair do |k,v|
other_winers = other_dice.bigger_or_draw_than(k)
pro += (v.to_f / all_count.to_f * other_winers /other_dice.all_count.to_f)
end
return pro
end
end

d4 = Dice.new(9,4)
d6 = Dice.new(6,6)

p ‘%.7f’ % d6.against(d4)[/code]