{"record":{"id":"afb9a74d2f0dac69","repo":"TheAlgorithms/Java","slug":"n-must-be-positive","errorCode":null,"errorMessage":"n must be positive.","messagePattern":"n must be positive\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/EulersFunction.java","lineNumber":19,"sourceCode":"package com.thealgorithms.maths;\n\n/**\n * Utility class for computing\n * <a href=\"https://en.wikipedia.org/wiki/Euler%27s_totient_function\">Euler's totient function</a>.\n */\npublic final class EulersFunction {\n    private EulersFunction() {\n    }\n\n    /**\n     * Validates that the input is a positive integer.\n     *\n     * @param n the input number to validate\n     * @throws IllegalArgumentException if {@code n} is non-positive\n     */\n    private static void checkInput(int n) {\n        if (n <= 0) {\n            throw new IllegalArgumentException(\"n must be positive.\");\n        }\n    }\n\n    /**\n     * Computes the value of Euler's totient function for a given input.\n     * This function has a time complexity of O(sqrt(n)).\n     *\n     * @param n the input number\n     * @return the value of Euler's totient function for the given input\n     * @throws IllegalArgumentException if {@code n} is non-positive\n     */\n    public static int getEuler(int n) {\n        checkInput(n);\n        int result = n;\n        for (int i = 2; i * i <= n; i++) {\n            if (n % i == 0) {\n                while (n % i == 0) {\n                    n /= i;","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/EulersFunction.java#L1-L37","documentation":"Thrown by EulersFunction.checkInput (invoked by getEuler) when n <= 0. Euler's totient function phi(n) counts integers up to n coprime to n and is defined for positive integers only. The private checkInput helper centralises this precondition so getEuler can assume n >= 1 in its O(sqrt(n)) factorisation loop.","triggerScenarios":"Calling getEuler(0), getEuler(-10), or passing an unvalidated integer. The check runs before the totient computation begins.","commonSituations":"User input parsed to 0 or negative; loop starting at 0; default int value of 0 passed inadvertently; n derived from a subtraction that underflowed.","solutions":["Pass a positive integer (>= 1) such as getEuler(36).","Validate at the caller: if (n <= 0) reject before calling.","Clamp derived values with Math.max(1, n)."],"exampleFix":"// before\nint phi = EulersFunction.getEuler(0);\n\n// after\nint phi = EulersFunction.getEuler(36);","handlingStrategy":"validation","validationCode":"if (n <= 0) {\n    throw new IllegalArgumentException(\"Euler totient requires n >= 1\");\n}\nEulersFunction.getEuler(n);","typeGuard":"static boolean isPositive(int n) { return n > 0; }","tryCatchPattern":null,"preventionTips":["Reject 0 and negatives at the input boundary.","Clamp derived values with Math.max(1, n).","Validate parsed integers before calling."],"tags":["validation","number-theory","precondition","domain-error"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}