Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
3
Indexable
Never
import java.util.Arrays;

public class UserMainCode {

    public static int findSumOfPrimes(int[] input1, int input2) {
        int smallestPrime = findSmallestPrime(input1);

        if (smallestPrime == Integer.MAX_VALUE) {
            smallestPrime = findSmallestNonPrime(input1);
        }

        final int finalSmallestPrime = smallestPrime; 

        int sumOfPrimes = Arrays.stream(input1)
                .filter(UserMainCode::isPrime)
                .filter(n -> n != finalSmallestPrime) 
                .sum();

        return sumOfPrimes;
    }

    public static boolean isPrime(int num) {
        if (num <= 1) return false;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) return false;
        }
        return true;
    }

    public static int findSmallestPrime(int[] arr) {
        int smallestPrime = Integer.MAX_VALUE;
        for (int num : arr) {
            if (isPrime(num)) {
                smallestPrime = Math.min(smallestPrime, num);
            }
        }
        return smallestPrime;
    }

    public static int findSmallestNonPrime(int[] arr) {
        int smallestNonPrime = Integer.MAX_VALUE;
        for (int num : arr) {
            if (!isPrime(num)) {
                smallestNonPrime = Math.min(smallestNonPrime, num);
            }
        }
        return smallestNonPrime;
    }
}