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 seen, this = n integer cycle = 1 while length(this)>1 and this[1]='0' do this = this[2..$] end while integer ch = this[1] for i=2 to length(this) do if this[i]>ch then return end if ch = this[i] end for seen = {this} while 1 do sequence digits = repeat(0,10) for i=1 to length(this) do digits[this[i]-'0'+1] += 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) this = 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 1 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})