How to resolve the algorithm Yellowstone sequence step by step in the C++ programming language
How to resolve the algorithm Yellowstone sequence step by step in the C++ programming language
Table of Contents
Problem Statement
The Yellowstone sequence, also called the Yellowstone permutation, is defined as: For n <= 3, For n >= 4,
The sequence is a permutation of the natural numbers, and gets its name from what its authors felt was a spiking, geyser like appearance of a plot of the sequence.
a(4) is 4 because 4 is the smallest number following 1, 2, 3 in the sequence that is relatively prime to the entry before it (3), and is not relatively prime to the number two entries before it (2).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Yellowstone sequence step by step in the C++ programming language
The provided C++ code implements a generator for the Yellowstone numbers, which are a sequence of positive integers defined by the following set of rules:
- 1 and 2 are Yellowstone numbers.
- For any integer n >= 3, n is a Yellowstone number if and only if:
- n does not belong to the set of already generated Yellowstone numbers.
- The greatest common divisor (GCD) of n with the previous Yellowstone number (denoted by n1) is 1.
- The GCD of n with the second-to-last Yellowstone number (denoted by n2) is greater than 1.
The yellowstone_generator
class is a template class that generates Yellowstone numbers of a given integer type. It maintains a set of generated Yellowstone numbers (sequence_
) and three private member variables (min_
, n_
, and n1_
, n2_
) to keep track of the current and previous Yellowstone numbers.
The next()
method generates the next Yellowstone number. It first updates n2_
to n1_
and n1_
to n_
. Then, it checks if n_
is less than 3. If n_
is less than 3, it simply increments n_
and returns it.
If n_
is greater than or equal to 3, it enters a loop to find the next Yellowstone number. It starts by incrementing n_
until it finds a number that satisfies the three conditions mentioned earlier. Once it finds a suitable number, it inserts it into the sequence_
set and removes all the numbers less than min_
from the set. It then increments min_
and returns the newly generated Yellowstone number.
The main()
function creates an instance of the yellowstone_generator
class with an unsigned int
type and prints the first 30 Yellowstone numbers.
Here is a step-by-step explanation of the code:
-
The
yellowstone_generator
class is a template class that takes an integer type as a parameter. -
The
next()
method of theyellowstone_generator
class generates the next Yellowstone number. It does this by maintaining a set of generated Yellowstone numbers (sequence_
), and three private member variables (min_
,n_
, andn1_
,n2_
) to keep track of the current, previous, and second-to-last Yellowstone numbers. -
The
next()
method first updatesn2_
ton1_
andn1_
ton_
. Then, it checks ifn_
is less than 3. Ifn_
is less than 3, it simply incrementsn_
and returns it. -
If
n_
is greater than or equal to 3, it enters a loop to find the next Yellowstone number. It starts by incrementingn_
until it finds a number that satisfies the three conditions mentioned earlier. Once it finds a suitable number, it inserts it into thesequence_
set and removes all the numbers less thanmin_
from the set. It then incrementsmin_
and returns the newly generated Yellowstone number. -
The
main()
function creates an instance of theyellowstone_generator
class with anunsigned int
type and prints the first 30 Yellowstone numbers.
Here is an example output of the program:
First 30 Yellowstone numbers:
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109
Source code in the cpp programming language
#include <iostream>
#include <numeric>
#include <set>
template <typename integer>
class yellowstone_generator {
public:
integer next() {
n2_ = n1_;
n1_ = n_;
if (n_ < 3) {
++n_;
} else {
for (n_ = min_; !(sequence_.count(n_) == 0
&& std::gcd(n1_, n_) == 1
&& std::gcd(n2_, n_) > 1); ++n_) {}
}
sequence_.insert(n_);
for (;;) {
auto it = sequence_.find(min_);
if (it == sequence_.end())
break;
sequence_.erase(it);
++min_;
}
return n_;
}
private:
std::set<integer> sequence_;
integer min_ = 1;
integer n_ = 0;
integer n1_ = 0;
integer n2_ = 0;
};
int main() {
std::cout << "First 30 Yellowstone numbers:\n";
yellowstone_generator<unsigned int> ygen;
std::cout << ygen.next();
for (int i = 1; i < 30; ++i)
std::cout << ' ' << ygen.next();
std::cout << '\n';
return 0;
}
You may also check:How to resolve the algorithm Pi step by step in the Elixir programming language
You may also check:How to resolve the algorithm Pragmatic directives step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Filter step by step in the Ruby programming language
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the Common Lisp programming language