{"record":{"id":"4439b36acc82fbda","repo":"TheAlgorithms/Java","slug":"input-must-be-non-negative-received-4439b3","errorCode":null,"errorMessage":"Input must be non-negative. Received: ","messagePattern":"Input must be non-negative\\. Received: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/PadovanSequence.java","lineNumber":26,"sourceCode":" * @see <a href=\"https://en.wikipedia.org/wiki/Padovan_sequence\">\n *     Wikipedia: Padovan Sequence</a>\n * @author Vraj Prajapati (@Rosander0)\n */\npublic final class PadovanSequence {\n\n    private PadovanSequence() {\n        // Utility class\n    }\n\n    /**\n     * Calculates the nth term of the Padovan Sequence.\n     *\n     * @param n the index of the sequence (must be non-negative)\n     * @return the nth term of the Padovan Sequence\n     */\n    public static long padovan(final int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Input must be non-negative. Received: \" + n);\n        }\n        if (n <= 2) {\n            return 1;\n        }\n        long a = 1;\n        long b = 1;\n        long c = 1;\n        long result = 0;\n        for (int i = 3; i <= n; i++) {\n            result = a + b;\n            a = b;\n            b = c;\n            c = result;\n        }\n        return result;\n    }\n}\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/PadovanSequence.java#L8-L44","documentation":"Thrown by PadovanSequence.padovan(int n) when n is negative. The Padovan sequence is defined for non-negative indices (P(0)=P(1)=P(2)=1, P(n)=P(n-2)+P(n-3)); a negative index has no defined value and would break the iterative generation loop. The guard fires before the base-case checks.","triggerScenarios":"Calling padovan(-1) or any padovan(n) where n < 0.","commonSituations":"Index computed from an expression like (n - k) that can go negative; user-supplied sequence index not sanitized; off-by-one in a loop that should exclude 0; deserialized query parameter that accepted negative values.","solutions":["Validate n >= 0 at the caller and reject/clamp before invoking padovan.","Fix the upstream arithmetic (loop bounds, subtractions) producing the negative index.","If your domain allows negative indices, define your own extension and remap before calling."],"exampleFix":"// before\nlong v = PadovanSequence.padovan(idx);\n\n// after\nif (idx < 0) {\n    throw new IllegalArgumentException(\"idx must be >= 0: \" + idx);\n}\nlong v = PadovanSequence.padovan(idx);","handlingStrategy":"validation","validationCode":"if (n < 0) {\n    throw new IllegalArgumentException(\"n must be >= 0: \" + n);\n}\nlong v = PadovanSequence.padovan(n);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate sequence indices for non-negativity before calling.","Audit index arithmetic (subtractions, loop bounds) for negative results.","Reject negative indices at the API boundary with a domain-specific error."],"tags":["math","sequence","invalid-argument","negative-index"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}