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

Published on 12 May 2024 09:40 PM

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

Source code in the unix programming language

lookandsay() {
    local num=$1 char seq i
    for ((i=0; i<=${#num}; i++)); do
        char=${num:i:1}
        if [[ $char == ${seq:0:1} ]]; then
            seq+=$char
        else
            [[ -n $seq ]] && printf "%d%s" ${#seq} ${seq:0:1}
            seq=$char
        fi
    done
}

for ((num=1, i=1; i<=10; i++)); do
    echo $num
    num=$( lookandsay $num )
done

  

You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm 9 billion names of God the integer step by step in the AutoHotkey programming language