TheAlgorithms/C-Sharp · error · ArgumentException

Lower bound must be greater than 0.

Error message

Lower bound must be greater than 0.

What it means

GetAutomorphicNumbers throws ArgumentException when lowerBound < 1, because automorphic numbers start at 1 and the generator expects a positive range start. Documented via the XML exception note that bounds must be greater than zero.

Solutions

  1. Pass lowerBound >= 1 (use 1 to scan from the start of the sequence).
  2. Clamp: lowerBound = Math.Max(1, lowerBound) before the call.
  3. Validate range inputs at the UI/config boundary.

Example fix

// before
var nums = AutomorphicNumber.GetAutomorphicNumbers(0, 100);
// after
var nums = AutomorphicNumber.GetAutomorphicNumbers(Math.Max(1, lower), 100);
Defensive patterns

Strategy: validation

Validate before calling

if (lowerBound < 1) throw new ArgumentOutOfRangeException(nameof(lowerBound), "must be >= 1");

Type guard

static bool IsValidRangeStart(int lower) => lower >= 1;

Try / catch

try { seq = AutomorphicNumber.GetAutomorphicNumbers(lo, hi); }
catch (ArgumentException ex) when (ex.Message.Contains("Lower bound")) { /* clamp lo to 1 and retry */ }

Prevention

When it happens

Trigger: Calling GetAutomorphicNumbers(lowerBound, upperBound) with lowerBound of 0 or negative, e.g. GetAutomorphicNumbers(0, 100).

Common situations: Using 0 as a natural 'start from beginning' value; ranges derived from user input or config defaults of 0; loop variables starting at zero passed straight through.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/bbae8ac0d8db061e. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/AutomorphicNumber.cs:23

/// if the square of the number will contain the number itself at the end.
/// </summary>
public static class AutomorphicNumber
{
    /// <summary>
    /// Generates a list of automorphic numbers that are between <paramref name="lowerBound"/> and <paramref name="upperBound"/>
    /// inclusive.
    /// </summary>
    /// <param name="lowerBound">The lower bound of the list.</param>
    /// <param name="upperBound">The upper bound of the list.</param>
    /// <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>

View on GitHub (pinned to 96e2905cab)