How to resolve the algorithm Look-and-say sequence step by step in the Pascal programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Look-and-say sequence step by step in the Pascal 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 Pascal programming language
Source code in the pascal programming language
program LookAndSayDemo(input, output);
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
uses
SysUtils;
function LookAndSay(s: string): string;
var
item: char;
index: integer;
count: integer;
begin
Result := '';
item := s[1];
count := 1;
for index := 2 to length(s) do
if item = s[index] then
inc(count)
else
begin
Result := Result + intTostr(count) + item;
item := s[index];
count := 1;
end;
Result := Result + intTostr(count) + item;
end;
var
number: string;
begin
writeln('Press RETURN to continue and ^C to stop.');
number := '1';
while not eof(input) do
begin
write(number);
readln;
number := LookAndSay(number);
end;
end.
program LookAndSayDemo(input, output);
{$IFDEF FPC}
{$Mode Delphi} // using result
{$optimization ON}
// i3-4330 3.5 Ghz
// {$CodeAlign proc=16,loop=8} //2,6 secs
{$CodeAlign proc=16,loop=1} //1,6 secs so much faster ???
{$ENDIF}
uses
SysUtils;
const
cntChar : array[1..9] of char =
('1','2','3','4','5','6','7','8','9');
function LookAndSay2 (const s: string): string;
//using pChar for result
var
source,
destin : pChar;
len,
idxFrom,
idxTo : integer;
cnt: integer;
item: char;
begin
idxFrom := length(s);
source := @s[1];
//adjust length of result
len := round(length(s)* 1.306+10);
setlength(result,len);
destin := @result[1];
dec(destin);
idxto := 1;
item := source^;
inc(source);
cnt := 1;
for idxFrom := idxFrom downto 2 do
begin
if item <> source^ then
begin
destin[idxTo] := cntChar[cnt];
destin[idxTo+1]:= item;
item := source^;
cnt := 1;
inc(idxto,2);
end
else
inc(cnt);
inc(source);
end;
destin[idxTo] := cntChar[cnt];
destin[idxTo+1]:= item;
setlength(result,idxto+1);
end;
var
number: string;
l1,l2,
i : integer;
begin
number := '1';
writeln(number);
writeln(1:4,length(number):16,1/1:10:6);
For i := 2 to 70 do
begin
l1 := length(number);
number := LookAndSay2(number);
l2 := length(number);
IF i <10 then
writeln(number);
writeln(i:4,length(number):16,l2/l1:10:6);
end;
end.
You may also check:How to resolve the algorithm Command-line arguments step by step in the Lua programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm String case step by step in the Zoea Visual programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the Insitux programming language
You may also check:How to resolve the algorithm Terminal control/Hiding the cursor step by step in the FunL programming language