{"record":{"id":"fdb2f596cea7de56","repo":"TheAlgorithms/C-Sharp","slug":"an-automorphic-number-must-always-be-positive","errorCode":null,"errorMessage":"An automorphic number must always be positive.","messagePattern":"An automorphic number must always be positive\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/AutomorphicNumber.cs","lineNumber":49,"sourceCode":"        if (lowerBound > upperBound)\n        {\n            throw new ArgumentException($\"The lower bound must be less than or equal to the upper bound.\");\n        }\n\n        return Enumerable.Range(lowerBound, upperBound).Where(IsAutomorphic);\n    }\n\n    /// <summary>\n    /// Checks if a given natural number is automorphic or not.\n    /// </summary>\n    /// <param name=\"number\">The number to check.</param>\n    /// <returns>True if the number is automorphic, false otherwise.</returns>\n    /// <exception cref=\"ArgumentException\">If the number is non-positive.</exception>\n    public static bool IsAutomorphic(int number)\n    {\n        if (number < 1)\n        {\n            throw new ArgumentException($\"An automorphic number must always be positive.\");\n        }\n\n        BigInteger square = BigInteger.Pow(number, 2);\n\n        // Extract the last digits of both numbers\n        while (number > 0)\n        {\n            if (number % 10 != square % 10)\n            {\n                return false;\n            }\n\n            number /= 10;\n            square /= 10;\n        }\n\n        return true;\n    }","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/AutomorphicNumber.cs#L31-L67","documentation":"IsAutomorphic(int number) checks whether a number's square ends in the number's own digits. Automorphism is defined only for positive integers, so any input < 1 (zero or negative) throws ArgumentException. The library rejects non-positive values up front instead of returning false, because the mathematical property is meaningless for them.","triggerScenarios":"Calling AutomorphicNumber.IsAutomorphic(0) or IsAutomorphic(-5); passing a value from a computation that can yield zero (e.g. a subtraction or an uninitialized default int).","commonSituations":"Filtering a list that contains 0 or negatives (e.g. list.Where(IsAutomorphic) over an unfiltered dataset); feeding zero from a default-initialized variable; porting code that expected a false return for non-positives.","solutions":["Check number >= 1 before calling IsAutomorphic and skip or handle non-positive values yourself.","Filter the input sequence to positive values before applying the predicate, e.g. nums.Where(n => n > 0 && IsAutomorphic(n)).","If zero/negative input is expected in your domain, wrap the call in try/catch or use your own automorphic check that returns false instead of throwing."],"exampleFix":"// before\nbool isAuto = AutomorphicNumber.IsAutomorphic(value); // throws when value <= 0\n// after\nbool isAuto = value >= 1 && AutomorphicNumber.IsAutomorphic(value);","handlingStrategy":"validation","validationCode":"bool isAuto = number >= 1 && AutomorphicNumber.IsAutomorphic(number);","typeGuard":null,"tryCatchPattern":"try\n{\n    ok = AutomorphicNumber.IsAutomorphic(n);\n}\ncatch (ArgumentException)\n{\n    ok = false; // non-positive input is trivially not automorphic\n}","preventionTips":["Short-circuit with n >= 1 before invoking the predicate.","Filter sequences to positive values before applying IsAutomorphic.","Initialize counters/accumulators to 1, not 0, when they will feed this check."],"tags":["argument-validation","math","csharp","algorithms"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}