(phixonline)-->
with javascript_semantics
constant nMax = iff(platform()=JS?8:12)
-- Aside: on desktop/Phix, strings can be modified in situ, whereas
-- JavaScript strings are immutable, and the equivalent code
-- in p2js.js ends up doing excessive splitting and splicing
-- hence nMax has to be significantly smaller in a browser.
atom t0 = time()
string superperm
sequence count
integer pos
function factSum(int n)
integer s = 0, f = 1
for i=1 to n do
f *= i
s += f
end for
return s
end function
function r(int n)
if (n == 0) then return false end if
integer c = superperm[pos-n+1]
count[n] -= 1
if count[n]=0 then
count[n] = n
if not r(n-1) then return false end if
end if
pos += 1
superperm[pos] = c
return true
end function
procedure superPerm(int n)
string chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[1..n]
pos = n
superperm = chars&repeat(' ',factSum(n)-n)
count = tagset(n)
while r(n) do end while
if n=0 then
if superperm!="" then ?9/0 end if
elsif n<=7 then
-- (I estimate it would take at least 5 days to validate
-- superPerm(12), feel free to try it on your own time)
for i=1 to factorial(n) do
if not match(permute(i,chars),superperm) then ?9/0 end if
end for
end if
end procedure
for n=0 to nMax do
superPerm(n)
integer l = length(superperm)
if l>40 then superperm[20..-20] = "..." end if
string e = elapsed(time()-t0)
printf(1,"superPerm(%2d) len = %d %s (%s)\n", {n, l, superperm, e})
end for