ダイスで組分け・解答例
d:id:Nabetani:20070820#p1 を解くスクリプトを書いてみました。
#!/usr/bin/ruby require 'mathn' # for gcd class Groups < Hash def initialize super(0) self[[0, 0, 0, 0, 0, 0]] = 1 @members=0 end def iterate tmp = self.dup self.clear tmp.each{|groups, frequency| (0..5).each{|i| x = groups.dup x[i] += 1 x.sort! self[x] += frequency } } @members += 1 end def numerator self.inject(0){|sum, item| sum + (item[0].index(1) ? item[1] : 0) } end def denominator self.inject(0){|sum, item| sum + item[1] } end def show_probability n = self.numerator d = self.denominator g = n.gcd(d) puts sprintf("n=%2d: %8.6f (%d/%d)\n", @members, n.to_f/d.to_f, (n/g).to_s, (d/g).to_s) end end groups = Groups.new ARGV[0].to_i.times { groups.iterate groups.show_probability }
> ruby ./dicegroup.rb 20 n= 1: 1.000000 (1/1) n= 2: 0.833333 (5/6) n= 3: 0.972222 (35/36) n= 4: 0.925926 (25/27) n= 5: 0.960648 (415/432) n= 6: 0.945216 (1225/1296) n= 7: 0.948967 (44275/46656) n= 8: 0.940358 (32905/34992) n= 9: 0.931418 (173825/186624) n=10: 0.918691 (4629145/5038848) n=11: 0.901407 (18168205/20155392) n=12: 0.879292 (2953745/3359232) n=13: 0.851716 (1853999485/2176782336) n=14: 0.818071 (5342289715/6530347008) n=15: 0.778281 (20329774345/26121388032) n=16: 0.732966 (21539352185/29386561536) n=17: 0.683323 (642576334555/940369969152) n=18: 0.630864 (197748653015/313456656384) n=19: 0.577167 (58617086216815/101559956668416) n=20: 0.523683 (79777892481245/152339935002624) >