Skip to content

Instantly share code, notes, and snippets.

@AravindaM
Created January 12, 2016 03:14
Show Gist options
  • Save AravindaM/d5dab80c1057a6903992 to your computer and use it in GitHub Desktop.
Save AravindaM/d5dab80c1057a6903992 to your computer and use it in GitHub Desktop.
Check if interger is a prime...
/*
* An optimized to check if a number is prime or not.
*/
public static boolean isPrime(int num) {
if (num == 2 || num == 3) {
return true;
}
if (num % 2 == 0 || num % 3 == 0) {
return false;
}
for (int i = 3; i < Math.sqrt(num); i += 2) {
if (num % i == 0 || num % Math.sqrt(num) == 0) {
return false;
}
}
return true;
}
// Read more: http://java67.blogspot.com/2016/01/java-program-to-print-prime-numbers.html#ixzz3wzp2ng3g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment