"""Use the original level equations to check all choices and the exact answer."""
from fractions import Fraction
from math import isclose, log2, sqrt

def width(level, k):
    # t = 2^x. The two endpoints correspond to the two positive roots.
    delta = sqrt(level**2 - 4 * k)
    return log2((level + delta) / (level - delta))

choices = (16, 18, 20, 22, 24)
matched = []
for k in choices:
    difference = width(14, k) - width(10, k)
    print(f"k={k}: width difference = {difference:.10f}")
    if isclose(difference, 2, abs_tol=1e-12):
        matched.append(k)
assert matched == [24]

# A common midpoint and width difference 2 double the larger endpoint's
# exponential and halve the other summand. The two level equations are linear.
large = Fraction(2 * 14 - 10, 3)
small = 10 - large
k = large * small
assert large > small > 0 and k == 24
lower = (small, large)
upper = (small / 2, 2 * large)
for height, (left, right) in ((10, lower), (14, upper)):
    assert left < right and left * right == k
    assert all(t + k / t == height for t in (left, right))
ratio = (upper[1] / upper[0]) / (lower[1] / lower[0])
assert ratio == 4
print(f"2^x endpoints: {lower[0]}, {lower[1]} and {upper[0]}, {upper[1]}")
print(f"exact difference = log2({ratio}) = 2; k = {k}")
