TheAlgorithms/C-Sharp · error · ArgumentException
The lower bound must be less than or equal to the upper…
Error message
The lower bound must be less than or equal to the upper bound.
What it means
GetAutomorphicNumbers(lowerBound, upperBound) generates automorphic numbers in an inclusive-style range via Enumerable.Range. The library requires the caller to pass a lowerBound that does not exceed upperBound; passing lowerBound > upperBound would produce an empty/nonsensical range, so it throws ArgumentException with this message. This is a deliberate input-contract check, not an internal failure.
Solutions
- Ensure lowerBound <= upperBound before calling; order the inputs with Math.Min/Math.Max if the source order is unknown.
- If an empty result is acceptable, skip the call when lowerBound > upperBound instead of invoking it.
- Note that Enumerable.Range(lowerBound, count) uses a count, so verify upperBound is intended as a count-like bound and the range is non-degenerate.
Example fix
// before
var result = AutomorphicNumber.GetAutomorphicNumbers(from, to); // from > to throws
// after
var lower = Math.Min(from, to);
var upper = Math.Max(from, to);
var result = lower <= upper
? AutomorphicNumber.GetAutomorphicNumbers(lower, upper)
: Enumerable.Empty<int>(); Defensive patterns
Strategy: validation
Validate before calling
if (lowerBound > upperBound)
throw new ArgumentException("lowerBound must be <= upperBound");
var result = AutomorphicNumber.GetAutomorphicNumbers(lowerBound, upperBound); Try / catch
try
{
var nums = AutomorphicNumber.GetAutomorphicNumbers(lower, upper);
}
catch (ArgumentException ex) when (ex.Message.Contains("lower bound"))
{
// handle swapped/invalid range
} Prevention
- Normalize ranges with Math.Min/Math.Max before calling.
- Validate user-supplied bounds at input time.
- Remember Enumerable.Range semantics (start, count) when choosing bounds.
When it happens
Trigger: Calling AutomorphicNumber.GetAutomorphicNumbers(a, b) where a > b, e.g. GetAutomorphicNumbers(50, 10) or a dynamically computed range where the bounds are swapped or computed in the wrong order.
Common situations: Passing user-supplied range inputs without ordering them first; variables accidentally swapped at the call site; off-by-one or reversed range logic when building ranges from min/max inputs.
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
- An automorphic number must always be positive.
- Invalid parameter settings for Ascon Hash
- Cash flows list cannot be empty
- cannot be negative
- num ≥ k ≥ 0
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/a77a3e0862aa01e2.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/AutomorphicNumber.cs:33
/// <returns>A list that contains all of the automorphic numbers between <paramref name="lowerBound"/> and <paramref name="upperBound"/> inclusive.</returns>
/// <exception cref="ArgumentException">If the <paramref name="lowerBound"/>
/// or <paramref name="upperBound"/> is not greater than zero
/// or <paramref name="upperBound"/>is lower than the <paramref name="lowerBound"/>.</exception>
public static IEnumerable<int> GetAutomorphicNumbers(int lowerBound, int upperBound)
{
if (lowerBound < 1)
{
throw new ArgumentException($"Lower bound must be greater than 0.");
}
if (upperBound < 1)
{
throw new ArgumentException($"Upper bound must be greater than 0.");
}
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.");
}
View on GitHub (pinned to 96e2905cab)