{"record":{"id":"01f4ac28417bd774","repo":"TheAlgorithms/Python","slug":"number-n-must-instead-be-a-positive-integer","errorCode":null,"errorMessage":"Number {n} must instead be a positive integer","messagePattern":"Number (.+?) must instead be a positive integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/segmented_sieve.py","lineNumber":35,"sourceCode":"    >>> sieve(0)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Number 0 must instead be a positive integer\r\n\r\n    >>> sieve(-1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Number -1 must instead be a positive integer\r\n\r\n    >>> sieve(22.2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Number 22.2 must instead be a positive integer\r\n    \"\"\"\r\n\r\n    if n <= 0 or isinstance(n, float):\r\n        msg = f\"Number {n} must instead be a positive integer\"\r\n        raise ValueError(msg)\r\n\r\n    in_prime = []\r\n    start = 2\r\n    end = int(math.sqrt(n))  # Size of every segment\r\n    temp = [True] * (end + 1)\r\n    prime = []\r\n\r\n    while start <= end:\r\n        if temp[start] is True:\r\n            in_prime.append(start)\r\n            for i in range(start * start, end + 1, start):\r\n                temp[i] = False\r\n        start += 1\r\n    prime += in_prime\r\n\r\n    low = end + 1\r\n    high = min(2 * end, n)\r\n\r","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/segmented_sieve.py#L17-L53","documentation":"sieve() in maths/segmented_sieve.py implements a segmented Sieve of Eratosthenes for primes up to n. It raises ValueError(f'Number {n} must instead be a positive integer') when n <= 0 or isinstance(n, float). Floats are rejected explicitly (unlike some sibling sieves) because int(math.sqrt(n)) and the segment arithmetic assume exact integers; the message interpolates the offending value, e.g. 'Number 22.2 must instead be a positive integer'.","triggerScenarios":"Calling sieve(-1), sieve(0), or sieve(22.2). Note 22.0 is also rejected — any float fails the isinstance check even when integral.","commonSituations":"Passing a normalized ratio or averaged bound (always float); a computed upper bound that hits 0 on empty input; mixing this sieve with other algorithms in the repo whose float handling differs, so code 'that worked over there' fails here.","solutions":["Ensure the bound is an int and >= 1: sieve(int(n)) after a positivity check.","Compute bounds with integer operations (len(x), //, max+1) rather than / which yields floats.","Validate n >= 1 (and not float) upstream with your own error if the bound is external."],"exampleFix":"# before\nsieve(upper * 1.0)  # ValueError: Number 22.0 must instead be a positive integer\n\n# after\nsieve(int(upper))","handlingStrategy":"validation","validationCode":"if not isinstance(n, int) or n < 1:\n    n = int(n)\n    if n < 1:\n        raise ValueError(f'bad sieve bound: {n}')\nsieve(n)","typeGuard":"def is_sieve_bound(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 1","tryCatchPattern":null,"preventionTips":["Use integer arithmetic (//, len, max) when computing bounds — / yields floats.","This sieve rejects ALL floats, even integral ones like 22.0.","Validate the bound once where it enters your program."],"tags":["python","value-error","primes","sieve","maths"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}