# Pi - Calculate pi using the Leibniz series with a specified number of iterations. # See: http://en.wikipedia.org/wiki/Leibniz_formula_for_pi. # and: http://derekwilliams.us/?p=4886. class Pi attr_accessor :iterations def initialize(iterations) @iterations = iterations end def calculate numerator = 4.0 denominator = 1.0 negval = 1.0 answer = 0.0 iterations.times do answer += (negval) * (numerator/denominator) negval *= -1 denominator += 2 end return answer end end start = Time.now threads = [] 10.times do threads << Thread.new do puts Pi.new(1000000000).calculate end end threads.each { |thr| thr.join } printf("Time: %d seconds\n", Time.now - start);