How to resolve the algorithm Cullen and Woodall numbers step by step in the C++ programming language
How to resolve the algorithm Cullen and Woodall numbers step by step in the C++ programming language
Table of Contents
Problem Statement
A Cullen number is a number of the form n × 2n + 1 where n is a natural number. A Woodall number is very similar. It is a number of the form n × 2n - 1 where n is a natural number. So for each n the associated Cullen number and Woodall number differ by 2. Woodall numbers are sometimes referred to as Riesel numbers or Cullen numbers of the second kind.
Cullen primes are Cullen numbers that are prime. Similarly, Woodall primes are Woodall numbers that are prime. It is common to list the Cullen and Woodall primes by the value of n rather than the full evaluated expression. They tend to get very large very quickly. For example, the third Cullen prime, n == 4713, has 1423 digits when evaluated.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Cullen and Woodall numbers step by step in the C++ programming language
Code Overview:
This C++ code generates and displays sequences of Cullen and Woodhall numbers. Cullen and Woodhall numbers are defined by specific mathematical formulas.
Detailed Explanation:
-
Header Includes:
<cstdint>
: Used foruint32_t
data type.<iostream>
: Used for input/output operations.<string>
: Used for string manipulation.
-
Global Variables:
number
: Stores the current number in the sequence (initialized to 0).power
: Stores the current power of 2 (initialized to 1).
-
**Function
number_initialise()
:- Resets
number
to 0 andpower
to 1.
- Resets
-
Enumeration
NumberType
:- Enum representing the type of number sequence:
Cullen
: Cullen numbers.Woodhall
: Woodhall numbers.
- Enum representing the type of number sequence:
-
**Function
next_number()
:- Increments
number
by 1 and doublespower
(left shifts by 1). - Based on the
number_type
, it calculates the next number in the sequence:- For Cullen:
number * power + 1
. - For Woodhall:
number * power - 1
.
- For Cullen:
- Returns the calculated next number.
- Increments
-
**Function
number_sequence()
:- Generates and displays the first
count
numbers of the givennumber_type
. - Initializes
number
andpower
. - Iterates
count
times to calculate and display each number.
- Generates and displays the first
-
**Function
main()
:- Calls
number_sequence()
to generate and display sequences of 20 Cullen numbers and 20 Woodhall numbers.
- Calls
Example Output:
The first 20 Cullen numbers are:
1 3 9 27 81 243 729 2187 6561 19683 59049 177147 531441 1594323 4782969 14348907 43046721 129140163 387420489 1162261467
The first 20 Woodhall numbers are:
-1 -3 -7 -15 -31 -63 -127 -255 -511 -1023 -2047 -4095 -8191 -16383 -32767 -65535 -131071 -262143 -524287 -1048575
Source code in the cpp programming language
#include <cstdint>
#include <iostream>
#include <string>
uint32_t number, power;
void number_initialise() {
number = 0;
power = 1;
}
enum NumberType { Cullen, Woodhall };
uint32_t next_number(const NumberType& number_type) {
number += 1;
power <<= 1;
switch ( number_type ) {
case Cullen: return number * power + 1;
case Woodhall: return number * power - 1;
};
return 0;
}
void number_sequence(const uint32_t& count, const NumberType& number_type) {
std::string type = ( number_type == Cullen ) ? "Cullen" : "Woodhall";
std::cout << "The first " << count << " " << type << " numbers are:" << std::endl;
number_initialise();
for ( uint32_t index = 1; index <= count; ++index ) {
std::cout << next_number(number_type) << " ";
}
std::cout << std::endl << std::endl;
}
int main() {
number_sequence(20, Cullen);
number_sequence(20, Woodhall);
}
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the PL/I programming language
You may also check:How to resolve the algorithm Pig the dice game/Player step by step in the Racket programming language
You may also check:How to resolve the algorithm 15 puzzle solver step by step in the F# programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the Ada programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the BASIC256 programming language