Dec 26, 2008

Write code to detect whether or not a given number is prime

Write code to detect whether or not a given number is prime


bool foo(int num)
{
if(num<=1) return false;
if(num==2) return true;
if(!num%2) return false;
for(int i=3;i < sqrt(num);i++)
{if(!num%i)return false;}
return true;
}


Better solution, see wiki

No comments:

Post a Comment