{"record":{"id":"1f18c76ccbabcafc","repo":"TheAlgorithms/C-Sharp","slug":"num-k-0","errorCode":null,"errorMessage":"num ≥ k ≥ 0","messagePattern":"num ≥ k ≥ 0","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/BinomialCoefficient.cs","lineNumber":19,"sourceCode":"namespace Algorithms.Numeric;\n\n/// <summary>\n///     The binomial coefficients are the positive integers\n///     that occur as coefficients in the binomial theorem.\n/// </summary>\npublic static class BinomialCoefficient\n{\n    /// <summary>\n    ///     Calculates Binomial coefficients for given input.\n    /// </summary>\n    /// <param name=\"num\">First number.</param>\n    /// <param name=\"k\">Second number.</param>\n    /// <returns>Binimial Coefficients.</returns>\n    public static BigInteger Calculate(BigInteger num, BigInteger k)\n    {\n        if (num < k || k < 0)\n        {\n            throw new ArgumentException(\"num ≥ k ≥ 0\");\n        }\n\n        // Tricks to gain performance:\n        // 1. Because (num over k) equals (num over (num-k)), we can save multiplications and divisions\n        // by replacing k with the minimum of k and (num - k).\n        k = BigInteger.Min(k, num - k);\n\n        // 2. We can simplify the computation of (num! / (k! * (num - k)!)) to ((num * (num - 1) * ... * (num - k + 1) / (k!))\n        // and thus save some multiplications and divisions.\n        var numerator = BigInteger.One;\n        for (var val = num - k + 1; val <= num; val++)\n        {\n            numerator *= val;\n        }\n\n        // 3. Typically multiplication is a lot faster than division, therefore compute the value of k! first (i.e. k - 1 multiplications)\n        // and then divide the numerator by the denominator (i.e. 1 division); instead of performing k - 1 divisions (1 for each factor in k!).\n        var denominator = BigInteger.One;","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/BinomialCoefficient.cs#L1-L37","documentation":"BinomialCoefficient.Calculate(num, k) computes C(num, k) and requires the mathematical precondition num >= k >= 0. If k is negative or k exceeds num, no binomial coefficient exists, so the method throws ArgumentException with the contract message 'num ≥ k ≥ 0'.","triggerScenarios":"Calculate(3, 5) (k > num); Calculate(5, -1) (negative k); passing a formula-derived k that can transiently exceed num, e.g. in probability or combinatorics loops where the bounds cross.","commonSituations":"Loop variables where k runs past num without an early break; user input for 'choose k from n' collected without validation; sign errors making k negative in numeric code using BigInteger results.","solutions":["Validate 0 <= k && k <= num before calling and return 0 (the conventional value for invalid k) or handle it in your logic.","Swap or clamp arguments when the choice is symmetric: use Min(k, num - k) semantics only after confirming k <= num.","If k can legitimately exceed num in your formula, treat C(num, k) as 0 and short-circuit instead of calling Calculate."],"exampleFix":"// before\nvar c = BinomialCoefficient.Calculate(n, k); // throws if n < k or k < 0\n// after\nvar c = (k < 0 || k > n) ? BigInteger.Zero : BinomialCoefficient.Calculate(n, k);","handlingStrategy":"validation","validationCode":"if (k < 0 || k > num)\n    return BigInteger.Zero; // conventional value for invalid k\nvar c = BinomialCoefficient.Calculate(num, k);","typeGuard":null,"tryCatchPattern":"try\n{\n    c = BinomialCoefficient.Calculate(n, k);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"num ≥ k ≥ 0\"))\n{\n    c = BigInteger.Zero;\n}","preventionTips":["Clamp or reject k outside [0, num] before computing.","In loops, break before k exceeds num.","Validate n/k from user input at the boundary."],"tags":["argument-validation","combinatorics","math","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"}