TheAlgorithms/Python · error · ValueError
is_prime() only accepts positive integers
Error message
is_prime() only accepts positive integers
What it means
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'.
Source
Thrown at maths/prime_check.py:44
>>> is_prime(563)
True
>>> is_prime(2999)
True
>>> is_prime(67483)
False
>>> is_prime(16.1)
Traceback (most recent call last):
...
ValueError: is_prime() only accepts positive integers
>>> is_prime(-4)
Traceback (most recent call last):
...
ValueError: is_prime() only accepts positive integers
"""
# precondition
if not isinstance(number, int) or not number >= 0:
raise ValueError("is_prime() only accepts positive integers")
if 1 < number < 4:
# 2 and 3 are primes
return True
elif number < 2 or number % 2 == 0 or number % 3 == 0:
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
return False
# All primes number are in format of 6k +/- 1
for i in range(5, int(math.sqrt(number) + 1), 6):
if number % i == 0 or number % (i + 2) == 0:
return False
return True
class Test(unittest.TestCase):
def test_primes(self):
assert is_prime(2)View on GitHub (pinned to f5988cc097)
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.
Example fix
# before is_prime(n / 2) # float argument -> ValueError # after is_prime(n // 2) # true division yields float; floor division yields int
Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(number, int) or number < 0:
number = int(number)
is_prime(number) Type guard
def is_prime_input(v) -> bool:
return isinstance(v, int) and not isinstance(v, bool) and v >= 0 Try / catch
try:
is_prime(n)
except ValueError:
n = int(n) # last resort for numeric strings/floats Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- number must be an integer
- multiplicative_persistence() only accepts integral values
- multiplicative_persistence() does not accept negative values
- additive_persistence() only accepts integral values
- additive_persistence() does not accept negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/324844b8b96a1dbc.
Report an issue: GitHub.