Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/tests/shootout/fannkuch-redux.lua
2727 views
1
--[[
2
MIT License
3
4
Copyright (c) 2017 Gabriel de Quadros Ligneul
5
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
SOFTWARE.
23
]]
24
-- The Computer Language Benchmarks Game
25
-- http://benchmarksgame.alioth.debian.org/
26
-- contributed by Mike Pall
27
28
local function prequire(name) local success, result = pcall(require, name); return success and result end
29
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../../bench_support")
30
31
function test()
32
33
local function fannkuch(n)
34
local p, q, s, sign, maxflips, sum = {}, {}, {}, 1, 0, 0
35
for i=1,n do p[i] = i; q[i] = i; s[i] = i end
36
repeat
37
-- Copy and flip.
38
local q1 = p[1] -- Cache 1st element.
39
if q1 ~= 1 then
40
for i=2,n do q[i] = p[i] end -- Work on a copy.
41
local flips = 1
42
repeat
43
local qq = q[q1]
44
if qq == 1 then -- ... until 1st element is 1.
45
sum = sum + sign*flips
46
if flips > maxflips then maxflips = flips end -- New maximum?
47
break
48
end
49
q[q1] = q1
50
if q1 >= 4 then
51
local i, j = 2, q1 - 1
52
repeat q[i], q[j] = q[j], q[i]; i = i + 1; j = j - 1; until i >= j
53
end
54
q1 = qq; flips = flips + 1
55
until false
56
end
57
if sign == 1 then
58
p[2], p[1] = p[1], p[2]; sign = -1 -- Rotate 1<-2.
59
else
60
p[2], p[3] = p[3], p[2]; sign = 1 -- Rotate 1<-2 and 1<-2<-3.
61
for i=3,n do
62
local sx = s[i]
63
if sx ~= 1 then s[i] = sx-1; break end
64
if i == n then return sum, maxflips end -- Out of permutations.
65
s[i] = i
66
-- Rotate 1<-...<-i+1.
67
local t = p[1]; for j=1,i do p[j] = p[j+1] end; p[i+1] = t
68
end
69
end
70
until false
71
end
72
73
local n = tonumber(arg and arg[1]) or 8
74
local sum, flips = fannkuch(n)
75
print(sum, "\nPfannkuchen(", n, ") = ", flips, "\n")
76
77
end
78
79
bench.runCode(test, "fannkuchen-redux")
80
81