A easy to understand and optimal solution


#1

After entering 1 and the number itself , we don’t need to find all the other factors, instead if we get even one more factor than 2 , then we break,and have the result.
int Solution::isPrime(int A) {
vectorvect;
int i,count=0;
if(A<2)
vect.push_back(1);
vect.push_back(1);
vect.push_back(A);
for(i=2;i<=sqrt(A);i++)
{
if(A%i==0)
{
count++;
vect.push_back(i);
if(i!=sqrt(A))
vect.push_back(A/i);
}
if(count>1)
break;
}
if(vect.size()!=2)
return 0;
else
return 1;

}