(Sieve of Eratosthenes) Modify Fig. 22.40, the Sieve of Eratosthenes, so that, if the number the user inputs into the program is not prime, the program displays the prime factors of the number. Remember that a prime number’s factors are only 1 and the prime number itself. Every nonprime number has a unique prime factorization. For example, the factors of 54 are 2, 3, 3 and 3. When these values
are multiplied together, the result is 54. For the number 54, the prime factors output should be 2 and 3.
What will be an ideal response?
```
#include
#include
#include
#include
using namespace std;
int main()
{
const int SIZE = 1024;
int i; // counter variable
int value;
int counter;
bitset< SIZE > sieve; // create bitset of 1024 bits
sieve.flip(); // flip all bits in sieve
sieve.reset( 0 ); // reset first bit (number 0)
sieve.reset( 1 ); // reset second bit (number 1)
// perform Sieve of Eratosthenes
int finalBit = sqrt( static_cast< double >( sieve.size() ) ) + 1;
for ( i = 2; i < finalBit; i++ )
{
if ( sieve.test( i ) )
{
for ( int j = 2 * i; j < SIZE; j += i )
sieve.reset( j );
} // end if
} // end for
cout << "The prime numbers in the range 2 to 1023 are:\n";
// display prime numbers in range 2-1023
for ( i = 2, counter = 1; i < SIZE; i++ )
{
if ( sieve.test( i ) )
{
cout << setw( 5 ) << i;
if ( counter++ % 12 == 0 )
cout << '\n';
} // end if
} // end for
cout << endl;
// get a value from the user to determine if it is prime
cout << "\nEnter a value from 1 to 1023 (-1 to end): ";
cin >> value;
while ( value != -1 )
{
if ( sieve[ value ] )
cout << value << " is a prime number\n";
else
{
cout << value << " is not a prime number\n"
<< "prime factor(s): ";
bool print = true;
for ( int f = 2; f < SIZE; )
{
if ( sieve.test( f ) && value % f == 0 )
{
if ( print )
cout << f << ' '; // output factor
value /= f; // modify value
if ( value <= 1 ) // time to stop
break;
print = false;
} // end if
else
{
++f; // move to next prime
print = true;
} // end else
} // end for
cout << '\n';
} // end else
cout << "\nEnter a value from 2 to 1023 (-1 to end): ";
cin >> value;
} // end while
} // end main
```
The prime numbers in the range 2 to 1023 are:
2 3 5 7 11 13 17 19 23 29 31 37
41 43 47 53 59 61 67 71 73 79 83 89
97 101 103 107 109 113 127 131 137 139 149 151
157 163 167 173 179 181 191 193 197 199 211 223
227 229 233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349 353 359
367 373 379 383 389 397 401 409 419 421 431 433
439 443 449 457 461 463 467 479 487 491 499 503
509 521 523 541 547 557 563 569 571 577 587 593
599 601 607 613 617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719 727 733 739 743
751 757 761 769 773 787 797 809 811 821 823 827
829 839 853 857 859 863 877 881 883 887 907 911
919 929 937 941 947 953 967 971 977 983 991 997
1009 1013 1019 1021
Enter a value from 1 to 1023 (-1 to end): 8
8 is not a prime number
prime factor(s): 2
Enter a value from 2 to 1023 (-1 to end): 444
444 is not a prime number
prime factor(s): 2 3 37
Enter a value from 2 to 1023 (-1 to end): -1
You might also like to view...
Which profile is different from other profiles because it is not a complete profile?
A. default B. roaming C. mandatory D. public
You can use a(n) ________ to align the user interface controls in a form
Fill in the blank(s) with correct word
appears in class C, then:
If the line: friend class A; appears in class B, and the line: friend class B; a. Class A is a friend of class C. b. Class A can access private variables of class B. c. Class C can call class A’s private member functions. d. Class B can access class A’s private variables.
A Windows user can make a backup of the hard drive files with File History
Indicate whether the statement is true or false