DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Kunal has posted 6 posts at DZone. View Full User Profile

IsPrime

09.10.2006
| 8310 views |
  • submit to reddit
        // finding whether 'n' is prime or not

private boolean isPrime (int n)
{
   if (n<=1) return false;
   if (n==2) return true;
   if (n%2==0) return false;
   int m=(int)Math.round(Math.sqrt(n));

   for (int i=3; i<=m; i+=2)
      if (n%i==0)
         return false;

   return true;
}