Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/tests/sunspider/controlflow-recursive.lua
2727 views
1
--[[
2
The Great Computer Language Shootout
3
http://shootout.alioth.debian.org/
4
contributed by Isaac Gouy
5
]]
6
local function prequire(name) local success, result = pcall(require, name); return success and result end
7
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../../bench_support")
8
9
function test()
10
11
local function ack(m,n)
12
if (m==0) then return n+1; end
13
if (n==0) then return ack(m-1,1); end
14
return ack(m-1, ack(m,n-1) );
15
end
16
17
local function fib(n)
18
if (n < 2) then return 1; end
19
return fib(n-2) + fib(n-1);
20
end
21
22
local function tak(x,y,z)
23
if (y >= x) then return z; end
24
return tak(tak(x-1,y,z), tak(y-1,z,x), tak(z-1,x,y));
25
end
26
27
local result = 0;
28
29
for i = 3,5 do
30
result = result + ack(3,i);
31
result = result + fib(17+i);
32
result = result + tak(3*i+3,2*i+2,i+1);
33
end
34
35
local expected = 57775;
36
37
if (result ~= expected) then
38
assert(false, "ERROR: bad result: expected " .. expected .. " but got " .. result);
39
end
40
41
end
42
43
bench.runCode(test, "controlflow-recursive")
44
45