TheAlgorithms/C-Sharp · error · ArgumentException
num ≥ k ≥ 0
Error message
num ≥ k ≥ 0
What it means
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'.
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.
Example fix
// before var c = BinomialCoefficient.Calculate(n, k); // throws if n < k or k < 0 // after var c = (k < 0 || k > n) ? BigInteger.Zero : BinomialCoefficient.Calculate(n, k);
Defensive patterns
Strategy: validation
Validate before calling
if (k < 0 || k > num)
return BigInteger.Zero; // conventional value for invalid k
var c = BinomialCoefficient.Calculate(num, k); Try / catch
try
{
c = BinomialCoefficient.Calculate(n, k);
}
catch (ArgumentException ex) when (ex.Message.Contains("num ≥ k ≥ 0"))
{
c = BigInteger.Zero;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- cannot be negative
- An automorphic number must always be positive.
- should be greater than zero
- Invalid parameter settings for Ascon Hash
- Cash flows list cannot be empty
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/1f18c76ccbabcafc.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/BinomialCoefficient.cs:19
namespace Algorithms.Numeric;
/// <summary>
/// The binomial coefficients are the positive integers
/// that occur as coefficients in the binomial theorem.
/// </summary>
public static class BinomialCoefficient
{
/// <summary>
/// Calculates Binomial coefficients for given input.
/// </summary>
/// <param name="num">First number.</param>
/// <param name="k">Second number.</param>
/// <returns>Binimial Coefficients.</returns>
public static BigInteger Calculate(BigInteger num, BigInteger k)
{
if (num < k || k < 0)
{
throw new ArgumentException("num ≥ k ≥ 0");
}
// Tricks to gain performance:
// 1. Because (num over k) equals (num over (num-k)), we can save multiplications and divisions
// by replacing k with the minimum of k and (num - k).
k = BigInteger.Min(k, num - k);
// 2. We can simplify the computation of (num! / (k! * (num - k)!)) to ((num * (num - 1) * ... * (num - k + 1) / (k!))
// and thus save some multiplications and divisions.
var numerator = BigInteger.One;
for (var val = num - k + 1; val <= num; val++)
{
numerator *= val;
}
// 3. Typically multiplication is a lot faster than division, therefore compute the value of k! first (i.e. k - 1 multiplications)
// and then divide the numerator by the denominator (i.e. 1 division); instead of performing k - 1 divisions (1 for each factor in k!).
var denominator = BigInteger.One;View on GitHub (pinned to 96e2905cab)