Check Prime Optimized Function Code Java Programming

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

Max Sub Array Sum: Kadane's Algorithm

Code of: Max Sub Array Sum: Kadane's Algorithm. public class MaxSubArraySumKadanesALgorithm {     public static void Kadane ( int ...