TheAlgorithms/Python · error · ValueError

That number is larger than our acceptable range.

Error message

That number is larger than our acceptable range.

What it means

Raised by project_euler/problem_004/sol1.py:solution(n=998001) when the downward scan from n-1 to 10000 finds no palindromic number that is a product of two 3-digit numbers. The smallest such palindrome is 101*101 = 10201, so any n <= 10201 exhausts the loop and raises. The message ('That number is larger than our acceptable range') is misleading - it actually means n is too SMALL, not too large.

Source

Thrown at project_euler/problem_004/sol1.py:47

        ...
    ValueError: That number is larger than our acceptable range.
    """

    # fetches the next number
    for number in range(n - 1, 9999, -1):
        str_number = str(number)

        # checks whether 'str_number' is a palindrome.
        if str_number == str_number[::-1]:
            divisor = 999

            # if 'number' is a product of two 3-digit numbers
            # then number is the answer otherwise fetch next number.
            while divisor != 99:
                if (number % divisor == 0) and (len(str(number // divisor)) == 3.0):
                    return number
                divisor -= 1
    raise ValueError("That number is larger than our acceptable range.")


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

View on GitHub (pinned to f5988cc097)

Solutions

  1. Call with n > 10201 (the smallest 3-digit-product palindrome plus one); the default 998001 works.
  2. Read the error correctly: it means 'no answer exists at or below n', so increase n - do not decrease it.
  3. Catch ValueError when sweeping n values and treat it as 'no palindrome in range'.
  4. Optionally patch the message locally to 'No palindromic product of two 3-digit numbers exists below n' to reduce confusion.

Example fix

# before
print(solution(10000))  # ValueError: That number is larger than our acceptable range.

# after
try:
    print(solution(n))
except ValueError:
    print(f'no 3-digit palindrome product below {n}; need n > 10201')
Defensive patterns

Strategy: validation

Validate before calling

MIN_N = 10201 + 1  # smallest palindrome product of two 3-digit numbers, plus 1
if n <= MIN_N:
    raise ValueError(f'n must be > 10201 for a 3-digit palindrome product, got {n}')
solution(n)

Type guard

def has_palindrome_in_range(n: int) -> bool:
    return isinstance(n, int) and n > 10201

Try / catch

try:
    solution(n)
except ValueError as e:
    if 'acceptable range' in str(e):
        # means n too small, despite the message wording
        ...

Prevention

When it happens

Trigger: solution(10000) (per the doctest), or any n <= 10201 such as solution(0) or solution(10201). Note solution(10201) itself raises because the range starts at n-1 exclusive of a match at exactly 10201 only if it divides out - the safe minimum is n > 10201.

Common situations: Parameterized sweeps over n that start too low; misreading the message and raising the bound (the opposite of the fix); unit tests generated from the doctest that re-use solution(10000) expecting failure; changing the default 998001 (999*999) to a smaller limit while testing.

Related errors


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