Summarize and say sequence: Difference between revisions

(Added Lua version)
Line 2,667:
19281716151413427110
19182716152413228110
</pre>
 
=={{header|Phix}}==
Optimisation idea taken from CoffeeScript, completes in under a second.
<lang Phix>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})</lang>
<pre>
{"9900","9009","9090"}
cycle length is 21
{"9900",
"2920",
"192210",
"19222110",
"19323110",
"1923123110",
"1923224110",
"191413323110",
"191433125110",
"19151423125110",
"19251413226110",
"1916151413325110",
"1916251423127110",
"191716151413326110",
"191726151423128110",
"19181716151413327110",
"19182716151423129110",
"29181716151413328110",
"19281716151423228110",
"19281716151413427110",
"19182716152413228110"}
</pre>
 
7,806

edits