{"record":{"id":"1a53d0bbfa3849ba","repo":"TheAlgorithms/Java","slug":"number-must-be-greater-than-zero-1a53d0","errorCode":null,"errorMessage":"Number must be greater than zero.","messagePattern":"Number must be greater than zero\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/Prime/SquareFreeInteger.java","lineNumber":32,"sourceCode":"import java.util.HashSet;\nimport java.util.List;\n\npublic final class SquareFreeInteger {\n    private SquareFreeInteger() {\n    }\n    /**\n     * This method returns whether an integer is square free\n     *\n     * @param number Integer value which is to be checked\n     * @return false when number has repeated prime factors\n     *         true when number has non repeated prime factors\n     * @throws IllegalArgumentException when number is negative or zero\n     */\n    public static boolean isSquareFreeInteger(int number) {\n\n        if (number <= 0) {\n            // throw exception when number is less than or is zero\n            throw new IllegalArgumentException(\"Number must be greater than zero.\");\n        }\n\n        // Store prime factors of number which is passed as argument\n        // in a list\n        List<Integer> primeFactorsList = PrimeFactorization.pfactors(number);\n\n        // Create set from list of prime factors of integer number\n        // if size of list and set is equal then the argument passed to this method is square free\n        // if size of list and set is not equal then the argument passed to this method is not\n        // square free\n        return primeFactorsList.size() == new HashSet<>(primeFactorsList).size();\n    }\n}\n","sourceCodeStart":14,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/Prime/SquareFreeInteger.java#L14-L46","documentation":"Thrown by SquareFreeInteger.isSquareFreeInteger(int number) when number <= 0. Square-free integers are defined for positive integers (no prime squared divides them), so zero and negatives are out of domain. The guard fires before delegating to PrimeFactorization.pfactors and comparing list/set sizes.","triggerScenarios":"Calling isSquareFreeInteger(0), isSquareFreeInteger(-6), or any isSquareFreeInteger(number) where number <= 0.","commonSituations":"User-supplied integer not validated for positivity; arithmetic expression crossing zero; loop bound off-by-one; deserialized field accepting non-positive values; calling on a difference that can be zero or negative.","solutions":["Validate number > 0 at the caller and reject or clamp before calling isSquareFreeInteger.","Fix upstream arithmetic producing non-positive values.","Define domain-specific semantics for 0/negatives (typically 'not square-free') before calling."],"exampleFix":"// before\nboolean sf = SquareFreeInteger.isSquareFreeInteger(n);\n\n// after\nif (n <= 0) {\n    throw new IllegalArgumentException(\"n must be > 0: \" + n);\n}\nboolean sf = SquareFreeInteger.isSquareFreeInteger(n);","handlingStrategy":"validation","validationCode":"if (number <= 0) {\n    throw new IllegalArgumentException(\"number must be > 0: \" + number);\n}\nboolean sf = SquareFreeInteger.isSquareFreeInteger(number);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate positive-integer inputs at the boundary before calling number-theory functions.","Audit arithmetic that produces inputs for non-positive results.","Treat 0 and negatives as domain errors for square-free checks."],"tags":["math","number-theory","invalid-argument","non-positive"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}