TheAlgorithms/Java · critical · IllegalStateException

No valid prime sum found.

Error message

No valid prime sum found.

What it means

Thrown as IllegalStateException (not IllegalArgumentException) by GoldbachConjecture.getPrimeSum when the search loop completes without finding two primes that sum to the input. The comment says 'Should not occur' — this is a defensive guard against a counterexample to Goldbach's Conjecture, which has been verified for all even integers up to 4×10^18. If this fires, either the input bypassed the precondition check or there is a bug in isPrime.

Source

Thrown at src/main/java/com/thealgorithms/maths/GoldbachConjecture.java:28

 */

public final class GoldbachConjecture {
    private GoldbachConjecture() {
    }
    public record Result(int number1, int number2) {
    }

    public static Result getPrimeSum(int number) {
        if (number <= 2 || number % 2 != 0) {
            throw new IllegalArgumentException("Number must be even and greater than 2.");
        }

        for (int i = 0; i <= number / 2; i++) {
            if (isPrime(i) && isPrime(number - i)) {
                return new Result(i, number - i);
            }
        }
        throw new IllegalStateException("No valid prime sum found."); // Should not occur
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Verify that isPrime is correct and unmodified — this is the most likely culprit if the error fires.
  2. Ensure the precondition (number > 2 and even) is always enforced before calling.
  3. If you are extending this class, do not bypass the initial validation check.
  4. Treat this as an assertion failure: investigate the isPrime implementation rather than the caller logic.

Example fix

// This error is defensive/unreachable; no caller-side fix applies.
// If it fires, investigate isPrime correctness:

// Verify isPrime for known primes near your input
for (int i = 2; i <= n / 2; i++) {
    if (!GoldbachConjecture.isPrime(i)) {
        System.err.println("isPrime bug detected at i=" + i);
    }
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
    var result = GoldbachConjecture.getPrimeSum(number);
} catch (IllegalStateException e) {
    // This should never happen for valid even inputs > 2.
    // If it does, isPrime may be corrupted — investigate immediately.
    throw new AssertionError("Goldbach conjecture violation detected", e);
}

Prevention

When it happens

Trigger: This should never fire if the precondition (even number > 2) is met and isPrime is correct. It could fire if: (1) the class is subclassed or modified to skip validation, (2) isPrime has a bug returning false for actual primes, or (3) integer overflow in isPrime's internal arithmetic causes incorrect results. The loop iterates i from 0 to number/2 checking isPrime(i) && isPrime(number - i).

Common situations: This is a theoretically-unreachable defensive throw. In practice, encountering it suggests a corrupted or modified isPrime implementation, or that someone changed the precondition guard. It serves as an assertion-style safeguard.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/4af429e866d2dbf3. Report an issue: GitHub.