76 lines
1.7 KiB
Text
76 lines
1.7 KiB
Text
with javascript_semantics
|
|
string n = "000000"
|
|
|
|
function incn()
|
|
for i=length(n) to 1 by -1 do
|
|
if n[i]='9' then
|
|
if i=1 then return false end if
|
|
n[i]='0'
|
|
else
|
|
n[i] += 1
|
|
exit
|
|
end if
|
|
end for
|
|
return true
|
|
end function
|
|
|
|
sequence res = {}, bestseen
|
|
integer maxcycle = 0
|
|
|
|
procedure srs()
|
|
sequence curr = n
|
|
while length(curr)>1 and curr[1]='0' do
|
|
curr = curr[2..$]
|
|
end while
|
|
integer ch = curr[1]
|
|
for i=2 to length(curr) do
|
|
if curr[i]>ch then return end if
|
|
ch = curr[i]
|
|
end for
|
|
sequence seen = {curr}
|
|
integer cycle = 1
|
|
while true do
|
|
sequence digits = repeat(0,10)
|
|
for i=1 to length(curr) do
|
|
integer idx = curr[i]-'0'+1
|
|
digits[idx] += 1
|
|
end for
|
|
string next = ""
|
|
for i=length(digits) to 1 by -1 do
|
|
if digits[i]!=0 then
|
|
next &= sprint(digits[i])
|
|
next &= i+'0'-1
|
|
end if
|
|
end for
|
|
if find(next,seen) then exit end if
|
|
seen = append(seen,next)
|
|
curr = next
|
|
cycle += 1
|
|
end while
|
|
if cycle>maxcycle then
|
|
res = {seen[1]}
|
|
maxcycle = cycle
|
|
bestseen = seen
|
|
elsif cycle=maxcycle then
|
|
res = append(res,seen[1])
|
|
end if
|
|
end procedure
|
|
|
|
while true do
|
|
srs()
|
|
if not incn() then exit end if
|
|
end while
|
|
|
|
-- add non-leading-0 perms:
|
|
for i=length(res) to 1 by -1 do
|
|
string ri = res[i]
|
|
for p=1 to factorial(length(ri)) do
|
|
string pri = permute(p,ri)
|
|
if pri[1]!='0' and not find(pri,res) then
|
|
res = append(res,pri)
|
|
end if
|
|
end for
|
|
end for
|
|
?res
|
|
puts(1,"cycle length is ") ?maxcycle
|
|
pp(bestseen,{pp_Nest,1})
|