Skip to content

Instantly share code, notes, and snippets.

@phc15
Last active March 8, 2021 07:19
Show Gist options
  • Save phc15/d1e860f0e6cd883ed1df68f84f5e54fa to your computer and use it in GitHub Desktop.
Save phc15/d1e860f0e6cd883ed1df68f84f5e54fa to your computer and use it in GitHub Desktop.
public class Prime {
public static boolean isPrime(int num) {
if (num > Math.pow(2, 31)) return false;
if (num < 0 || num == 0 || num == 1) return false;
// A not prime num = a * b, a will always smaller or equal to b (vice versa)
// & a and b can't both larger then square root of num(e.g. 36 = 6 * 6 = 4 * 9)
int n = (int)Math.sqrt(num);
for(int i = 2; i <= n ; i++){
if(num % i == 0) return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment