{"record":{"id":"ff4a1e239b2f03bc","repo":"TheAlgorithms/Python","slug":"only-positive-integers-have-prime-factors","errorCode":null,"errorMessage":"Only positive integers have prime factors","messagePattern":"Only positive integers have prime factors","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/basic_maths.py","lineNumber":20,"sourceCode":"\r\nimport math\r\n\r\n\r\ndef prime_factors(n: int) -> list:\r\n    \"\"\"Find Prime Factors.\r\n    >>> prime_factors(100)\r\n    [2, 2, 5, 5]\r\n    >>> prime_factors(0)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Only positive integers have prime factors\r\n    >>> prime_factors(-10)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Only positive integers have prime factors\r\n    \"\"\"\r\n    if n <= 0:\r\n        raise ValueError(\"Only positive integers have prime factors\")\r\n    pf = []\r\n    while n % 2 == 0:\r\n        pf.append(2)\r\n        n = int(n / 2)\r\n    for i in range(3, int(math.sqrt(n)) + 1, 2):\r\n        while n % i == 0:\r\n            pf.append(i)\r\n            n = int(n / i)\r\n    if n > 2:\r\n        pf.append(n)\r\n    return pf\r\n\r\n\r\ndef number_of_divisors(n: int) -> int:\r\n    \"\"\"Calculate Number of Divisors of an Integer.\r\n    >>> number_of_divisors(100)\r\n    9\r\n    >>> number_of_divisors(0)\r","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/basic_maths.py#L2-L38","documentation":"prime_factors() returns the prime factorization of n by trial division. It raises ValueError('Only positive integers have prime factors') for n <= 0 because factorization is only defined for positive integers.","triggerScenarios":"prime_factors(0); prime_factors(-10); any n <= 0 reaching the function from user input or a computation that underflowed to/below zero.","commonSituations":"Parsing signed input; loops that decrement a counter past 1; zero sentinels used to mean 'no value' but passed through to math code.","solutions":["Filter or reject n <= 0 before calling.","Use abs(n) only if negative input is genuinely meaningful in your domain (it changes semantics).","Treat 0 and negatives as invalid data at the parse boundary, not deep in the math call."],"exampleFix":"# before\nfactors = prime_factors(n)\n\n# after\nif n <= 0:\n    raise ValueError(f\"expected positive integer, got {n}\")\nfactors = prime_factors(n)","handlingStrategy":"validation","validationCode":"if n <= 0 or not isinstance(n, int):\n    raise ValueError(f\"n must be a positive integer, got {n!r}\")\npf = prime_factors(n)","typeGuard":"def is_positive_int(n: object) -> bool:\n    return isinstance(n, int) and n > 0","tryCatchPattern":null,"preventionTips":["Reject 0 and negatives at input parse time","Use is_positive_int guard shared across basic_maths functions"],"tags":["math","number-theory","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}