Summarize and say sequence: Difference between revisions

Add CLU
(Add CLU)
Line 1,027:
(doseq [ds result]
(println (apply str ds))))</lang>
 
=={{header|CLU}}==
<lang clu>summarize = proc (s: string) returns (string) signals (bad_format)
digit_count: array[int] := array[int]$fill(0,10,0)
for c: char in string$chars(s) do
d: int := int$parse(string$c2s(c)) resignal bad_format
digit_count[d] := digit_count[d] + 1
end
out: stream := stream$create_output()
for d: int in int$from_to_by(9,0,-1) do
if digit_count[d]>0 then
stream$puts(out, int$unparse(digit_count[d]))
stream$puts(out, int$unparse(d))
end
end
return(stream$get_contents(out))
end summarize
 
converge = proc (s: string) returns (int) signals (bad_format)
seen: array[string] := array[string]$[]
steps: int := 0
while true do
for str: string in array[string]$elements(seen) do
if str = s then return(steps) end
end
array[string]$addh(seen, s)
s := summarize(s)
steps := steps + 1
end
end converge
 
start_up = proc ()
po: stream := stream$primary_output()
seeds: array[int]
max: int := 0
for i: int in int$from_to(1, 999999) do
steps: int := converge(int$unparse(i))
if steps > max then
max := steps
seeds := array[int]$[i]
elseif steps = max then
array[int]$addh(seeds,i)
end
end
stream$puts(po, "Seed values: ")
for i: int in array[int]$elements(seeds) do
stream$puts(po, int$unparse(i) || " ")
end
stream$putl(po, "\nIterations: " || int$unparse(max))
stream$putl(po, "\nSequence: ")
s: string := int$unparse(array[int]$bottom(seeds))
for i: int in int$from_to(1, max) do
stream$putl(po, s)
s := summarize(s)
end
end start_up</lang>
{{out}}
<pre>Seed values: 9009 9090 9900
Iterations: 21
 
Sequence:
9009
2920
192210
19222110
19323110
1923123110
1923224110
191413323110
191433125110
19151423125110
19251413226110
1916151413325110
1916251423127110
191716151413326110
191726151423128110
19181716151413327110
19182716151423129110
29181716151413328110
19281716151423228110
19281716151413427110
19182716152413228110</pre>
 
=={{header|CoffeeScript}}==
2,114

edits