Hi

My assignment is to find the unique prime factors of a user inputed number. So for 54, the uniqe prime factors would be 2 and 3. If we display the factors as 2 3 3 3 we get extra credit. I figured out how to do this without manipulating bit by bit (not the extra credit). We were never told if it had to be bit by bit so my question is: is it easy to display these factors using bit manipulation? The reason i ask is if its 10,000 lines or something crazy i doubt my teacher would make us do it. If it is simple and I just can't get my head around it then please tell me so and please provide a starting point.

If you have a way for me to get the extra credit with my current code I would appreciate it cuz i cant figure it out.

My source code is below

Thanks

Code:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <bitset>
using namespace std;
void getFactors (int n)
{
for ( int i = 1 ; i <= n ; i++ )
{
int j = i - 1;
while ( j > 1 )
{
if ( i % j == 0 )
break;
else
j--;
}
if ( j == 1 )
{
if ( n % i == 0 )
{
cout<<i<<" ";
}
}
}
cout<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
const int SIZE = 1024;
int value;
std::bitset< SIZE > sieve; // create bitset of 1024 bits
sieve.flip(); // flip all bits in bitset 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;
// determine all prime numbers from 2 to 1024
for ( int i = 2; i < finalBit; i++ )
{
if ( sieve.test( i ) ) // bit i is on
{
for ( int j = 2 * i; j < SIZE; j += i )
sieve.reset( j ); // set bit j off
} // end if
} // end for
cout << "The prime numbers in the range 2 to 1023 are:\n";
// display prime numbers in range 2-1023
for ( int k = 2, counter = 1; k < SIZE; k++ )
{
if ( sieve.test( k ) ) // bit k is on
{
cout << setw( 5 ) << k;
if ( counter++ % 12 == 0 ) // counter is a multiple of 12
cout << '\n';
} // end if
} // end for
cout << endl;
// get value from user
cout << "\nEnter a value from 2 to 1023 (-1 to end): ";
cin >> value;
// determine whether user input is prime
while ( value != -1 )
{
if ( sieve[ value ] ) // prime number
{
cout << value << " is a prime number\n";
}
else // not a prime number
{
cout << value << " is not a prime number\n The unique prime factors are: ";
getFactors(value);
}
cout << "\nEnter a value from 2 to 1023 (-1 to end): ";
cin >> value;
} // end while
return 0;
}