{"record":{"id":"324844b8b96a1dbc","repo":"TheAlgorithms/Python","slug":"is-prime-only-accepts-positive-integers","errorCode":null,"errorMessage":"is_prime() only accepts positive integers","messagePattern":"is_prime\\(\\) only accepts positive integers","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/prime_check.py","lineNumber":44,"sourceCode":"    >>> is_prime(563)\n    True\n    >>> is_prime(2999)\n    True\n    >>> is_prime(67483)\n    False\n    >>> is_prime(16.1)\n    Traceback (most recent call last):\n        ...\n    ValueError: is_prime() only accepts positive integers\n    >>> is_prime(-4)\n    Traceback (most recent call last):\n        ...\n    ValueError: is_prime() only accepts positive integers\n    \"\"\"\n\n    # precondition\n    if not isinstance(number, int) or not number >= 0:\n        raise ValueError(\"is_prime() only accepts positive integers\")\n\n    if 1 < number < 4:\n        # 2 and 3 are primes\n        return True\n    elif number < 2 or number % 2 == 0 or number % 3 == 0:\n        # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes\n        return False\n\n    # All primes number are in format of 6k +/- 1\n    for i in range(5, int(math.sqrt(number) + 1), 6):\n        if number % i == 0 or number % (i + 2) == 0:\n            return False\n    return True\n\n\nclass Test(unittest.TestCase):\n    def test_primes(self):\n        assert is_prime(2)","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/prime_check.py#L26-L62","documentation":"is_prime() in maths/prime_check.py uses 6k+/-1 trial division and requires its argument to be a non-negative Python int: if not isinstance(number, int) or not number >= 0 it raises ValueError('is_prime() only accepts positive integers'). Both wrong type (floats like 16.1, strings) and negatives (-4) hit the same guard. The message says 'positive' but 0 and 1 are accepted (they return False), so the real contract is 'non-negative integer'.","triggerScenarios":"Calling is_prime(16.1), is_prime(-4), is_prime('7'), or is_prime(7.0). Values read from JSON ('7' or 7.0) or division results (10/2 == 5.0) commonly produce floats.","commonSituations":"Python 3 division always yielding floats (n / 2 passed onward); JSON/deserialized data; code ported from Python 2 where 5/2 was int; catching TypeError instead of ValueError around the call.","solutions":["Convert with int() when the value is integral: is_prime(int(number)) or use is_prime(number // divisor) style integer ops.","Validate at the data boundary so only ints reach primality code.","Catch ValueError if bad input is a runtime possibility in your pipeline."],"exampleFix":"# before\nis_prime(n / 2)  # float argument -> ValueError\n\n# after\nis_prime(n // 2)  # true division yields float; floor division yields int","handlingStrategy":"type-guard","validationCode":"if not isinstance(number, int) or number < 0:\n    number = int(number)\nis_prime(number)","typeGuard":"def is_prime_input(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 0","tryCatchPattern":"try:\n    is_prime(n)\nexcept ValueError:\n    n = int(n)  # last resort for numeric strings/floats","preventionTips":["Use // not / when feeding division results onward.","Convert JSON numbers ('7', 7.0) before primality checks.","The contract is non-negative int: 0 and 1 are accepted and return False."],"tags":["python","value-error","primes","input-validation","maths"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}