TheAlgorithms/Java · error · IllegalArgumentException
Number must be even and greater than 2.
Error message
Number must be even and greater than 2.
What it means
Thrown by GoldbachConjecture.getPrimeSum(int number) when number is less than or equal to 2, or when number is odd. The Goldbach Conjecture states that every even integer greater than 2 can be expressed as the sum of two primes. The method enforces this precondition before searching for the prime pair. The check combines two conditions: number <= 2 (boundary) and number % 2 != 0 (parity).
Source
Thrown at src/main/java/com/thealgorithms/maths/GoldbachConjecture.java:20
import static com.thealgorithms.maths.Prime.PrimeCheck.isPrime;
/**
* This is a representation of the unsolved problem of Goldbach's Projection, according to which every
* even natural number greater than 2 can be written as the sum of 2 prime numbers
* More info: https://en.wikipedia.org/wiki/Goldbach%27s_conjecture
* @author Vasilis Sarantidis (https://github.com/BILLSARAN)
*/
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
- Validate that number is even AND > 2 before calling getPrimeSum.
- If the input may be odd or small, return early with a domain-appropriate result (e.g., Optional.empty()) instead of calling.
- Sanitize user input to ensure it meets the Goldbach constraint.
Example fix
// before
var result = GoldbachConjecture.getPrimeSum(n);
// after
if (n <= 2 || n % 2 != 0) {
throw new IllegalArgumentException(
"Goldbach's conjecture requires an even integer greater than 2. Got: " + n);
}
var result = GoldbachConjecture.getPrimeSum(n); Defensive patterns
Strategy: validation
Validate before calling
if (number <= 2 || number % 2 != 0) {
throw new IllegalArgumentException("Number must be even and > 2. Got: " + number);
}
var result = GoldbachConjecture.getPrimeSum(number); Type guard
static boolean isGoldbachEligible(int n) {
return n > 2 && n % 2 == 0;
} Try / catch
try {
var result = GoldbachConjecture.getPrimeSum(number);
} catch (IllegalArgumentException e) {
// number is odd or <= 2; not applicable
} Prevention
- Check number > 2 and number % 2 == 0 before calling.
- Reject or skip odd numbers and small values upstream.
- Document the even-and-greater-than-2 precondition in your wrapper.
When it happens
Trigger: Calling getPrimeSum(1), getPrimeSum(2) (boundary), getPrimeSum(3), getPrimeSum(5), or any odd/even-but-too-small number. Any caller passing an unvalidated integer triggers this if the value is odd or <= 2.
Common situations: Processing user-supplied numbers without parity/size checks. Math problem solvers that feed arbitrary integers. Educational code that tests edge cases with small or odd numbers.
Related errors
- Input value must be a positive integer. Input value: {number
- No valid prime sum found.
- Input 'n' is too big to give accurate result.
- k must be between 1 and the size of the array
- Array must be non-empty.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/baefffb462285095.
Report an issue: GitHub.