Pages

Monday, July 25, 2011

a007. 判斷質數


#include
#include
using namespace std;

int main()
{
int input;
while( cin >> input )
{
if( input == 2 )
cout<<"質數"< else if( input % 2 == 0 )
cout<<"非質數"< else
{
int isPrime = 1;
for( int i = 3 ; i <= sqrt((double)input); i++ )
{
if( input % i == 0 )
{
cout<<"非質數"< isPrime = 0;
break;
}
}
if( isPrime )
cout<<"質數"< }
}

return 0;
}

No comments:

Post a Comment