{"record":{"id":"0666a6e92804fe9a","repo":"TheAlgorithms/Java","slug":"multiplicativepersistence-does-not-accept-negati","errorCode":null,"errorMessage":"multiplicativePersistence() does not accept negative values","messagePattern":"multiplicativePersistence\\(\\) does not accept negative values","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/NumberPersistence.java","lineNumber":34,"sourceCode":"public final class NumberPersistence {\n\n    // Private constructor to prevent instantiation\n    private NumberPersistence() {\n    }\n\n    /**\n     * Calculates the multiplicative persistence of a given number.\n     *\n     * <p>Multiplicative persistence is the number of steps required to reduce a number to a single digit\n     * by multiplying its digits repeatedly.\n     *\n     * @param num the number to calculate persistence for; must be non-negative\n     * @return the multiplicative persistence of the number\n     * @throws IllegalArgumentException if the input number is negative\n     */\n    public static int multiplicativePersistence(int num) {\n        if (num < 0) {\n            throw new IllegalArgumentException(\"multiplicativePersistence() does not accept negative values\");\n        }\n\n        int steps = 0;\n        while (num >= 10) {\n            int product = 1;\n            int temp = num;\n            while (temp > 0) {\n                product *= temp % 10;\n                temp /= 10;\n            }\n            num = product;\n            steps++;\n        }\n        return steps;\n    }\n\n    /**\n     * Calculates the additive persistence of a given number.","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/NumberPersistence.java#L16-L52","documentation":"Thrown by NumberPersistence.multiplicativePersistence when num is negative. Persistence is defined for non-negative integers (it repeatedly multiplies decimal digits), and the digit-extraction loop relies on num being non-negative. The guard rejects negatives before entering the loop.","triggerScenarios":"Calling multiplicativePersistence(-1) or any multiplicativePersistence(num) where num < 0.","commonSituations":"Caller passes user input or parsed text without sign normalization; absolute-value transformation missing in a pipeline that processes differences (which can be negative); a subtraction result fed directly into the method.","solutions":["Pass Math.abs(num) if the persistence of the magnitude is acceptable for your use case.","Validate input >= 0 at the boundary (form/parser) and reject negatives with a domain-specific error.","Fix upstream logic so only non-negative values reach this call."],"exampleFix":"// before\nint p = NumberPersistence.multiplicativePersistence(delta);\n\n// after\nint p = NumberPersistence.multiplicativePersistence(Math.abs(delta));","handlingStrategy":"validation","validationCode":"int input = Math.abs(num);\nint p = NumberPersistence.multiplicativePersistence(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.","Document at the call site whether negatives are errors or to be abs()-ed."],"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"}