TheAlgorithms/Python · error · ValueError

Parameters chain_length and number_limit must be greater tha

Error message

Parameters chain_length and number_limit must be greater than 0

What it means

Raised by solution() in project_euler/problem_074/sol2.py when chain_length <= 0 or number_limit <= 0. A non-positive chain length makes 'exactly chain_length non-repeating elements' undefined, and number_limit <= 0 leaves the search range range(1, number_limit) empty; both are rejected with ValueError after the isinstance guard.

Source

Thrown at project_euler/problem_074/sol2.py:105

    >>> solution(0, 1000)
    Traceback (most recent call last):
        ...
    ValueError: Parameters chain_length and number_limit must be greater than 0

    >>> solution(10, 0)
    Traceback (most recent call last):
        ...
    ValueError: Parameters chain_length and number_limit must be greater than 0

    >>> solution(10, 1000)
    26
    """

    if not isinstance(chain_length, int) or not isinstance(number_limit, int):
        raise TypeError("Parameters chain_length and number_limit must be int")

    if chain_length <= 0 or number_limit <= 0:
        raise ValueError(
            "Parameters chain_length and number_limit must be greater than 0"
        )

    # the counter for the chains with the exact desired length
    chains_counter = 0
    # the cached sizes of the previous chains
    chain_sets_lengths: dict[int, int] = {}

    for start_chain_element in range(1, number_limit):
        # The temporary set will contain the elements of the chain
        chain_set = set()
        chain_set_length = 0

        # Stop computing the chain when you find a cached size, a repeating item or the
        # length is greater then the desired one.
        chain_element = start_chain_element
        while (
            chain_element not in chain_sets_lengths

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass both parameters > 0: solution(60, 1000000) for the canonical problem.
  2. Validate config before calling: if chain_length <= 0 or number_limit <= 0: raise.
  3. Fix the upstream computation that zeroed number_limit.

Example fix

# before
number_limit = upper - lower  # 0 when lower >= upper
result = solution(60, number_limit)

# after
if number_limit <= 0:
    raise ValueError(f"number_limit must be > 0, got {number_limit}")
result = solution(60, number_limit)
Defensive patterns

Strategy: validation

Validate before calling

if chain_length <= 0 or number_limit <= 0:
    raise ValueError(
        f"need chain_length > 0 and number_limit > 0, "
        f"got {chain_length}, {number_limit}"
    )
solution(chain_length, number_limit)

Type guard

def are_positive_ints(*values) -> bool:
    return all(isinstance(v, int) and v > 0 for v in values)

Try / catch

try:
    result = solution(chain_length, number_limit)
except ValueError as e:
    if "greater than 0" in str(e):
        raise ConfigError(f"invalid euler-74 params: {e}") from e
    raise

Prevention

When it happens

Trigger: solution(0, 1000), solution(60, 0), solution(-1, -1), or a computed number_limit that collapses to 0 (e.g. limit - offset with offset >= limit).

Common situations: Config-driven thresholds defaulting to 0; sweeps that include edge values; arithmetic on limits producing 0 or negatives.

Related errors


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