require 'Benchmark' include Benchmark require 'rational.rb' BOUND = 100 RELATIVE = 0 def mygcd(one,other) min = one.abs max = other.abs while min > 0 tmp = min min = max % min max = tmp end max end def mygcd2(one,other) min = one.abs max = other.abs while min > 0 min, max = max % min, min end max end # require 'mathn.rb' puts "Computing gcd of #{BOUND}x#{BOUND} pairs starting at #{RELATIVE}" bm(6) do |x| x.report("gcd") do BOUND.times do |i| BOUND.times do |j| (RELATIVE+i).gcd(RELATIVE+j) end end end x.report("gcd2") do BOUND.times do |i| BOUND.times do |j| (RELATIVE+i).gcd2(RELATIVE+j) end end end x.report("mygcd") do BOUND.times do |i| BOUND.times do |j| mygcd(RELATIVE+i,RELATIVE+j) end end end x.report("mygcd2") do BOUND.times do |i| BOUND.times do |j| mygcd2(RELATIVE+i,RELATIVE+j) end end end end