TheAlgorithms/Python · error · ValueError

Open Knight Tour cannot be performed on a board of size {n}

Error message

Open Knight Tour cannot be performed on a board of size {n}

What it means

Raised by casimir_force() in physics/casimir_effect.py when the distance (plate separation) argument is negative. Plate separation enters the formula as distance**4, so a sign is meaningless there; the library rejects negative separations outright. The check fires after the force check, before the area check.

Source

Thrown at backtracking/knight_tour.py:95

    [[1]]

    >>> open_knight_tour(2)
    Traceback (most recent call last):
        ...
    ValueError: Open Knight Tour cannot be performed on a board of size 2
    """

    board = [[0 for i in range(n)] for j in range(n)]

    for i in range(n):
        for j in range(n):
            board[i][j] = 1
            if open_knight_tour_helper(board, (i, j), 1):
                return board
            board[i][j] = 0

    msg = f"Open Knight Tour cannot be performed on a board of size {n}"
    raise ValueError(msg)


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass the absolute plate separation: abs(distance) when the sign is only positional.
  2. Validate distance > 0 in your data pipeline before calling.
  3. Catch ValueError and reject the offending record if parsing external measurements.

Example fix

# before
casimir_force(force=3e-12, area=0, distance=gap_offset)  # gap_offset = -0.00344

# after
casimir_force(force=3e-12, area=0, distance=abs(gap_offset))
Defensive patterns

Strategy: validation

Validate before calling

if distance < 0:
    raise ValueError(f"plate separation must be >= 0, got {distance}")
casimir_force(force=f, area=a, distance=distance)

Try / catch

try:
    casimir_force(force=f, area=a, distance=d)
except ValueError as e:
    if "Distance" in str(e):
        casimir_force(force=f, area=a, distance=abs(d))
    else:
        raise

Prevention

When it happens

Trigger: casimir_force(force=3457e-12, area=0, distance=-0.00344); passing a displacement relative to a reference point that can be negative on one side.

Common situations: Distance measured as a signed offset from an origin (e.g. interferometer zero position) rather than an absolute plate separation; unit-conversion code that flips sign.

Related errors


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