function varg_tostring(...)
local s = select(1, ...)
for n = 2, select('#', ...) do
s = s..","..select(n,...)
end
return s
end
function memoize(f)
local cache = {}
return function (...)
local al = varg_tostring(...)
if cache[al] then
return cache[al]
else
local y = f(...)
cache[al] = y
return y
end
end
end
function tak(x, y, z)
if x <= y then
return y
else
return tak(tak(x-1, y, z),
tak(y-1, z, x),
tak(z-1, x, y))
end
end
tak = memoize(tak)
x, y, z = 192, 96, 0
print(('tak(%d, %d, %d) = %d'):format(x, y, z, tak(x, y, z)))