{"record":{"id":"53b633ac62c86e78","repo":"TheAlgorithms/C-Sharp","slug":"the-value-listofns-first-x-x-1-for-some-n-i-is-smaller-than","errorCode":null,"errorMessage":"The value {listOfNs.First(x => x <= 1)} for some n_i is smaller than or equal to 1.","messagePattern":"The value (.+?) for some n_i is smaller than or equal to 1\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/ModularArithmetic/ChineseRemainderTheorem.cs","lineNumber":129,"sourceCode":"        return result;\n    }\n\n    /// <summary>\n    /// Checks the requirements for the algorithm and throws an ArgumentException if they are not being met.\n    /// </summary>\n    /// <param name=\"listOfAs\">An ordered list of a_0, a_1, ..., a_k.</param>\n    /// <param name=\"listOfNs\">An ordered list of n_0, n_1, ..., n_k.</param>\n    /// <exception cref=\"ArgumentException\">If any of the requirements is not fulfilled.</exception>\n    private static void CheckRequirements(List<long> listOfAs, List<long> listOfNs)\n    {\n        if (listOfAs == null || listOfNs == null || listOfAs.Count != listOfNs.Count)\n        {\n            throw new ArgumentException(\"The parameters 'listOfAs' and 'listOfNs' must not be null and have to be of equal length!\");\n        }\n\n        if (listOfNs.Any(x => x <= 1))\n        {\n            throw new ArgumentException($\"The value {listOfNs.First(x => x <= 1)} for some n_i is smaller than or equal to 1.\");\n        }\n\n        if (listOfAs.Any(x => x < 0))\n        {\n            throw new ArgumentException($\"The value {listOfAs.First(x => x < 0)} for some a_i is smaller than 0.\");\n        }\n\n        // Check if all pairs of (n_i, n_j) are coprime:\n        for (var i = 0; i < listOfNs.Count; i++)\n        {\n            for (var j = i + 1; j < listOfNs.Count; j++)\n            {\n                long gcd;\n                if ((gcd = ExtendedEuclideanAlgorithm.Compute(listOfNs[i], listOfNs[j]).Gcd) != 1L)\n                {\n                    throw new ArgumentException($\"The GCD of n_{i} = {listOfNs[i]} and n_{j} = {listOfNs[j]} equals {gcd} and thus these values aren't coprime.\");\n                }\n            }","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/ModularArithmetic/ChineseRemainderTheorem.cs#L111-L147","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Filter or validate listOfNs so every modulus is >= 2 before calling Compute.","Fix the source of the zero/one modulus (unset defaults, bad input parsing).","Surface a clear validation message to the user when collecting the congruence system."],"exampleFix":"// before\nChineseRemainderTheorem.Compute(new List<long> { 2 }, new List<long> { 1, 3 }); // throws\n\n// after\nif (listOfNs.All(n => n > 1))\n{\n    ChineseRemainderTheorem.Compute(listOfAs, listOfNs);\n}","handlingStrategy":"validation","validationCode":"if (listOfNs.Any(n => n <= 1))\n{\n    throw new ArgumentException(\"All moduli must be greater than 1.\");\n}\nChineseRemainderTheorem.Compute(listOfAs, listOfNs);","typeGuard":"static bool HasValidModuli(List<long> ns) => ns != null && ns.All(n => n > 1);","tryCatchPattern":"try\n{\n    var result = ChineseRemainderTheorem.Compute(listOfAs, listOfNs);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"smaller than or equal to 1\"))\n{\n    logger.LogError(\"Invalid modulus in CRT input: {Msg}\", ex.Message);\n    throw;\n}","preventionTips":["Validate moduli >= 2 when collecting the congruence system from users.","Avoid default-initialized (zero) entries in modulus arrays.","Document modulus constraints in the caller's input validation messages."],"tags":["math","modular-arithmetic","invalid-argument-value","csharp"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}