How to resolve the algorithm Summarize and say sequence step by step in the 11l programming language
How to resolve the algorithm Summarize and say sequence step by step in the 11l programming language
Table of Contents
Problem Statement
There are several ways to generate a self-referential sequence. One very common one (the Look-and-say sequence) is to start with a positive integer, then generate the next term by concatenating enumerated groups of adjacent alike digits: The terms generated grow in length geometrically and never converge. Another way to generate a self-referential sequence is to summarize the previous term. Count how many of each alike digit there is, then concatenate the sum and digit for each of the sorted enumerated digits. Note that the first five terms are the same as for the previous sequence. Sort the digits largest to smallest. Do not include counts of digits that do not appear in the previous term. Depending on the seed value, series generated this way always either converge to a stable value or to a short cyclical pattern. (For our purposes, I'll use converge to mean an element matches a previously seen element.) The sequence shown, with a seed value of 0, converges to a stable value of 1433223110 after 11 iterations. The seed value that converges most quickly is 22. It goes stable after the first element. (The next element is 22, which has been seen before.)
Find all the positive integer seed values under 1000000, for the above convergent self-referential sequence, that takes the largest number of iterations before converging. Then print out the number of iterations and the sequence they return. Note that different permutations of the digits of the seed will yield the same sequence. For this task, assume leading zeros are not permitted.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Summarize and say sequence step by step in the 11l programming language
Source code in the 11l programming language
[String] result
V longest = 0
F make_sequence(n) -> N
DefaultDict[Char, Int] map
L(c) n
map[c]++
V z = ‘’
L(k) sorted(map.keys(), reverse' 1B)
z ‘’= Char(code' map[k] + ‘0’.code)
z ‘’= k
I :longest <= z.len
:longest = z.len
I z !C :result
:result [+]= z
make_sequence(z)
L(test) [‘9900’, ‘9090’, ‘9009’]
result.clear()
longest = 0
make_sequence(test)
print(‘[#.] Iterations: #.’.format(test, result.len + 1))
print(result.join("\n"))
print("\n")
You may also check:How to resolve the algorithm Loops/For step by step in the Prolog programming language
You may also check:How to resolve the algorithm Copy a string step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Super-d numbers step by step in the J programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Haskell programming language