Browse Source

Add method to check if number is prime

remotes/origin/test/client/add-test-cases
Alena Bandarovich 11 months ago
parent
commit
d8584854c3
  1. 18
      src/main/java/Calculations.java

18
src/main/java/Calculations.java

@ -16,4 +16,22 @@ public class Calculations {
} }
return factorial; return factorial;
} }
public static boolean isPrime(int num) {
if (num <= 1) {
return false; // Numbers less than or equal to 1 are not prime
}
if (num == 2 || num == 3) {
return true; // 2 and 3 are prime numbers
}
if (num % 2 == 0) {
return false; // Even numbers greater than 2 are not prime
}
for (int i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i == 0) {
return false; // If num is divisible by any number from 3 to sqrt(num), it's not prime
}
}
return true; // If num is not divisible by any number from 2 to sqrt(num), it's prime
}
} }
Loading…
Cancel
Save