How to resolve the algorithm Summarize and say sequence step by step in the Bracmat programming language
How to resolve the algorithm Summarize and say sequence step by step in the Bracmat 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 Bracmat programming language
Source code in the bracmat programming language
( ( self-referential
= seq N next
. ( next
= R S d f
. 0:?S
& whl
' (@(!arg:%@?d ?arg)&(.!d)+!S:?S)
& :?R
& whl
' ( !S:#?f*(.?d)+?S
& !f !d !R:?R
)
& str$!R
)
& 1:?N
& !arg:?seq
& whl
' ( next$!arg:?arg
& ~(!seq:? !arg ?)
& !arg !seq:?seq
& 1+!N:?N
)
& (!seq.!N)
)
& ( Perm
= permutations S p
. :?permutations
& ( perm
= prefix List original A Z p
. !arg:(?prefix.)
& str$!prefix:?p
& (!S:?+(.!p)+?|(.!p)+!S:?S)
| !arg:(0 ?.?)&
| !arg:(?prefix.?List:?original)
& whl
' ( @(!List:%?A ?Z)
& perm$(!prefix !A.!Z)
& str$(!Z !A):~!original:?List
)
)
& 0:?S
& perm$(.!arg)
& :?permutations
& whl
' ( !S:?*(.?p)+?S
& !p !permutations:?permutations
)
& !permutations
)
& -1:?i:?max
& :?seqs
& whl
' ( 1+!i:<1000000:?i
& ( @(!i:? %@?a >%@!a ?)
| self-referential$!i
: ( ?seq
. ( >!max:?max&:?seqs
| !max
)
& ( "Seed Value(s):" Perm$!i
. "Sequence: (same for all three seeds except for first element)
"
!seq
)
!seqs
: ?seqs
)
|
)
)
& out$("Iterations:" !max !seqs)
);
You may also check:How to resolve the algorithm Unix/ls step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Delphi programming language
You may also check:How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the Factor programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the Stata programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the M2000 Interpreter programming language