"""Check: Weingartner's B'(x) insertions combined with halving keep the lcm property, and the gains add."""
import sys, math
from math import gcd
x = int(sys.argv[1])
spf = list(range(x + 1))
for i in range(2, int(x ** 0.5) + 1):
    if spf[i] == i:
        for j in range(i * i, x + 1, i):
            if spf[j] == j: spf[j] = i
F = [1] * (x + 1)
for n in range(2, x + 1):
    p = spf[n]; F[n] = max(F[n // p], p * n)
def pf(n):
    s = set()
    while n > 1: s.add(spf[n]); n //= spf[n]
    return s
B = [n for n in range(2, x + 1) if F[n] > x and all(F[n // q] <= x for q in pf(n))]
Bp = set(B)
for n in B:
    if 6 * n > x and 5 * n <= x:
        Bp.discard(n); Bp |= {2 * n, 3 * n, 5 * n}
    elif 12 * n > x and 11 * n <= x:
        Bp.discard(n); Bp |= {3 * n, 4 * n, 5 * n, 7 * n, 11 * n}
E = {c for c in B if c % 2 == 0 and 3 * c > 2 * x}
assert E <= Bp
S2 = (Bp - E) | {c // 2 for c in E}
assert len(S2) == len(Bp)                      # no collisions
L = math.log(x)
def bad(S):
    S = sorted(S); return sum(1 for i, a in enumerate(S) for b in S[i + 1:] if a * b // gcd(a, b) <= x)
sB, sBp, sE = sum(1/n for n in B), sum(1/n for n in Bp), sum(1/c for c in E)
s2 = sum(1/n for n in S2)
print(f"x={x}: (1-B)L={(1-sB)*L:.4f} (1-B')L={(1-sBp)*L:.4f} gain_E*L={sE*L:.4f} (1-S2)L={(1-s2)*L:.4f}  S2-(B'+E) = {s2-sBp-sE:.2e}")
if x <= 5000: print("bad pairs in B':", bad(Bp), " in S2:", bad(S2), " max(S2) <= x:", max(S2) <= x)
