TheAlgorithms/C-Sharp · error · ArgumentException

The parameters 'listOfAs' and 'listOfNs' must not be null…

Error message

The parameters 'listOfAs' and 'listOfNs' must not be null and have to be of equal length!

What it means

ChineseRemainderTheorem.CheckRequirements (long overload) validates its inputs before solving the congruence system. It throws ArgumentException when either list is null or when the lists of remainders (a_i) and moduli (n_i) have different lengths, since each a_i must pair with one n_i.

Solutions

  1. Ensure every remainder a_i has a corresponding modulus n_i so both lists have equal, non-null length.
  2. Null-check or default-initialize both lists before calling Compute.
  3. Build both lists in a single pass over the parsed input so they cannot diverge.

Example fix

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

// after
if (listOfAs != null && listOfNs != null && listOfAs.Count == listOfNs.Count)
{
    ChineseRemainderTheorem.Compute(listOfAs, listOfNs);
}
Defensive patterns

Strategy: validation

Validate before calling

if (listOfAs == null || listOfNs == null || listOfAs.Count != listOfNs.Count)
{
    throw new ArgumentException("Remainders and moduli must be non-null and of equal length.");
}
ChineseRemainderTheorem.Compute(listOfAs, listOfNs);

Type guard

static bool IsPairedInput<T>(List<T> a, List<T> n) => a != null && n != null && a.Count == n.Count;

Try / catch

try
{
    var result = ChineseRemainderTheorem.Compute(listOfAs, listOfNs);
}
catch (ArgumentException ex) when (ex.Message.Contains("must not be null and have to be of equal length"))
{
    logger.LogError("CRT input lists unpaired: a={A}, n={N}", listOfAs?.Count, listOfNs?.Count);
    throw;
}

Prevention

When it happens

Trigger: Calling Compute with a null List<long> for listOfAs or listOfNs, or lists of unequal counts (e.g. 3 remainders but 2 moduli).

Common situations: Parsing congruence systems from text where one line failed to parse and was skipped; building the lists in separate loops with different conditions; passing uninitialized lists.

Related errors


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

Appendix: source

Thrown at Algorithms/ModularArithmetic/ChineseRemainderTheorem.cs:124

        if (result < 0)
        {
            result += prodN;
        }

        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;

View on GitHub (pinned to 96e2905cab)