TheAlgorithms/C-Sharp · error · ArgumentException

cannot be negative

Error message

{nameof(number)} cannot be negative

What it means

IsKeithNumber(number) determines whether a non-negative integer is a Keith (repfigit) number by iterating digit-sequence sums. Negative numbers are outside the definition domain, so the library throws ArgumentException with the parameter name interpolated into the message. Zero is allowed by this guard.

Solutions

  1. Check number >= 0 before calling IsKeithNumber.
  2. Use Math.Abs only if the sign is genuinely irrelevant to your use case.
  3. Filter input collections to non-negative values prior to checking.

Example fix

// before
bool isKeith = KeithNumberChecker.IsKeithNumber(userValue);
// after
if (userValue < 0) throw new ArgumentOutOfRangeException(nameof(userValue));
bool isKeith = KeithNumberChecker.IsKeithNumber(userValue);
Defensive patterns

Strategy: validation

Validate before calling

bool isKeith = number >= 0 && KeithNumberChecker.IsKeithNumber(number);

Type guard

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

Try / catch

try { return KeithNumberChecker.IsKeithNumber(n); }
catch (ArgumentException) { return false; // negatives are never Keith numbers }

Prevention

When it happens

Trigger: Calling KeithNumberChecker.IsKeithNumber with any negative int, e.g. IsKeithNumber(-14).

Common situations: Subtracting offsets from parsed user input, or feeding signed values from a range scan without a lower bound check.

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/55c00686e4e0368f. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/KeithNumberChecker.cs:19

namespace Algorithms.Numeric;

/// <summary>
///  In number theory, a Keith number or repfigit number is a natural number n in a given number base b with k digits such that
///  when a sequence is created such that the first k terms are the k digits of n and each subsequent term is the sum of the
///  previous k terms, n is part of the sequence.
/// </summary>
public static class KeithNumberChecker
{
    /// <summary>
    ///     Checks if a number is a Keith number or not.
    /// </summary>
    /// <param name="number">Number to check.</param>
    /// <returns>True if it is a Keith number; False otherwise.</returns>
    public static bool IsKeithNumber(int number)
    {
        if (number < 0)
        {
            throw new ArgumentException($"{nameof(number)} cannot be negative");
        }

        var tempNumber = number;

        var stringNumber = number.ToString();

        var digitsInNumber = stringNumber.Length;

        /* storing the terms of the series */
        var termsArray = new int[number];

        for (var i = digitsInNumber - 1; i >= 0; i--)
        {
            termsArray[i] = tempNumber % 10;
            tempNumber /= 10;
        }

        var sum = 0;

View on GitHub (pinned to 96e2905cab)