Question: For this computer assignment, you are to write and implement an interactive C++ program to find and print all prime numbers, which are less than
For this computer assignment, you are to write and implement an interactive C++ program to find and print all prime numbers, which are less than or equal to a given value of n, using the algorithm known as the Sieve of Eratosthenes.
The algorithm uses an optimization feature by looking at the key values for m <= sqrt ( n ). However, in your implementation, test all numbers m such that m * m <= n, rather than computing the square root of n.
Begin with the integer m = 2, which is the smallest prime number. The pass scans the set and removes all multiples of 2, having the form 2 * k for k >= 2. The multiples cannot be prime numbers, because they are divisible by 2
Use a set container to store the prime numbers. In the STL, a set is implemented as an associative container, it uses the model of a mathematical set, it stores keys that are objects of a specified data type, where duplicate keys are not allowed. To use a set in your program, you need to insert the statement: #include in your header file.
In addition to the main ( ) routine implement (at least) the following two subroutines in your program:
void sieve ( set < int >& s, int n ) : This routine can be used to apply the Sieve of Eratosthenes algorithm to remove all nonprime numbers from the integer set s = { 2, 3, , n }.
void print_primes ( const set < int >& s ) : This routine can be used to print the elements in the integer set s (NO_ITEMS = 16 primes per line and ITEM_W = 4 spaces allocated for each prime)
Include your header file prog3.h, by inserting the statement: #include prog3.h at the top of your source file prog3.cc.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
