How to resolve the algorithm Look-and-say sequence step by step in the C programming language
How to resolve the algorithm Look-and-say sequence step by step in the C 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 C programming language
The provided C code generates and prints the look-and-say sequence. The look-and-say sequence is a sequence of numbers where each term is the number of times the previous term repeats itself, followed by the term itself.
Here's how the code works:
-
Initialize three character pointers:
a
,b
, andx
.a
is initially allocated memory for two characters (to store the first number, "1").b
is initially a null pointer, andx
is used as a temporary variable. -
Enter a
for
loop that continues untilb
can't be reallocated to a larger size. -
Inside the loop:
- Print the value of
x
, which is a copy of the current string ina
. - Count the number of consecutive occurrences of each character in
a
and build the next term of the look-and-say sequence inb
. The code useslen
to keep track of the current length ofb
andcnt
to count consecutive occurrences of a character. - Update
a
to point to the newly allocated memory forb
and setx
toa
(sox
points to the current string).
- Print the value of
-
Continue the loop until
realloc
fails, indicating that no more memory can be allocated.
This code will print the look-and-say sequence, starting with "1" and continuing indefinitely. The first few terms of the sequence are:
1
11
21
1211
111221
312211
13112221
1113213211
...
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Ol programming language
You may also check:How to resolve the algorithm Dot product step by step in the Nim programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the Pascal programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the WDTE programming language
You may also check:How to resolve the algorithm Guess the number step by step in the AutoHotkey programming language