# IIS-L-26895: density of E = union of [N_k, N_{k+1}) with N_k odd, N = 5-smooth numbers.
# Prints |E ∩ [2^m, 2^{m+1})| / 2^m and that times m*ln2 (should stay bounded if ~1/log X).
import math, bisect
M = 400
L = 2**(M+2)
S = []
p2 = 1
while p2 <= L:
    p3 = p2
    while p3 <= L:
        p5 = p3
        while p5 <= L:
            S.append(p5); p5 *= 5
        p3 *= 3
    p2 *= 2
S.sort()
def E_upto(x):  # |E ∩ [1, x)|
    tot = 0
    for k in range(len(S) - 1):
        a, b = S[k], S[k+1]
        if a >= x: break
        if a & 1: tot += min(b, x) - a
    return tot
# incremental: cumulative E length at each S[k]
cum = [0]
for k in range(len(S) - 1):
    cum.append(cum[-1] + ((S[k+1] - S[k]) if S[k] & 1 else 0))
def E_before(x):
    k = bisect.bisect_right(S, x) - 1  # S[k] <= x
    return cum[k] + ((x - S[k]) if S[k] & 1 else 0)
assert E_before(30) == 10 and all(E_before(x) == E_upto(x) for x in (17, 26, 30, 1000, 12345))
worst = 0
for m in range(4, M + 1, 1):
    X = 2**m
    d = (E_before(2*X) - E_before(X)) / X
    # largest single exceptional interval in [X,2X) relative to X/log X
    big = max(((S[k+1]-S[k]) for k in range(bisect.bisect_left(S, X), bisect.bisect_left(S, 2*X)) if S[k] & 1), default=0)
    r = big * m * math.log(2) / X
    worst = max(worst, r) if m > 20 else worst
    if m % 20 == 0 or m < 12:
        print(f"m={m:3d} dens={d:.6f} dens*logX={d*m*math.log(2):.4f} maxgap*logX/X={r:.4f}")
print("count", len(S), "worst maxgap*logX/X for m>20:", round(worst, 4))
