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

Published on 12 May 2024 09:40 PM

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

Source code in the powerbasic programming language

FUNCTION RLEncode (i AS STRING) AS STRING
    DIM tmp1 AS STRING, tmp2 AS STRING, outP AS STRING
    DIM Loop0 AS LONG, count AS LONG

    FOR Loop0 = 1 TO LEN(i)
        tmp1 = MID$(i, Loop0, 1)
        IF tmp1 <> tmp2 THEN
            IF count > 1 THEN
                outP = outP & TRIM$(STR$(count)) & tmp2
                tmp2 = tmp1
                count = 1
            ELSEIF 0 = count THEN
                tmp2 = tmp1
                count = 1
            ELSE
                outP = outP & "1" & tmp2
                tmp2 = tmp1
            END IF
        ELSE
            INCR count
        END IF
    NEXT

    outP = outP & TRIM$(STR$(count)) & tmp2
    FUNCTION = outP
END FUNCTION

FUNCTION lookAndSay(BYVAL count AS LONG) AS STRING
    DIM iii AS STRING, tmp AS STRING

    IF count > 1 THEN
        iii = lookAndSay(count - 1)
    ELSEIF count < 2 THEN
        iii = "1"
    END IF

    tmp = RLEncode(iii)
    lookAndSay = tmp
END FUNCTION

FUNCTION PBMAIN () AS LONG
    DIM v AS LONG
    v = VAL(INPUTBOX$("Enter a number."))
    MSGBOX lookAndSay(v)
END FUNCTION

  

You may also check:How to resolve the algorithm 100 doors step by step in the Dafny programming language
You may also check:How to resolve the algorithm FASTA format step by step in the Sidef programming language
You may also check:How to resolve the algorithm Lah numbers step by step in the zkl programming language
You may also check:How to resolve the algorithm Unbias a random generator step by step in the OCaml programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Scala programming language