Last active
March 8, 2021 07:19
-
-
Save phc15/d1e860f0e6cd883ed1df68f84f5e54fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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