Topswops: Difference between revisions

Added Lua version
(Added PicoLisp)
(Added Lua version)
Line 1,203:
38
elapsed time: 0.299617582 seconds</pre>
 
=={{header|Lua}}==
<lang Lua>-- Return an iterator to produce every permuation of list
function permute (list)
local function perm (list, n)
if n == 0 then coroutine.yield(list) end
for i = 1, n do
list[i], list[n] = list[n], list[i]
perm(list, n - 1)
list[i], list[n] = list[n], list[i]
end
end
return coroutine.wrap(function() perm(list, #list) end)
end
 
-- Perform one topswop round on table t
function swap (t)
local new, limit = {}, t[1]
for i = 1, #t do
if i <= limit then
new[i] = t[limit - i + 1]
else
new[i] = t[i]
end
end
return new
end
 
-- Find the most swaps needed for any starting permutation of n cards
function topswops (n)
local numTab, highest, count = {}, 0
for i = 1, n do numTab[i] = i end
local nextPerm = permute(numTab)
local numList = nextPerm()
local orig = table.concat(numList)
while numList do
count = 0
while numList[1] ~= 1 do
numList = swap(numList)
count = count + 1
end
if count > highest then highest = count end
numList = nextPerm()
end
return highest
end
 
-- Main procedure
for i = 1, 10 do print(i, topswops(i)) end</lang>
{{out}}
<pre>1 0
2 1
3 2
4 4
5 7
6 10
7 16
8 22
9 30
10 38</pre>
 
=={{header|Mathematica}}==
Anonymous user