Question: Write a program in C++ which reads an endless sequence of integers from stdin and tests whether each number is prime or not and prints

Write a program in C++ which reads an endless sequence of integers from stdin and tests whether each number is prime or not and prints a meaningful message to stdout if it is. Include a function isprime(int)" which is a boolean function that does all the checking. Move the "isprime(int)" code into a class called "isprime" that overloads its function operator to act as a function object. Instantiate an "isprime" object in the main program and use it to test the input data.
To avoid having to iteratively test each new input against all possible numbers smaller than its square root, create an increasingly larger list of prime numbers and use a linear search to see if the number in question is listed or not. Initially, the list should only contain the number 2. If you want to see, whether N is prime number, have the overloaded function operator call a private member function that expands the list of known prime numbers to cover N. Then test if N is in the list by traversing the list. You are encouraged to use "find()" from STL to do the traversal but you don't have to. Note that "find()" returns an iterator to the element, if it was in the list, and the end-iterator, if it wasn't found. You must translate this into a boolean variable that indicates whether the number N was found in the list or not.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!