This comment is reply for 7808 匿名: 面積から攻めるのはよく知られていますね。...(π). Go to thread root.
seri #7918(2008/11/01 21:31 GMT) [ Ruby ] Rating0/0=0.00
「ビュフォンの針」は初めて知りました。 というわけで実装。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
class Needle LENGTH = 5 RAND_MAX = 10000 attr_reader :x1, :x2 def initialize() @x1 = rand(RAND_MAX) + rand @x2 = LENGTH * Math.cos(rand * 2 * Math::PI) + @x1 end end n = 10000 crossed = 0 n.times { needle = Needle.new() k1 = (needle.x1 / (2 * Needle::LENGTH)).to_i k2 = (needle.x2 / (2 * Needle::LENGTH)).to_i crossed += 1 if k1 != k2 } pi = n.to_f / crossed.to_f puts "pi = #{pi}"
Rating0/0=0.00-0+
[ reply ]
seri
#7918()
[
Ruby
]
Rating0/0=0.00
Rating0/0=0.00-0+