Optimized Function: Check if a number is Prime or not Java code: 🧊
public class CheckPrimeOptimized {
public static boolean isPrime(int n) {
if (n == 2) {
return true;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(6833));
System.out.println(isPrime(6834));
}
}
java .\CheckPrimeOptimized.java
true
false
No comments:
Post a Comment