TheAlgorithms/Python · error · ValueError

k must not be negative

Error message

k must not be negative

What it means

Raised by capture_radii() in physics/basic_orbital_capture.py when projectile_velocity exceeds c (the speed of light, ~2.998e8 m/s). Newtonian capture physics is invalid at or above light speed, so the library refuses the input rather than returning a wrong answer. Note the check is strictly greater-than: exactly c is accepted even though that is itself unphysical.

Source

Thrown at backtracking/all_combinations.py:55

        ...
    ValueError: n must not be negative
    >>> generate_all_combinations(n=5, k=4)
    [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]]
    >>> generate_all_combinations(n=3, k=3)
    [[1, 2, 3]]
    >>> generate_all_combinations(n=3, k=1)
    [[1], [2], [3]]
    >>> generate_all_combinations(n=1, k=0)
    [[]]
    >>> generate_all_combinations(n=1, k=1)
    [[1]]
    >>> from itertools import combinations
    >>> all(generate_all_combinations(n, k) == combination_lists(n, k)
    ...     for n in range(1, 6) for k in range(1, 6))
    True
    """
    if k < 0:
        raise ValueError("k must not be negative")
    if n < 0:
        raise ValueError("n must not be negative")

    result: list[list[int]] = []
    create_all_state(1, n, k, [], result)
    return result


def create_all_state(
    increment: int,
    total_number: int,
    level: int,
    current_list: list[int],
    total_list: list[list[int]],
) -> None:
    """
    Helper function to recursively build all combinations.

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert the velocity to m/s before calling (multiply km/s by 1000).
  2. Cap or reject velocities >= c in your own pipeline before invoking the function.
  3. If you genuinely need relativistic capture, use a relativistic model; this library will not accept such inputs.

Example fix

# before
capture_radii(6.957e8, 1.99e30, 3e8 + 1)  # velocity above c -> ValueError

# after
velocity_ms = velocity_kms * 1000
assert velocity_ms < 299_792_458
capture_radii(6.957e8, 1.99e30, velocity_ms)
Defensive patterns

Strategy: validation

Validate before calling

C = 299_792_458.0
if projectile_velocity >= C:
    raise ValueError("velocity at/above c is outside the Newtonian model")
r = capture_radii(mass, radius, projectile_velocity)

Try / catch

try:
    r = capture_radii(m, R, v)
except ValueError as e:
    if "speed of light" in str(e):
        v = v / 1000  # km/s -> m/s fixup, then retry once
        r = capture_radii(m, R, v)
    else:
        raise

Prevention

When it happens

Trigger: Passing projectile_velocity in km/s instead of m/s (e.g. 30000 km/s = 3e7... but 3e8+ values), or simulating relativistic projectiles (velocity = 3e8 m/s) without realizing this is a Newtonian model.

Common situations: Unit mismatch: velocity supplied in km/s while the function expects m/s is the classic case (values above 2.998e5 km/s trip it). Also attempting sci-fi or particle-physics speed regimes with a classical formula.

Related errors


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