forked from ArkScript-lang/Ark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathackermann.lua
More file actions
86 lines (74 loc) · 1.7 KB
/
ackermann.lua
File metadata and controls
86 lines (74 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
table.reduce = function (list, fn)
local acc
for k, v in ipairs(list) do
if 1 == k then
acc = v
else
acc = fn(acc, v)
end
end
return acc
end
local function ackermann(m, n)
if m == 0 then return n + 1 end
if n == 0 then return ackermann(m-1,1) end
return ackermann(m-1, ackermann(m, n-1))
end
results = {}
for i=1,25 do
temp = {}
for j=1,5 do
start = os.clock()
ackermann(3, 6)
table.insert(temp, (os.clock() - start) * 1000)
end
r = table.reduce(
temp,
function (a, b)
return a + b
end
)
r = r / 5
table.insert(results, r)
end
local function quantile(t, q)
assert(t ~= nil, "No table provided to quantile")
assert(q >= 0 and q <= 1, "Quantile must be between 0 and 1")
table.sort(t)
local position = #t * q + 0.5
local mod = position % 1
if position < 1 then
return t[1]
elseif position > #t then
return t[#t]
elseif mod == 0 then
return t[position]
else
return mod * t[math.ceil(position)] +
(1 - mod) * t[math.floor(position)]
end
end
local function median(t)
assert(t ~= nil, "No table provided to median")
return quantile(t, 0.5)
end
function standardDeviation(t, m)
local vm
local sum = 0
local count = 0
local result
for k,v in pairs(t) do
if type(v) == 'number' then
vm = v - m
sum = sum + (vm * vm)
count = count + 1
end
end
result = math.sqrt(sum / (count-1))
return result
end
sum = table.reduce(results, function (a, b) return a + b end)
mean = sum / 25
print("Mean time:", mean, "ms")
print("Median time:", median(results), "ms")
print("Stddev:", standardDeviation(results, mean), "ms")