TheAlgorithms/C-Sharp · error · ArgumentException

cannot be negative

Error message

{nameof(number)} cannot be negative

What it means

IsPerfectNumber(number) sums the proper divisors of a non-negative integer and compares to the number itself. Negative numbers are outside the definition domain, so the library throws ArgumentException naming the parameter. Note 0 passes the guard but the divisor loop yields sum 0, so 0 is trivially treated as 'perfect' by this implementation.

Solutions

  1. Validate number >= 0 (ideally >= 1) before calling.
  2. Restrict range scans to positive integers.
  3. Use Math.Abs if sign is irrelevant, though mathematically only positive numbers are perfect.

Example fix

// before
foreach (var n in numbers) IsPerfectNumber(n); // may include negatives
// after
foreach (var n in numbers.Where(n => n > 0)) IsPerfectNumber(n);
Defensive patterns

Strategy: validation

Validate before calling

bool isPerfect = number > 0 && PerfectNumberChecker.IsPerfectNumber(number);

Type guard

static bool IsPositive(int v) => v > 0;

Try / catch

try { return PerfectNumberChecker.IsPerfectNumber(n); }
catch (ArgumentException) { return false; // negatives are never perfect }

Prevention

When it happens

Trigger: Calling PerfectNumberChecker.IsPerfectNumber with a negative int, e.g. IsPerfectNumber(-6).

Common situations: Scanning a signed integer range that crosses zero, or subtracting an offset that pushes values below 0.

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


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

Appendix: source

Thrown at Algorithms/Numeric/PerfectNumberChecker.cs:20

/// <summary>
///     In number theory, a perfect number is a positive integer that is equal to the sum of its positive
///     divisors, excluding the number itself.For instance, 6 has divisors 1, 2 and 3 (excluding
///     itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.
/// </summary>
public static class PerfectNumberChecker
{
    /// <summary>
    ///     Checks if a number is a perfect number or not.
    /// </summary>
    /// <param name="number">Number to check.</param>
    /// <returns>True if is a perfect number; False otherwise.</returns>
    /// <exception cref="ArgumentException">Error number is not on interval (0.0; int.MaxValue).</exception>
    public static bool IsPerfectNumber(int number)
    {
        if (number < 0)
        {
            throw new ArgumentException($"{nameof(number)} cannot be negative");
        }

        var sum = 0; /* sum of its positive divisors */
        for (var i = 1; i < number; ++i)
        {
            if (number % i == 0)
            {
                sum += i;
            }
        }

        return sum == number;
    }
}

View on GitHub (pinned to 96e2905cab)