Topswops: Difference between revisions

m
→‎{{header|Lua}}: Switched to using iterator with 'for .. in'
m (→‎{{header|Perl 6}}: use built-in for permutations, use 'sort' not '[max]' to avoid reduction limit)
m (→‎{{header|Lua}}: Switched to using iterator with 'for .. in')
Line 1,217:
return coroutine.wrap(function() perm(list, #list) end)
end
 
-- Perform one topswop round on table t
function swap (t)
Line 1,230:
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
localfor nextPermnumList =in permute(numTab) do
local numList = nextPerm()
while numList do
count = 0
while numList[1] ~= 1 do
Line 1,244 ⟶ 1,242:
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>
Anonymous user