(Prime Numbers) Modify Exercise 21.13 so that, if the number the user inputs into the pro- gram is not prime, the program displays the prime factors of the number and the number of times each prime factor appears in the unique prime factorization. For example, the output for the num- ber 54 should be The unique prime factorization of 54 is: 2 * 3 * 3 * 3
What will be an ideal response?
```
#include
#include
#include
using namespace std;
int main()
{
const int SIZE = 1024;
int i;
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"
<< "The unique prime factorization of " << value << " is: ";
for ( int f = 2; f < SIZE; )
{
if ( sieve.test( f ) && value % f == 0 )
{
cout << f; // output factor
value /= f; // modify value
if ( value <= 1 ) // time to stop
break;
cout << " * ";
} // end if
else
+f; // move to next prime
} // 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): 99
99 is not a prime number
The unique prime factorization of 99 is: 3 * 3 * 11
Enter a value from 2 to 1023 (-1 to end): 888
888 is not a prime number
The unique prime factorization of 888 is: 2 * 2 * 2 * 3 * 37
Enter a value from 2 to 1023 (-1 to end): -1
You might also like to view...
A ________ is a portion of the document that can be formatted differently from the rest of the document
A) page break B) manual page break C) section D) section break
Wrist and finger pain is usually the result of a mouse that needs maintenance.
Answer the following statement true (T) or false (F)