{"record":{"id":"6ed7bd33abc21732","repo":"TheAlgorithms/Java","slug":"additivepersistence-does-not-accept-negative-val","errorCode":null,"errorMessage":"additivePersistence() does not accept negative values","messagePattern":"additivePersistence\\(\\) does not accept negative values","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/NumberPersistence.java","lineNumber":63,"sourceCode":"            num = product;\n            steps++;\n        }\n        return steps;\n    }\n\n    /**\n     * Calculates the additive persistence of a given number.\n     *\n     * <p>Additive persistence is the number of steps required to reduce a number to a single digit\n     * by summing its digits repeatedly.\n     *\n     * @param num the number to calculate persistence for; must be non-negative\n     * @return the additive persistence of the number\n     * @throws IllegalArgumentException if the input number is negative\n     */\n    public static int additivePersistence(int num) {\n        if (num < 0) {\n            throw new IllegalArgumentException(\"additivePersistence() does not accept negative values\");\n        }\n\n        int steps = 0;\n        while (num >= 10) {\n            int sum = 0;\n            int temp = num;\n            while (temp > 0) {\n                sum += temp % 10;\n                temp /= 10;\n            }\n            num = sum;\n            steps++;\n        }\n        return steps;\n    }\n}\n","sourceCodeStart":45,"sourceCodeEnd":80,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/NumberPersistence.java#L45-L80","documentation":"Thrown by NumberPersistence.additivePersistence when num is negative. Additive persistence repeatedly sums the decimal digits of a number until a single digit remains; the digit-summing loop assumes a non-negative input. The guard rejects negatives before the loop starts.","triggerScenarios":"Calling additivePersistence(-1) or any additivePersistence(num) where num < 0.","commonSituations":"Parsed integer from input where the sign was not validated; result of a subtraction passed in directly; numeric field deserialized from JSON without a min-constraint.","solutions":["Pass Math.abs(num) when the magnitude is what matters.","Validate num >= 0 at the input boundary and surface a domain error otherwise.","Adjust upstream computation so non-negative values only reach this call."],"exampleFix":"// before\nint p = NumberPersistence.additivePersistence(value);\n\n// after\nint p = NumberPersistence.additivePersistence(Math.abs(value));","handlingStrategy":"validation","validationCode":"int input = Math.abs(num);\nint p = NumberPersistence.additivePersistence(input);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize sign at the input boundary when the magnitude is what matters.","Validate parsed integers for sign before feeding to persistence methods.","Apply Math.abs consistently to both persistence methods to avoid asymmetric behavior."],"tags":["math","persistence","invalid-argument","negative-value"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}