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/sol2.py:solution when n <= 0 after int(n) succeeds; the trial-division factorization loop requires a positive integer. CAVEAT: the module is unparseable under Python 3 ('except TypeError, ValueError:' at line 48), so it raises SyntaxError at import and this ValueError is unreachable until the syntax is repaired.
Source
Thrown at project_euler/problem_003/sol2.py:50
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.")
prime = 1
i = 2
while i * i <= n:
while n % i == 0:
prime = i
n //= i
i += 1
if n > 1:
prime = n
return int(prime)
if __name__ == "__main__":
print(f"{solution() = }")
View on GitHub (pinned to f5988cc097)
Solutions
- Repair the except clause at project_euler/problem_003/sol2.py:48 to parenthesized form so the module loads.
- Pass n >= 1.
- Validate bounds before calling: if not isinstance(n, int) or n < 1: reject.
- Catch ValueError when iterating over externally supplied bounds.
Example fix
# before
solution(0)
# after
n = max(int(user_input), 1) if str(user_input).strip().isdigit() else None
if n is None:
raise ValueError('n must be a positive integer')
solution(n) Defensive patterns
Strategy: validation
Validate before calling
n = int(n)
if n <= 0:
raise ValueError(f'n must be >= 1, got {n}')
solution(n) Type guard
def is_valid_factorization_input(n) -> bool:
return isinstance(n, int) and n >= 1 Try / catch
try:
solution(n)
except ValueError:
... Prevention
- Start sweep ranges at 1, not 0.
- Fix the module's py2 except syntax before relying on any of its errors.
- Treat empty-string input separately: int('') raises, it does not become 0.
When it happens
Trigger: After the fix: solution(0) or solution(-1). Today: any import of project_euler/problem_003/sol2.py under Python 3 raises SyntaxError('multiple exception types must be parenthesized') instead.
Common situations: Calling the legacy solution from a py3 driver or doctest; batch scripts enumerating n from a range that includes 0; input parsing that yields 0 for empty strings (int('') raises TypeError instead - a related trap).
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/66218fe516305aee.
Report an issue: GitHub.