{"record":{"id":"ff796af2168a1bde","repo":"TheAlgorithms/Java","slug":"input-numbers-must-be-natural","errorCode":null,"errorMessage":"Input numbers must be natural!","messagePattern":"Input numbers must be natural!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/AmicableNumber.java","lineNumber":54,"sourceCode":"\n        Set<Pair<Integer, Integer>> result = new LinkedHashSet<>();\n\n        for (int i = from; i < to; i++) {\n            for (int j = i + 1; j <= to; j++) {\n                if (isAmicableNumber(i, j)) {\n                    result.add(Pair.of(i, j));\n                }\n            }\n        }\n        return result;\n    }\n\n    /**\n     * Checks whether 2 numbers are AmicableNumbers or not.\n     */\n    public static boolean isAmicableNumber(int a, int b) {\n        if (a <= 0 || b <= 0) {\n            throw new IllegalArgumentException(\"Input numbers must be natural!\");\n        }\n        return sumOfDividers(a, a) == b && sumOfDividers(b, b) == a;\n    }\n\n    /**\n     * Recursively calculates the sum of all dividers for a given number excluding the divider itself.\n     */\n    private static int sumOfDividers(int number, int divisor) {\n        if (divisor == 1) {\n            return 0;\n        } else if (number % --divisor == 0) {\n            return sumOfDividers(number, divisor) + divisor;\n        } else {\n            return sumOfDividers(number, divisor);\n        }\n    }\n}\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/AmicableNumber.java#L36-L72","documentation":"Thrown by AmicableNumber.isAmicableNumber when either 'a' or 'b' is <= 0. The amicable relation is a property of positive integers (it relies on sumOfDividers), so any non-positive operand is rejected before computing divisor sums.","triggerScenarios":"isAmicableNumber(0, 5); isAmicableNumber(220, -4); isAmicableNumber(0, 0); calling with a loop counter that started at 0.","commonSituations":"Looping i from 0 instead of 1; default-initialized int fields (0) passed before assignment; negative results from subtraction used as amicable candidates.","solutions":["Pass two positive integers: isAmicableNumber(220, 284).","Guard both operands: if (a > 0 && b > 0) ... else handle.","Start search loops at 1, not 0."],"exampleFix":"// before\nboolean am = AmicableNumber.isAmicableNumber(i, j);\n// after\nif (i <= 0 || j <= 0) continue;\nboolean am = AmicableNumber.isAmicableNumber(i, j);","handlingStrategy":"validation","validationCode":"if (a <= 0 || b <= 0) {\n    throw new IllegalArgumentException(\"both numbers must be natural\");\n}\nboolean am = AmicableNumber.isAmicableNumber(a, b);","typeGuard":"static boolean bothNatural(int a, int b) {\n    return a > 0 && b > 0;\n}","tryCatchPattern":"try {\n    boolean am = AmicableNumber.isAmicableNumber(a, b);\n} catch (IllegalArgumentException e) {\n    // one or both operands were non-positive\n}","preventionTips":["Start amicable search loops at 1.","Validate both operands from user input before the call.","Guard pair-generation so you never emit a 0 operand."],"tags":["validation","maths","precondition","positive-number","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}