TheAlgorithms/C-Sharp · error · ArgumentException
An automorphic number must always be positive.
Error message
An automorphic number must always be positive.
What it means
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.
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.
Example fix
// before bool isAuto = AutomorphicNumber.IsAutomorphic(value); // throws when value <= 0 // after bool isAuto = value >= 1 && AutomorphicNumber.IsAutomorphic(value);
Defensive patterns
Strategy: validation
Validate before calling
bool isAuto = number >= 1 && AutomorphicNumber.IsAutomorphic(number);
Try / catch
try
{
ok = AutomorphicNumber.IsAutomorphic(n);
}
catch (ArgumentException)
{
ok = false; // non-positive input is trivially not automorphic
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- cannot be negative
- The lower bound must be less than or equal to the upper…
- num ≥ k ≥ 0
- should be greater than zero
- Invalid parameter settings for Ascon Hash
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/fdb2f596cea7de56.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/AutomorphicNumber.cs:49
if (lowerBound > upperBound)
{
throw new ArgumentException($"The lower bound must be less than or equal to the upper bound.");
}
return Enumerable.Range(lowerBound, upperBound).Where(IsAutomorphic);
}
/// <summary>
/// Checks if a given natural number is automorphic or not.
/// </summary>
/// <param name="number">The number to check.</param>
/// <returns>True if the number is automorphic, false otherwise.</returns>
/// <exception cref="ArgumentException">If the number is non-positive.</exception>
public static bool IsAutomorphic(int number)
{
if (number < 1)
{
throw new ArgumentException($"An automorphic number must always be positive.");
}
BigInteger square = BigInteger.Pow(number, 2);
// Extract the last digits of both numbers
while (number > 0)
{
if (number % 10 != square % 10)
{
return false;
}
number /= 10;
square /= 10;
}
return true;
}View on GitHub (pinned to 96e2905cab)