How to resolve the algorithm Summarize and say sequence step by step in the q programming language

Published on 12 May 2024 09:40 PM
#Q

How to resolve the algorithm Summarize and say sequence step by step in the q 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 q programming language

Source code in the q programming language

ls:{raze(string 1_ deltas d,count x),'x d:where differ x}  / look & say
sumsay:ls desc@                                            / summarize & say

seeds:group desc each string til 1000000                   / seeds for million integers
seq:(key seeds)!30 sumsay\'key seeds                       / sequences for unique seeds
top:max its:(count distinct@)each seq                      / count iterations

/ report results
rpt:{1 x,": ",y,"\n\n";}
rpt["Seeds"]" "sv string raze seeds where its=top          / all forms of top seed/s
rpt["Iterations"]string top
rpt["Sequence"]"\n\n","\n"sv raze seq where its=top


  

You may also check:How to resolve the algorithm Empty directory step by step in the AWK programming language
You may also check:How to resolve the algorithm Sleep step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Average loop length step by step in the VBA programming language
You may also check:How to resolve the algorithm Read a file character by character/UTF8 step by step in the Julia programming language
You may also check:How to resolve the algorithm Greyscale bars/Display step by step in the EasyLang programming language