TheAlgorithms/C-Sharp · error · ArgumentException

The value for some n_i is smaller than or equal to 1.

Error message

The value {listOfNs.First(x => x <= 1)} for some n_i is smaller than or equal to 1.

What it means

The CRT solver requires every modulus n_i to be greater than 1, because modular arithmetic modulo 0 or 1 is degenerate (and modulo a negative is ill-defined here). CheckRequirements throws ArgumentException naming the first offending modulus via the interpolated message.

Solutions

  1. Filter or validate listOfNs so every modulus is >= 2 before calling Compute.
  2. Fix the source of the zero/one modulus (unset defaults, bad input parsing).
  3. Surface a clear validation message to the user when collecting the congruence system.

Example fix

// before
ChineseRemainderTheorem.Compute(new List<long> { 2 }, new List<long> { 1, 3 }); // throws

// after
if (listOfNs.All(n => n > 1))
{
    ChineseRemainderTheorem.Compute(listOfAs, listOfNs);
}
Defensive patterns

Strategy: validation

Validate before calling

if (listOfNs.Any(n => n <= 1))
{
    throw new ArgumentException("All moduli must be greater than 1.");
}
ChineseRemainderTheorem.Compute(listOfAs, listOfNs);

Type guard

static bool HasValidModuli(List<long> ns) => ns != null && ns.All(n => n > 1);

Try / catch

try
{
    var result = ChineseRemainderTheorem.Compute(listOfAs, listOfNs);
}
catch (ArgumentException ex) when (ex.Message.Contains("smaller than or equal to 1"))
{
    logger.LogError("Invalid modulus in CRT input: {Msg}", ex.Message);
    throw;
}

Prevention

When it happens

Trigger: Calling Compute where any entry of listOfNs is <= 1 (0, 1, or negative), e.g. moduli containing 1 from a degenerate pairwise-coprime requirement or 0 from an unset default.

Common situations: Default-initialized array elements of 0 included as moduli; a user entering n=1 thinking it is harmless; off-by-one parsing that dropped a digit.

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/53b633ac62c86e78. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/ModularArithmetic/ChineseRemainderTheorem.cs:129

        return result;
    }

    /// <summary>
    /// Checks the requirements for the algorithm and throws an ArgumentException if they are not being met.
    /// </summary>
    /// <param name="listOfAs">An ordered list of a_0, a_1, ..., a_k.</param>
    /// <param name="listOfNs">An ordered list of n_0, n_1, ..., n_k.</param>
    /// <exception cref="ArgumentException">If any of the requirements is not fulfilled.</exception>
    private static void CheckRequirements(List<long> listOfAs, List<long> listOfNs)
    {
        if (listOfAs == null || listOfNs == null || listOfAs.Count != listOfNs.Count)
        {
            throw new ArgumentException("The parameters 'listOfAs' and 'listOfNs' must not be null and have to be of equal length!");
        }

        if (listOfNs.Any(x => x <= 1))
        {
            throw new ArgumentException($"The value {listOfNs.First(x => x <= 1)} for some n_i is smaller than or equal to 1.");
        }

        if (listOfAs.Any(x => x < 0))
        {
            throw new ArgumentException($"The value {listOfAs.First(x => x < 0)} for some a_i is smaller than 0.");
        }

        // Check if all pairs of (n_i, n_j) are coprime:
        for (var i = 0; i < listOfNs.Count; i++)
        {
            for (var j = i + 1; j < listOfNs.Count; j++)
            {
                long gcd;
                if ((gcd = ExtendedEuclideanAlgorithm.Compute(listOfNs[i], listOfNs[j]).Gcd) != 1L)
                {
                    throw new ArgumentException($"The GCD of n_{i} = {listOfNs[i]} and n_{j} = {listOfNs[j]} equals {gcd} and thus these values aren't coprime.");
                }
            }

View on GitHub (pinned to 96e2905cab)