{"record":{"id":"b1e6c3a6e0c67be2","repo":"TheAlgorithms/Python","slug":"input-must-be-a-positive-integer-b1e6c3","errorCode":null,"errorMessage":"Input must be a positive integer","messagePattern":"Input must be a positive integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/prime_sieve_eratosthenes.py","lineNumber":34,"sourceCode":"    \"\"\"\n    Print the prime numbers up to n\n\n    >>> prime_sieve_eratosthenes(10)\n    [2, 3, 5, 7]\n    >>> prime_sieve_eratosthenes(20)\n    [2, 3, 5, 7, 11, 13, 17, 19]\n    >>> prime_sieve_eratosthenes(2)\n    [2]\n    >>> prime_sieve_eratosthenes(1)\n    []\n    >>> prime_sieve_eratosthenes(-1)\n    Traceback (most recent call last):\n    ...\n    ValueError: Input must be a positive integer\n    \"\"\"\n\n    if num <= 0:\n        raise ValueError(\"Input must be a positive integer\")\n\n    primes = [True] * (num + 1)\n\n    p = 2\n    while p * p <= num:\n        if primes[p]:\n            for i in range(p * p, num + 1, p):\n                primes[i] = False\n        p += 1\n\n    return [prime for prime in range(2, num + 1) if primes[prime]]\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/prime_sieve_eratosthenes.py#L16-L52","documentation":"prime_sieve_eratosthenes() in maths/prime_sieve_eratosthenes.py returns all primes <= num via the classic sieve. It raises ValueError('Input must be a positive integer') when num <= 0, because primes[True] * (num + 1) would build an empty/negative-length list and the sieve loop would be meaningless. num == 1 is legal and returns [] (no primes <= 1); the rejection starts at 0 and below.","triggerScenarios":"Calling prime_sieve_eratosthenes(-1) or prime_sieve_eratosthenes(0). Note floats like 2.0 are NOT rejected here — [True] * (2.0 + 1) would fail differently — so the documented guard covers only the <= 0 case.","commonSituations":"Passing an upper bound computed as max(list) - 1 on empty lists (yields negative), a user-supplied limit that defaults to 0, or an off-by-one that lands at 0.","solutions":["Check the bound before calling: raise or default when num < 1 in your own code.","Ensure limit-producing expressions (max(), len()-1, a-b) cannot go non-positive on degenerate inputs.","Catch ValueError when the limit is external user input."],"exampleFix":"# before\nprimes = prime_sieve_eratosthenes(limit)  # ValueError when limit == 0\n\n# after\nlimit = max(1, limit)\nprimes = prime_sieve_eratosthenes(limit)","handlingStrategy":"validation","validationCode":"if num < 1:\n    raise ValueError(f'upper bound must be >= 1, got {num}')\nprime_sieve_eratosthenes(num)","typeGuard":"def is_sieve_bound(v) -> bool:\n    return isinstance(v, int) and v >= 1","tryCatchPattern":null,"preventionTips":["Guard max()/len()-derived bounds against going non-positive.","Default user-supplied limits to a sane positive value.","num == 1 is valid and returns [] — reserve errors for <= 0."],"tags":["python","value-error","primes","sieve","maths"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}