Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started