{"record":{"id":"10b1884b55550bd5","repo":"TheAlgorithms/Java","slug":"sylvester-does-not-accept-negative-numbers-or-ze","errorCode":null,"errorMessage":"sylvester() does not accept negative numbers or zero.","messagePattern":"sylvester\\(\\) does not accept negative numbers or zero\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/recursion/SylvesterSequence.java","lineNumber":40,"sourceCode":"    private SylvesterSequence() {\n    }\n\n    /**\n     * Calculates the nth number in Sylvester's sequence.\n     *\n     * <p>The sequence is defined recursively, with the first term being 2:\n     * <pre>\n     * a(1) = 2\n     * a(n) = a(n-1) * (a(n-1) - 1) + 1 for n > 1\n     * </pre>\n     *\n     * @param n the position in the sequence (must be greater than 0)\n     * @return the nth number in Sylvester's sequence\n     * @throws IllegalArgumentException if n is less than or equal to 0\n     */\n    public static BigInteger sylvester(int n) {\n        if (n <= 0) {\n            throw new IllegalArgumentException(\"sylvester() does not accept negative numbers or zero.\");\n        }\n        if (n == 1) {\n            return BigInteger.valueOf(2);\n        } else {\n            BigInteger prev = sylvester(n - 1);\n            // Sylvester sequence formula: a(n) = a(n-1) * (a(n-1) - 1) + 1\n            return prev.multiply(prev.subtract(BigInteger.ONE)).add(BigInteger.ONE);\n        }\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/recursion/SylvesterSequence.java#L22-L51","documentation":"Thrown by sylvester(int n) when the requested position n is less than or equal to 0. Sylvester's sequence is defined for positive indices starting at 1, so any non-positive index is out of domain. The method uses this guard to prevent descending into undefined recursion.","triggerScenarios":"Calling sylvester(0), sylvester(-1), or any sylvester(n) where n <= 0. The internal recursion itself never triggers this because it only recurses with n-1 down to the n == 1 base case.","commonSituations":"Passing a user-supplied or computed index without clamping; off-by-one when converting from a zero-based UI selector to the 1-based sequence position; calling with a default/uninitialized int value of 0.","solutions":["Validate that n >= 1 before calling sylvester(), and surface a meaningful error to the user otherwise.","If your input is zero-based, pass n + 1 to align with the 1-based indexing.","Coerce negative or zero inputs to the nearest valid index (1) only if that matches your business logic."],"exampleFix":"// before\nBigInteger v = SylvesterSequence.sylvester(userIndex);\n\n// after\nif (userIndex < 1) throw new IllegalArgumentException(\"index must be >= 1, got \" + userIndex);\nBigInteger v = SylvesterSequence.sylvester(userIndex);","handlingStrategy":"validation","validationCode":"if (n < 1) throw new IllegalArgumentException(\"Sylvester index must be >= 1, got \" + n);","typeGuard":"// n is a primitive int; treat as validated value object if wrapping is acceptable\npublic static boolean isValidSylvesterIndex(int n) { return n >= 1; }","tryCatchPattern":null,"preventionTips":["Treat the sequence as 1-based at every API boundary and convert explicitly.","Never pass through user-supplied ints without a lower-bound check.","Unit-test boundary values 0, 1, and 2 to lock the contract."],"tags":["math","recursion","input-validation","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}