TheAlgorithms/Python · critical · ValueError
Parameter n must be greater than or equal to one.
Error message
Parameter n must be greater than or equal to one.
What it means
Documented ValueError from project_euler/problem_003/sol1.py:solution when n <= 0 after int conversion; the largest-prime-factor algorithm only accepts positive integers. CAVEAT: unreachable as shipped because the file's 'except TypeError, ValueError:' is Python 2 syntax and the module raises SyntaxError under Python 3 before this check runs.
Source
Thrown at project_euler/problem_003/sol1.py:86
Traceback (most recent call last):
...
ValueError: Parameter n must be greater than or equal to one.
>>> solution([])
Traceback (most recent call last):
...
TypeError: Parameter n must be int or castable to int.
>>> solution("asd")
Traceback (most recent call last):
...
TypeError: Parameter n must be int or castable to int.
"""
try:
n = int(n)
except TypeError, ValueError:
raise TypeError("Parameter n must be int or castable to int.")
if n <= 0:
raise ValueError("Parameter n must be greater than or equal to one.")
max_number = 0
if is_prime(n):
return n
while n % 2 == 0:
n //= 2
if is_prime(n):
return n
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
if is_prime(n // i):
max_number = n // i
break
elif is_prime(i):
max_number = i
return max_number
if __name__ == "__main__":View on GitHub (pinned to f5988cc097)
Solutions
- Fix the except clause at project_euler/problem_003/sol1.py:84 to use parenthesized exceptions so the module imports.
- Pass n >= 1 (1 is accepted; 2 returns 2 immediately).
- Clamp user-derived input: n = max(n, 1) only if 1 is semantically acceptable, otherwise reject.
- Catch ValueError for batch enumeration over possibly-invalid inputs.
Example fix
# before
solution(0)
# after
n = int(input_value)
if n <= 0:
raise ValueError('n must be positive')
result = solution(n) Defensive patterns
Strategy: validation
Validate before calling
n = int(n)
if n < 1:
raise ValueError('n must be a positive integer')
solution(n) Type guard
def is_valid_factorization_input(n) -> bool:
return isinstance(n, int) and not isinstance(n, bool) and n >= 1 Try / catch
try:
solution(n)
except ValueError as e:
if 'greater than or equal to one' in str(e):
... Prevention
- Reject n <= 0 at the call site; do not rely on the library check (module is unparseable until syntax fixed).
- Remember solution(1) returns 1 - decide if that is meaningful for you.
- Validate user/CLI input before batch runs.
When it happens
Trigger: After the syntax fix: solution(0) or solution(-10). As written today: importing the module under Python 3 fails immediately with SyntaxError.
Common situations: Generic benchmark harnesses that call every solution with 0; negative values from subtracting user input without clamping; running the legacy file under a modern interpreter where the py2 syntax breaks first.
Related errors
- Parameter n must be greater than or equal to one.
- Parameter n must be greater than or equal to one.
- Parameter n must be greater than or equal to one.
- Parameter n must be int or castable to int.
- Parameter n must be int or castable to int.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/81c221dfa71b207b.
Report an issue: GitHub.