#! /usr/local/bin/ruby class Tarai def initialize @cache = Hash.new do |hash, key| hash[key] = Hash.new do |hash, key| hash[key] = {} end end end def call(x, y, z) return y if x <= y @cache[x][y][z] ||= call( call(x - 1, y, z), call(y - 1, z, x), call(z - 1, x, y) ) end end tarai = Tarai.new p tarai.call(100, 50, 0)