{"record":{"id":"baefffb462285095","repo":"TheAlgorithms/Java","slug":"number-must-be-even-and-greater-than-2","errorCode":null,"errorMessage":"Number must be even and greater than 2.","messagePattern":"Number must be even and greater than 2\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/GoldbachConjecture.java","lineNumber":20,"sourceCode":"\nimport static com.thealgorithms.maths.Prime.PrimeCheck.isPrime;\n\n/**\n * This is a representation of the unsolved problem of Goldbach's Projection, according to which every\n * even natural number greater than 2 can be written as the sum of 2 prime numbers\n * More info: https://en.wikipedia.org/wiki/Goldbach%27s_conjecture\n * @author Vasilis Sarantidis (https://github.com/BILLSARAN)\n */\n\npublic final class GoldbachConjecture {\n    private GoldbachConjecture() {\n    }\n    public record Result(int number1, int number2) {\n    }\n\n    public static Result getPrimeSum(int number) {\n        if (number <= 2 || number % 2 != 0) {\n            throw new IllegalArgumentException(\"Number must be even and greater than 2.\");\n        }\n\n        for (int i = 0; i <= number / 2; i++) {\n            if (isPrime(i) && isPrime(number - i)) {\n                return new Result(i, number - i);\n            }\n        }\n        throw new IllegalStateException(\"No valid prime sum found.\"); // Should not occur\n    }\n}\n","sourceCodeStart":2,"sourceCodeEnd":31,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java#L2-L31","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar result = GoldbachConjecture.getPrimeSum(n);\n\n// after\nif (n <= 2 || n % 2 != 0) {\n    throw new IllegalArgumentException(\n        \"Goldbach's conjecture requires an even integer greater than 2. Got: \" + n);\n}\nvar result = GoldbachConjecture.getPrimeSum(n);","handlingStrategy":"validation","validationCode":"if (number <= 2 || number % 2 != 0) {\n    throw new IllegalArgumentException(\"Number must be even and > 2. Got: \" + number);\n}\nvar result = GoldbachConjecture.getPrimeSum(number);","typeGuard":"static boolean isGoldbachEligible(int n) {\n    return n > 2 && n % 2 == 0;\n}","tryCatchPattern":"try {\n    var result = GoldbachConjecture.getPrimeSum(number);\n} catch (IllegalArgumentException e) {\n    // number is odd or <= 2; not applicable\n}","preventionTips":["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."],"tags":["math","goldbach","prime","argument-validation","parity"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}