Last active
September 24, 2021 11:30
-
-
Save IAFahim/32a876109f9d1c79e681e8889dd36670 to your computer and use it in GitHub Desktop.
isPrime Java
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 static boolean isPrime(int x) { | |
if (x <= 1) return false; | |
if (x == 2 || x == 3 || x == 5) { | |
return true; | |
} | |
if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0) { | |
return false; | |
} | |
int sqrt = (int) Math.floor(Math.sqrt(x)); | |
for (int i = 6; i <= sqrt; i += 6) { | |
if (x % (i + 1) == 0 || x % (i + 5) == 0) return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment