Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed
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 "find0" from STL to do the traversal but you don't have to. Note that "find0" 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 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 "find0" from STL to do the traversal but you don't have to. Note that "find0" 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions