{"record":{"id":"e38478c6cd0e0e6e","repo":"TheAlgorithms/Java","slug":"input-must-be-a-positive-integer-provided","errorCode":null,"errorMessage":"Input must be a positive integer. Provided: ","messagePattern":"Input must be a positive integer\\. Provided: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/LucasSeries.java","lineNumber":29,"sourceCode":" * @see <a href=\"https://en.wikipedia.org/wiki/Lucas_number\">Lucas Number</a>\n * @author TheAlgorithms Contributors\n */\npublic final class LucasSeries {\n    private LucasSeries() {\n    }\n\n    /**\n     * Calculate the nth Lucas number using recursion.\n     * Time Complexity: O(2^n) - exponential due to recursive calls\n     * Space Complexity: O(n) - recursion depth\n     *\n     * @param n the position in the Lucas sequence (1-indexed, must be positive)\n     * @return the nth Lucas number\n     * @throws IllegalArgumentException if n is less than 1\n     */\n    public static int lucasSeries(int n) {\n        if (n < 1) {\n            throw new IllegalArgumentException(\"Input must be a positive integer. Provided: \" + n);\n        }\n        if (n == 1) {\n            return 2;\n        }\n        if (n == 2) {\n            return 1;\n        }\n        return lucasSeries(n - 1) + lucasSeries(n - 2);\n    }\n\n    /**\n     * Calculate the nth Lucas number using iteration.\n     * Time Complexity: O(n) - single loop through n iterations\n     * Space Complexity: O(1) - constant space usage\n     *\n     * @param n the position in the Lucas sequence (1-indexed, must be positive)\n     * @return the nth Lucas number\n     * @throws IllegalArgumentException if n is less than 1","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/LucasSeries.java#L11-L47","documentation":"Thrown by the recursive lucasSeries(int n) when n < 1. The Lucas sequence is 1-indexed with L(1)=2, L(2)=1, L(n)=L(n-1)+L(n-2). Positions below 1 have no defined value, and the recursion would miss its base cases.","triggerScenarios":"Calling lucasSeries(0) or lucasSeries(-3). Hit when n is zero-based but the API expects 1-based indexing, or when a computed position underflows.","commonSituations":"Off-by-one: the caller uses 0-based indexing (common in array contexts) but LucasSeries expects 1-based. Passing a loop variable that starts at 0. Subtracting from n in a recursive or iterative caller that can reach 0.","solutions":["Ensure n >= 1 before calling lucasSeries; adjust 0-based indices by adding 1","Prefer the iterative lucasSeriesIteration for any production use","Add an explicit bounds check at the call site with a clear error message"],"exampleFix":"// before\nint result = LucasSeries.lucasSeries(arrayIndex);\n\n// after\nint result = LucasSeries.lucasSeries(arrayIndex + 1);  // convert 0-based to 1-based","handlingStrategy":"validation","validationCode":"if (n < 1) {\n    throw new IllegalArgumentException(\"Position must be >= 1 (1-indexed): \" + n);\n}\nint result = LucasSeries.lucasSeries(n);","typeGuard":"static boolean isValidLucasPosition(int n) {\n    return n >= 1;\n}","tryCatchPattern":null,"preventionTips":["The Lucas series API is 1-indexed: L(1)=2, L(2)=1 — adjust 0-based indices by adding 1","Prefer the iterative lucasSeriesIteration for production code","Add a clear comment at the call site noting the 1-based indexing convention"],"tags":["math","validation","illegal-argument","recursion"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}