public class PrimeFinder {

    // Function to check if a number is prime
    public static boolean isPrime(int num) {
        if (num <= 1) return false;
        if (num == 2 || num == 3) return true;
        if (num % 2 == 0 || num % 3 == 0) return false;

        for (int i = 5; i * i <= num; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }

    // Function to find the next prime number larger than the input
    public static int nextPrime(int num) {
        // Start searching from the next number
        int nextNumber = num + 1;

        // Loop until a prime number is found
        while (!isPrime(nextNumber)) {
            nextNumber++;
        }

        return nextNumber;
    }

    public static int prevPrime(int number) {
        if (number <= 2) {
            throw new IllegalArgumentException("No prime number smaller than 2.");
        }

        int closestPrime = number - 1;

        while (!isPrime(closestPrime)) {
            closestPrime--;
        }
        return closestPrime;
    }
}