#8159: 想請問已經用根號來判斷了,為何還逾時??


loyd (loyd)


請各位大大幫忙, 

我已經使用根號來判斷了,還要改哪裡才能不逾時呢?

 

#include<iostream>

#include<math.h>

using namespace std;

 

int main()

{

int x;

while( !cin.eof())

{

cin >> x;

if (x==1||x==2)

{

cout <<"質數"<<endl;

}

else

{

bool isPrime = false;

for( int i=2; i<sqrt(static_cast<double>(x)); i++)

{

if(x%i == 0)

{

isPrime = true;

cout <<"非質數"<<endl;

break;

}

}

if(isPrime == false)

cout <<"質數"<<endl;

}

}