Path: blob/master/bench/tests/sunspider/controlflow-recursive.lua
2727 views
--[[1The Great Computer Language Shootout2http://shootout.alioth.debian.org/3contributed by Isaac Gouy4]]5local function prequire(name) local success, result = pcall(require, name); return success and result end6local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../../bench_support")78function test()910local function ack(m,n)11if (m==0) then return n+1; end12if (n==0) then return ack(m-1,1); end13return ack(m-1, ack(m,n-1) );14end1516local function fib(n)17if (n < 2) then return 1; end18return fib(n-2) + fib(n-1);19end2021local function tak(x,y,z)22if (y >= x) then return z; end23return tak(tak(x-1,y,z), tak(y-1,z,x), tak(z-1,x,y));24end2526local result = 0;2728for i = 3,5 do29result = result + ack(3,i);30result = result + fib(17+i);31result = result + tak(3*i+3,2*i+2,i+1);32end3334local expected = 57775;3536if (result ~= expected) then37assert(false, "ERROR: bad result: expected " .. expected .. " but got " .. result);38end3940end4142bench.runCode(test, "controlflow-recursive")434445