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_005/sol1.py:solution when n <= 0 after int conversion; the smallest-multiple (LCM 1..n) search requires n >= 1. CAVEAT: the file's py2 except syntax makes it a SyntaxError under Python 3 at import, so this ValueError cannot fire until the syntax is fixed at line 51.

Source

Thrown at project_euler/problem_005/sol1.py:53

    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 = 0
    while 1:
        i += n * (n - 1)
        nfound = 0
        for j in range(2, n):
            if i % j != 0:
                nfound = 1
                break
        if nfound == 0:
            if i == 0:
                i = 1
            return i
    return None


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

View on GitHub (pinned to f5988cc097)

Solutions

  1. Fix the except clause at project_euler/problem_005/sol1.py:51 to except (TypeError, ValueError):
  2. Pass n >= 1 (n=1 returns 1, n=20 gives the classic answer 232792560).
  3. Skip non-positive bounds in sweeps rather than calling with them.
  4. Catch ValueError in generic drivers that enumerate n.

Example fix

# before
for n in range(0, 21):
    solution(n)  # n=0 raises

# after
for n in range(1, 21):
    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_lcm_bound(n) -> bool:
    return isinstance(n, int) and not isinstance(n, bool) and n >= 1

Try / catch

try:
    solution(n)
except ValueError:
    ...

Prevention

When it happens

Trigger: After the fix: solution(0) or solution(-3). Today: any import under Python 3 raises SyntaxError first.

Common situations: n=0 sentinels meaning 'use default'; range loops starting at 0 in benchmarks; the same py3 migration breakage shared with errors 837/839.

Related errors


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