{"record":{"id":"83ff0edcc87ebc96","repo":"TheAlgorithms/Python","slug":"n-and-r-must-be-non-negative-integers","errorCode":null,"errorMessage":"n and r must be non-negative integers","messagePattern":"n and r must be non-negative integers","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/binomial_coefficient.py","lineNumber":43,"sourceCode":"    >>> binomial_coefficient(-2, 3)\n    Traceback (most recent call last):\n        ...\n    ValueError: n and r must be non-negative integers\n    >>> binomial_coefficient(5, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: n and r must be non-negative integers\n    >>> binomial_coefficient(10.1, 5)\n    Traceback (most recent call last):\n        ...\n    TypeError: 'float' object cannot be interpreted as an integer\n    >>> binomial_coefficient(10, 5.1)\n    Traceback (most recent call last):\n        ...\n    TypeError: 'float' object cannot be interpreted as an integer\n    \"\"\"\n    if n < 0 or r < 0:\n        raise ValueError(\"n and r must be non-negative integers\")\n    if 0 in (n, r):\n        return 1\n    c = [0 for i in range(r + 1)]\n    # nc0 = 1\n    c[0] = 1\n    for i in range(1, n + 1):\n        # to compute current row from previous row.\n        j = min(i, r)\n        while j > 0:\n            c[j] += c[j - 1]\n            j -= 1\n    return c[r]\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/binomial_coefficient.py#L25-L61","documentation":"binomial_coefficient(n, r) computes C(n, r) with a Pascal-row DP. It raises ValueError('n and r must be non-negative integers') when n < 0 or r < 0; note it does not type-check, so floats fall through to a TypeError from range() instead.","triggerScenarios":"binomial_coefficient(-1, 5); binomial_coefficient(10, -2); negative r from an r = k - n computation where k < n.","commonSituations":"Combinatorics loops with underflowing indices; deriving r via subtraction; passing parsed floats like 10.0 will NOT raise this error but a later TypeError — sanitize types too.","solutions":["Validate n >= 0 and r >= 0 before calling.","Coerce float inputs with int() when they are whole numbers.","For r > n, know the function returns a row value rather than erroring — check that case separately if you need C(n,r)=0 semantics."],"exampleFix":"# before\nc = binomial_coefficient(n, k - n)  # negative when k < n\n\n# after\nr = k - n\nc = binomial_coefficient(n, r) if r >= 0 else 0","handlingStrategy":"validation","validationCode":"if n < 0 or r < 0:\n    raise ValueError(f\"n and r must be non-negative: n={n}, r={r}\")\nif not isinstance(n, int) or not isinstance(r, int):\n    raise TypeError(\"n and r must be integers\")\nc = binomial_coefficient(n, r)","typeGuard":"def valid_binom_args(n: object, r: object) -> bool:\n    return isinstance(n, int) and isinstance(r, int) and n >= 0 and r >= 0","tryCatchPattern":null,"preventionTips":["The function does not type-check — floats raise a later TypeError from range()","Clamp r = k - n results to 0 semantics yourself when k < n"],"tags":["math","combinatorics","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}