TheAlgorithms/Python · critical · TypeError
Parameter n must be int or castable to int.
Error message
Parameter n must be int or castable to int.
What it means
Documented TypeError from project_euler/problem_003/sol1.py:solution when int(n) fails (e.g. solution('asd')). CAVEAT: the guard uses Python 2 syntax 'except TypeError, ValueError:' - under Python 3 this file raises SyntaxError at import, so the TypeError never actually fires until the clause is parenthesized.
Source
Thrown at project_euler/problem_003/sol1.py:84
ValueError: Parameter n must be greater than or equal to one.
>>> solution(-17)
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
View on GitHub (pinned to f5988cc097)
Solutions
- Fix the except clause to 'except (TypeError, ValueError):' at project_euler/problem_003/sol1.py:84.
- Then pass int or int-castable values (e.g. 600851475143, '13195').
- Pre-check with isinstance(n, int) in caller code.
- Include this module in doctest CI to catch the SyntaxError class of breakage.
Example fix
# before
except TypeError, ValueError:
raise TypeError('Parameter n must be int or castable to int.')
# after
except (TypeError, ValueError):
raise TypeError('Parameter n must be int or castable to int.') from None Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(n, (int, float)) and not str(n).isdigit():
raise TypeError(f'n must be int-castable, got {n!r}')
solution(n) Type guard
def is_int_castable(v) -> bool:
try:
int(v)
return True
except (TypeError, ValueError):
return False Try / catch
try:
solution(n)
except TypeError:
... Prevention
- Repair the py2 except clause at problem_003/sol1.py:84 before anything else.
- Pass ints or numeric strings only (floats like 2.5 will truncate silently).
- Compile-check legacy modules in CI.
When it happens
Trigger: After the syntax fix: solution('asd'), solution(None), solution([1]). As written today: any import or execution of project_euler/problem_003/sol1.py under Python 3 fails with SyntaxError('multiple exception types must be parenthesized').
Common situations: Running this repo's doctests or test suite under Python 3; CI pipelines that import every solution module; migrating an old checkout of the algorithms repository to modern Python.
Related errors
- Parameter n must be int or castable to int.
- Parameter n must be int or castable to int.
- Parameter n must be int or castable to int.
- Parameter n must be greater than or equal to one.
- Parameter n must be greater than or equal to one.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/8fe7e94277ac22f9.
Report an issue: GitHub.