{"record":{"id":"3901600e8a731281","repo":"TheAlgorithms/Python","slug":"only-positive-numbers-are-accepted","errorCode":null,"errorMessage":"Only positive numbers are accepted","messagePattern":"Only positive numbers are accepted","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/basic_maths.py","lineNumber":48,"sourceCode":"        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\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Only positive numbers are accepted\r\n    >>> number_of_divisors(-10)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Only positive numbers are accepted\r\n    \"\"\"\r\n    if n <= 0:\r\n        raise ValueError(\"Only positive numbers are accepted\")\r\n    div = 1\r\n    temp = 1\r\n    while n % 2 == 0:\r\n        temp += 1\r\n        n = int(n / 2)\r\n    div *= temp\r\n    for i in range(3, int(math.sqrt(n)) + 1, 2):\r\n        temp = 1\r\n        while n % i == 0:\r\n            temp += 1\r\n            n = int(n / i)\r\n        div *= temp\r\n    if n > 1:\r\n        div *= 2\r\n    return div\r\n\r\n\r\ndef sum_of_divisors(n: int) -> int:\r","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/basic_maths.py#L30-L66","documentation":"number_of_divisors() counts divisors via factorization. Divisor count is only defined for positive integers, so n <= 0 raises ValueError('Only positive numbers are accepted').","triggerScenarios":"number_of_divisors(0); number_of_divisors(-10); any range iteration that includes 0, e.g. loop over range(0, n) calling the function.","commonSituations":"Iterating over sequences that start at 0; accepting negative user input; reuse of the same n across several basic_maths functions where 0 slipped through.","solutions":["Start loops at 1 (range(1, n + 1)) when enumerating candidates.","Validate n > 0 at the input boundary.","Special-case 0 explicitly if your domain needs it, since the library will not."],"exampleFix":"# before\nfor i in range(0, 101):\n    d = number_of_divisors(i)  # fails at i=0\n\n# after\nfor i in range(1, 101):\n    d = number_of_divisors(i)","handlingStrategy":"validation","validationCode":"if n <= 0:\n    raise ValueError(f\"n must be positive, got {n}\")\ncount = number_of_divisors(n)","typeGuard":"def is_positive_int(n: object) -> bool:\n    return isinstance(n, int) and n > 0","tryCatchPattern":null,"preventionTips":["Start enumeration loops at 1, never 0","Share one positivity guard across all basic_maths call sites"],"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"}