#! /usr/local/bin/ruby def time start = Time.now result = yield diff = Time.now - start puts "Elapsed time: #{diff} secs" p result end def make_cache(arity) seed = "{}" (arity - 1).times do seed = "Hash.new { |hash, key| hash[key] = #{seed} }" end eval(seed) end def make_tarai cache = make_cache(3) tarai = lambda do |x, y, z| return y if x <= y cache[x][y][z] ||= tarai.call( tarai.call(x - 1, y, z), tarai.call(y - 1, z, x), tarai.call(z - 1, x, y) ) end end tarai = make_tarai time { tarai.call(100, 50, 0) }