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/sol3.py:solution when n <= 0 after int conversion; the incremental factor-finding loop assumes n >= 1. CAVEAT: unreachable in the shipped file because 'except TypeError, ValueError:' is Python 2 syntax - Python 3 raises SyntaxError at import before this branch can execute.

Source

Thrown at project_euler/problem_003/sol3.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.")
    i = 2
    ans = 0
    if n == 2:
        return 2
    while n > 2:
        while n % i != 0:
            i += 1
        ans = i
        while n % i == 0:
            n = n // i
        i += 1
    return int(ans)


if __name__ == "__main__":
    print(f"{solution() = }")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Fix the except clause at project_euler/problem_003/sol3.py:48.
  2. Pass n >= 1.
  3. Validate CLI/user input before the call (positive-integer check).
  4. Catch ValueError in enumerating drivers.

Example fix

# before
solution(-100)

# after
if int(n) > 0:
    print(solution(n))
Defensive patterns

Strategy: validation

Validate before calling

n = int(n)
if n < 1:
    raise ValueError('n must be >= 1')
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

When it happens

Trigger: After the syntax fix: solution(0) or solution(-100). Today: importing project_euler/problem_003/sol3.py under Python 3 exits with SyntaxError.

Common situations: Zero default parameters in generic runners; negative n from unvalidated CLI args; the same py3 migration that breaks all three problem_003 solutions at once, which can mask this check entirely.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/575b347c2338f529. Report an issue: GitHub.