{"record":{"id":"875a81a1f8e30cc7","repo":"TheAlgorithms/Python","slug":"please-enter-positive-integers-for-n-and-k-where-n","errorCode":null,"errorMessage":"Please enter positive integers for n and k where n >= k","messagePattern":"Please enter positive integers for n and k where n >= k","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/combinations.py","lineNumber":36,"sourceCode":"    >>> combinations(20,5)\n    15504\n\n    >>> combinations(52, 5)\n    2598960\n\n    >>> combinations(0, 0)\n    1\n\n    >>> combinations(-4, -5)\n    ...\n    Traceback (most recent call last):\n    ValueError: Please enter positive integers for n and k where n >= k\n    \"\"\"\n\n    # If either of the conditions are true, the function is being asked\n    # to calculate a factorial of a negative number, which is not possible\n    if n < k or k < 0:\n        raise ValueError(\"Please enter positive integers for n and k where n >= k\")\n    res = 1\n    for i in range(k):\n        res *= n - i\n        res //= i + 1\n    return res\n\n\nif __name__ == \"__main__\":\n    print(\n        \"The number of five-card hands possible from a standard\",\n        f\"fifty-two card deck is: {combinations(52, 5)}\\n\",\n    )\n\n    print(\n        \"If a class of 40 students must be arranged into groups of\",\n        f\"4 for group projects, there are {combinations(40, 4)} ways\",\n        \"to arrange them.\\n\",\n    )","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/combinations.py#L18-L54","documentation":"Raised by combinations() in maths/combinations.py when asked for a binomial coefficient that would require a factorial of a negative number: specifically n < k or k < 0. The multiplicative formula res *= n - i over range(k) only makes combinatorial sense for 0 <= k <= n, so the function guards that up front with ValueError.","triggerScenarios":"Calling combinations(-4, -5), combinations(3, 5) (k > n), or combinations(10, -1). Note combinations(0, 0) is valid and returns 1, and combinations(n, 0) returns 1.","commonSituations":"Swapped arguments (combinations(k, n)); loop bounds where the upper index comes from a shorter list than the selection size (e.g. choosing 5 items from a 3-item list); negative k from subtracting sizes (len(a) - len(b) when b is longer).","solutions":["Check the argument order — the signature is combinations(n, k) with n total items and k chosen.","Clamp k with k = min(k, n) or return 0 by convention when k > n, mirroring math.comb.","Prefer the standard library math.comb(n, k) which returns 0 for k > n instead of raising."],"exampleFix":"# before\ncombinations(3, 5)  # ValueError\n\n# after\nfrom math import comb\nresult = comb(3, 5)  # 0, no exception\n# or guard: n, k = max(n, k), min(n, k) if args may be swapped","handlingStrategy":"validation","validationCode":"if k < 0 or n < k:\n    raise ValueError(f'need 0 <= k <= n, got n={n}, k={k}')\nresult = combinations(n, k)","typeGuard":null,"tryCatchPattern":"try:\n    result = combinations(n, k)\nexcept ValueError:\n    result = 0  # mirror math.comb convention for k > n, if that suits your domain","preventionTips":["Prefer math.comb which returns 0 for k > n instead of raising.","Assert 0 <= k <= n when n and k come from different list lengths."],"tags":["maths","combinatorics","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}