Skip to content

Instantly share code, notes, and snippets.

@IAFahim
Last active September 24, 2021 11:30
Show Gist options
  • Save IAFahim/32a876109f9d1c79e681e8889dd36670 to your computer and use it in GitHub Desktop.
Save IAFahim/32a876109f9d1c79e681e8889dd36670 to your computer and use it in GitHub Desktop.
isPrime Java
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