TheAlgorithms/C-Sharp · error · ArgumentException

cannot be negative

Error message

{nameof(number)} cannot be negative

What it means

CalculateAliquotSum throws ArgumentException for negative inputs. The aliquot sum (sum of proper divisors) is defined for non-negative integers, and the divisor loop assumes number >= 0; the XML doc notes the valid interval (0; int.MaxValue).

Solutions

  1. Validate number >= 0 before calling and reject or clamp negatives in your own code.
  2. Decide domain semantics: 0 is allowed by the code (returns 0 per loop bounds), negatives are not — filter them out upstream.
  3. Wrap in try-catch for ArgumentException only as a last line of defense.

Example fix

// before
var sum = AliquotSumCalculator.CalculateAliquotSum(n);
// after
var sum = n >= 0 ? AliquotSumCalculator.CalculateAliquotSum(n) : throw new ArgumentOutOfRangeException(nameof(n));
Defensive patterns

Strategy: validation

Validate before calling

if (number < 0) throw new ArgumentOutOfRangeException(nameof(number), "aliquot sum requires a non-negative integer");

Type guard

static bool IsValidAliquotInput(int n) => n >= 0;

Try / catch

try { sum = AliquotSumCalculator.CalculateAliquotSum(n); }
catch (ArgumentException ex) { /* reject or clamp negative input */ }

Prevention

When it happens

Trigger: Calling AliquotSumCalculator.CalculateAliquotSum with a negative int, e.g. from user input or subtraction (a - b) that went below zero.

Common situations: Perfect/abundant-number classification over datasets containing negatives; sign mistakes when computing inputs; unvalidated user or config values.

Related errors


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

Appendix: source

Thrown at Algorithms/Numeric/AliquotSumCalculator.cs:21

/// <summary>
///     In number theory, the aliquot sum s(n) of a positive integer n is the sum of all proper divisors
///     of n, that is, all divisors of n other than n itself. For example, the proper divisors of 15
///     (that is, the positive divisors of 15 that are not equal to 15) are 1, 3 and 5, so the aliquot
///     sum of 15 is 9 i.e. (1 + 3 + 5). Wikipedia: https://en.wikipedia.org/wiki/Aliquot_sum.
/// </summary>
public static class AliquotSumCalculator
{
    /// <summary>
    ///     Finds the aliquot sum of an integer number.
    /// </summary>
    /// <param name="number">Positive number.</param>
    /// <returns>The Aliquot Sum.</returns>
    /// <exception cref="ArgumentException">Error number is not on interval (0.0; int.MaxValue).</exception>
    public static int CalculateAliquotSum(int number)
    {
        if (number < 0)
        {
            throw new ArgumentException($"{nameof(number)} cannot be negative");
        }

        var sum = 0;
        for (int i = 1, limit = number / 2; i <= limit; ++i)
        {
            if (number % i == 0)
            {
                sum += i;
            }
        }

        return sum;
    }
}

View on GitHub (pinned to 96e2905cab)