#include // for use of atoi function #include #include using namespace std; int main( int argc, char* argv[] ) { // Check a command-line argument is provided. if ( argc !=2 ) { cerr << "Usage: " << argv[0] << " n" << "\n" << " where n is a positive integer.\n"; return 1; } // Convert the C-style string to an integer using the function atoi found in cstdlib. int n = atoi( argv[1] ); if ( n <= 0 ) { cerr << "The argument must be a positive integer.\n"; return 1; } // Make a list with n integers list primes; for ( int i=2; i<=n; ++i ) primes.push_back(i); list::iterator p = primes.begin(); while ( p != primes.end() ) { cout << *p << '\n'; list::iterator q = p; q ++ ; // make q refer to the next item after p while ( q != primes.end() ) { if ( *q % *p == 0 ) q = primes.erase(q); else ++q; } p ++ ; // move to the next prime } return 0; }