Skip to content

Commit 2f62929

Browse files
authored
Correct the prime check algorithm
There are no prime numbers smaller than 2 and numbers greater than and divisible by 2 are not prime.
1 parent 5d77b08 commit 2f62929

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

β€ŽMaths/PrimeCheck.javaβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public static void main(String[] args) {
2121
* @return {@code true} if {@code n} is prime
2222
*/
2323
public static boolean isPrime(int n) {
24+
if (n == 2) {
25+
return true;
26+
}
27+
if (n < 2 || n % 2 == 0) {
28+
return false;
29+
}
2430
for (int i = 3; i <= Math.sqrt(n); i += 2) {
2531
if (n % i == 0) {
2632
return false;

0 commit comments

Comments
 (0)