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

Published on 12 May 2024 09:40 PM

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

Source code in the clu programming language

look_and_say = proc (s: string) returns (string)
    out: array[char] := array[char]$[]
    count: int := 0
    last: char := '\000'
    
    for c: char in string$chars(s) do
        if c ~= last then   
            if count ~= 0 then
                array[char]$addh(out, char$i2c(count + 48))
                array[char]$addh(out, last)
            end
            last := c
            count := 1
        else
            count := count + 1
        end
    end
    
    array[char]$addh(out, char$i2c(count + 48))
    array[char]$addh(out, last)
    return (string$ac2s(out))
end look_and_say

start_up = proc ()
    lines = 15
    
    po: stream := stream$primary_output()
    cur: string := "1"
    
    for i: int in int$from_to(1, lines) do
        stream$putl(po, cur)
        cur := look_and_say(cur)
    end
end start_up

  

You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Batch File programming language
You may also check:How to resolve the algorithm String comparison step by step in the Oforth programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the LSL programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Python programming language
You may also check:How to resolve the algorithm Percentage difference between images step by step in the PicoLisp programming language