TheAlgorithms/Python · error · ValueError

Magic {gon_side}-gon ring is impossible

Error message

Magic {gon_side}-gon ring is impossible

What it means

Raised by solution() in project_euler/problem_068/sol1.py when no permutation of the 2*gon_side numbers forms a magic gon ring. For the supported range [3, 5] magic rings are known to exist, so this branch is effectively a defensive assertion: reaching it means the permutation logic or is_magic_gon was changed/broken, not that the input is bad.

Source

Thrown at project_euler/problem_068/sol1.py:77

    >>> solution(6)
    Traceback (most recent call last):
    ValueError: gon_side must be in the range [3, 5]
    """
    if gon_side < 3 or gon_side > 5:
        raise ValueError("gon_side must be in the range [3, 5]")

    # Since it's 16, we know 10 is on the outer ring
    # Put the big numbers at the end so that they are never the first number
    small_numbers = list(range(gon_side + 1, 0, -1))
    big_numbers = list(range(gon_side + 2, gon_side * 2 + 1))

    for perm in permutations(small_numbers + big_numbers):
        numbers = generate_gon_ring(gon_side, list(perm))
        if is_magic_gon(numbers):
            return int("".join(str(n) for n in numbers))

    msg = f"Magic {gon_side}-gon ring is impossible"
    raise ValueError(msg)


def generate_gon_ring(gon_side: int, perm: list[int]) -> list[int]:
    """
    Generate a gon_side-gon ring from a permutation state
    The permutation state is the ring, but every duplicate is removed

    >>> generate_gon_ring(3, [4, 2, 3, 5, 1, 6])
    [4, 2, 3, 5, 3, 1, 6, 1, 2]
    >>> generate_gon_ring(5, [6, 5, 4, 3, 2, 1, 7, 8, 9, 10])
    [6, 5, 4, 3, 4, 2, 1, 2, 7, 8, 7, 9, 10, 9, 5]
    """
    result = [0] * (gon_side * 3)
    result[0:3] = perm[0:3]
    perm.append(perm[1])

    magic_number = 1 if gon_side < 5 else 2

View on GitHub (pinned to f5988cc097)

Solutions

  1. Restore the original generate_gon_ring/is_magic_gon logic if you edited them (check git diff).
  2. Verify with the doctests: python -m doctest project_euler/problem_068/sol1.py -v.
  3. Keep gon_side within [3, 5]; the error is a canary for internal logic bugs, not bad user input.
Defensive patterns

Strategy: try-catch

Try / catch

try:
    answer = solution(gon_side)
except ValueError as e:
    if "impossible" in str(e):
        # internal invariant broke: logic was modified
        logger.exception("gon search exhausted; check recent edits")
        raise
    raise

Prevention

When it happens

Trigger: Practically unreachable via public input since gon_side is already constrained to [3, 5] and solutions exist for all three. It can fire if someone modifies generate_gon_ring or is_magic_gon, or relaxes the range guard without checking existence.

Common situations: Contributors editing the permutation order or ring-sum logic during refactors; local experiments that reorder big/small number lists; regression testing after touching is_magic_gon.

Related errors


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