{"record":{"id":"0459ce45264d08ab","repo":"TheAlgorithms/Java","slug":"input-must-be-non-negative-0459ce","errorCode":null,"errorMessage":"Input must be non-negative!","messagePattern":"Input must be non-negative!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/PerrinNumber.java","lineNumber":30,"sourceCode":" * @see <a href=\"https://en.wikipedia.org/wiki/Perrin_number\">\n *     Wikipedia: Perrin Number</a>\n * @see PadovanSequence\n */\npublic final class PerrinNumber {\n\n    private PerrinNumber() {\n        // Utility class\n    }\n\n    /**\n     * Calculates the nth term of the Perrin Sequence.\n     *\n     * @param n the index of the sequence (must be non-negative)\n     * @return the nth term of the Perrin Sequence\n     */\n    public static long perrin(final int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Input must be non-negative!\");\n        }\n        if (n == 0) {\n            return 3;\n        }\n        if (n == 1) {\n            return 0;\n        }\n        if (n == 2) {\n            return 2;\n        }\n        long a = 3;\n        long b = 0;\n        long c = 2;\n        long result = 0;\n        for (int i = 3; i <= n; i++) {\n            result = a + b;\n            a = b;\n            b = c;","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/PerrinNumber.java#L12-L48","documentation":"Thrown by PerrinNumber.perrin(int n) when n is negative. The Perrin sequence is defined for non-negative indices (P(0)=3, P(1)=0, P(2)=2, P(n)=P(n-2)+P(n-3)); a negative index has no defined value and would break the iterative loop. The guard fires before the base-case checks.","triggerScenarios":"Calling perrin(-1) or any perrin(n) where n < 0.","commonSituations":"Index computed from an arithmetic expression that can go negative; user-supplied index not validated; off-by-one in a loop bound; deserialized parameter that accepted negative integers.","solutions":["Validate n >= 0 at the caller and reject or clamp before calling perrin.","Fix the upstream arithmetic (loop bounds, subtractions) producing the negative index.","If negative indices are meaningful in your domain, define your own mapping before calling."],"exampleFix":"// before\nlong v = PerrinNumber.perrin(idx);\n\n// after\nif (idx < 0) {\n    throw new IllegalArgumentException(\"idx must be >= 0: \" + idx);\n}\nlong v = PerrinNumber.perrin(idx);","handlingStrategy":"validation","validationCode":"if (n < 0) {\n    throw new IllegalArgumentException(\"n must be >= 0: \" + n);\n}\nlong v = PerrinNumber.perrin(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"}