How to resolve the algorithm Look-and-say sequence step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Look-and-say sequence step by step in the BASIC programming language

Table of Contents

Problem Statement

The   Look and say sequence   is a recursively defined sequence of numbers studied most notably by   John Conway.

The   look-and-say sequence   is also known as the   Morris Number Sequence,   after cryptographer Robert Morris,   and the puzzle   What is the next number in the sequence 1,   11,   21,   1211,   111221?   is sometimes referred to as the Cuckoo's Egg,   from a description of Morris in Clifford Stoll's book   The Cuckoo's Egg.

Sequence Definition

An example:

Write a program to generate successive members of the look-and-say sequence.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Look-and-say sequence step by step in the BASIC programming language

Source code in the basic programming language

10 DEFINT A-Z: I$="1"
20 FOR Z=1 TO 15
30 PRINT I$
40 O$=""
50 FOR I=1 TO LEN(I$)
60 C=1
70 IF MID$(I$,I,1)=MID$(I$,I+C,1) THEN C=C+1: GOTO 70
80 O$=O$+CHR$(C+48)+MID$(I$,I,1)
90 I=I+C-1
100 NEXT I
110 I$=O$
120 NEXT Z


10 I$="1"
20 FOR Z=1 TO 15
30  PRINT I$
40  O$=""
50  FOR I=1 TO LEN(I$)
60   C=1
70   IF MID$(I$,I,1)=MID$(I$,I+C,1) THEN C=C+1: GOTO 70
80   O$=O$+CHR$(C+48)+MID$(I$,I,1)
90   I=I+C-1
100  NEXT I
110  I$=O$
120 NEXT Z

100 cls
110 dim x$(2)
120 i = 0 ' índice de cadena de entrada
130 x$(i) = "1"
140 input "Indica cuantas repeticiones: ",r
150 print "Secuencia:"
160 print x$(i)
170 for n = 1 to r-1
180   j = 1-i ' índice de cadena de salida
190   x$(j) = ""
200   k = 1
210   while k <= len(x$(i))
220     k0 = k+1
230     while ((k0 <= len(x$(i))) and (mid$(x$(i),k,1) = mid$(x$(i),k0,1)))
240       k0 = k0+1
250     wend
260     x$(j) = x$(j)+str$(k0-k)+mid$(x$(i),k,1)
270     k = k0
280   wend
290   i = j
300   print x$(j)
310 next n
320 end


LET i$ = "1"
FOR z = 1 TO 15
    PRINT i$
    LET o$ = ""
    FOR i = 1 TO LEN(i$)
        LET c = 1
        DO WHILE (i$)[i:i+1-1] = (i$)[i+c:i+c+1-1]
           LET c = c+1
        LOOP
        LET o$ = o$ & CHR$(c+48) & (i$)[i:i+1-1]
        LET i = i+c-1
    NEXT i
    LET i$ = o$
NEXT z
END


  

You may also check:How to resolve the algorithm Associative array/Merging step by step in the Haskell programming language
You may also check:How to resolve the algorithm Regular expressions step by step in the MUMPS programming language
You may also check:How to resolve the algorithm 100 doors step by step in the E programming language
You may also check:How to resolve the algorithm Inheritance/Single step by step in the Scala programming language
You may also check:How to resolve the algorithm Attractive numbers step by step in the AWK programming language