{"record":{"id":"753c830d82e4b2b9","repo":"TheAlgorithms/Python","slug":"factorial-not-defined-for-negative-values-753c83","errorCode":null,"errorMessage":"factorial() not defined for negative values","messagePattern":"factorial\\(\\) not defined for negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/factorial.py","lineNumber":31,"sourceCode":"    >>> factorial(0.1)\n    Traceback (most recent call last):\n        ...\n    ValueError: factorial() only accepts integral values\n    >>> factorial(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: factorial() not defined for negative values\n    >>> factorial(1)\n    1\n    >>> factorial(6)\n    720\n    >>> factorial(0)\n    1\n    \"\"\"\n    if number != int(number):\n        raise ValueError(\"factorial() only accepts integral values\")\n    if number < 0:\n        raise ValueError(\"factorial() not defined for negative values\")\n    value = 1\n    for i in range(1, number + 1):\n        value *= i\n    return value\n\n\ndef factorial_recursive(n: int) -> int:\n    \"\"\"\n    Calculate the factorial of a positive integer\n    https://en.wikipedia.org/wiki/Factorial\n\n    >>> import math\n    >>> all(factorial_recursive(i) == math.factorial(i) for i in range(20))\n    True\n    >>> factorial_recursive(0.1)\n    Traceback (most recent call last):\n        ...\n    ValueError: factorial_recursive() only accepts integral values","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/factorial.py#L13-L49","documentation":"Raised by factorial() in maths/factorial.py when number is integral but negative. Factorial is defined only for non-negative integers (the empty product for 0 gives 1); range(1, number + 1) with a negative number would silently return 1, so the function rejects negatives with ValueError instead.","triggerScenarios":"Calling factorial(-1) or any negative whole value, including whole floats like -3.0 which pass the earlier integrality check. The guard is number < 0.","commonSituations":"Negative results of subtractions (k - n with arguments swapped); inverted loop bounds; user input with a minus sign; off-by-one errors at 0 boundaries.","solutions":["Validate number >= 0 at the call site with a domain-specific message.","Fix the argument order or arithmetic producing the negative value (common with n - k style expressions).","If negative-argument factorials are genuinely needed, use math.gamma(n + 1) and handle its poles."],"exampleFix":"# before\nfactorial(n - k)  # ValueError when k > n\n\n# after\nif n < k:\n    raise ValueError('need n >= k')\nfactorial(n - k)","handlingStrategy":"validation","validationCode":"if number < 0:\n    raise ValueError(f'factorial undefined for negative {number}')","typeGuard":null,"tryCatchPattern":"try:\n    r = factorial(number)\nexcept ValueError as e:\n    if 'negative values' in str(e):\n        raise ValueError(f'swapped n/k or bad arithmetic produced {number}') from e\n    raise","preventionTips":["Check n >= k before computing factorial(n - k) style expressions.","Use math.gamma(n + 1) only if you truly need the analytic continuation."],"tags":["maths","factorial","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}