#!/usr/bin/env python3 """ constants.py -- programme 0011, TASK 4: the measured (K') constant. F(N) = sum_{p None: Ns = [10 ** 4, 10 ** 5, 10 ** 6, 10 ** 7, 10 ** 8] pr = primes_upto(max(Ns) // 2) rows = [] for N in Ns: inv = 1.0 / pr.astype(np.float64) pref = np.concatenate(([0.0], np.cumsum(inv))) small = pr[pr <= isqrt(N)] ic = np.searchsorted(pr, N // small, side="right") ip = np.searchsorted(pr, small, side="right") S = float(np.sum((1.0 / small) * (pref[ic] - pref[ip]))) A = floor_sum(pr, N) F = float(N) * S - float(A) rows.append((N, log(N), log(log(N)), F)) print("== TASK 4: the (K') law F = C_F * N loglog N/log N (1+O(1/logN)) ==") print(f"{'N':>11} {'F':>14} {'F/N':>9} {'ratio=F L/(N m)':>16}") for N, L, m, F in rows: print(f"{N:>11} {F:>14.1f} {F/N:>9.5f} {F*L/(N*m):>16.5f}") print() print("== residual-stability test: C1(C) = (C - ratio)*L must be flat at the true C ==") for C in (0.5, 0.4696, 0.1544, 1.0): c1 = np.array([(C - F * L / (N * m)) * L for N, L, m, F in rows]) print(f" C = {C:<7}: C1 = {[round(v,4) for v in c1]} " f"mean {c1.mean():>8.4f} scatter {c1.std():>7.4f}") print(" -> only C = 0.4696 gives a flat C1; C = 1/2 drifts by 0.21 across the range,") print(" so the accessible range has not yet reached the asymptotic plateau.") print() print("== 3-parameter fit of the same data ==") X = np.vstack([np.ones(len(rows)), np.array([1 / L for _, L, _, _ in rows]), np.array([m / L ** 2 for _, L, m, _ in rows])]).T y = np.array([F * L / (N * m) for N, L, m, F in rows]) coef, *_ = np.linalg.lstsq(X, y, rcond=None) print(f" F L/(N m) = {coef[0]:.5f} + {coef[1]:.5f}/L + {coef[2]:.5f} m/L^2 " f"(rms {np.sqrt(np.mean((X @ coef - y) ** 2)):.2e})") print(f" -> C_F = {coef[0]:.4f} (collinear basis; the stability test above is the" f" reliable read)") print() print("== the 0009/0010 normalisation ==") print(" c L/(N m) = 1.10 for N in [1e5, 1e8] (Landau), and F/c = C_F/(c L/(N m)):") prs = primes_upto(max(Ns) // 2) for N, L, m, F in rows[-4:]: small = prs[prs <= isqrt(N)] c = int((np.searchsorted(prs, N // small, side="right") - np.searchsorted(prs, small, side="right")).sum()) print(f" N = {N:>11}: F/c = {F/c:.5f} c L/(N m) = {c*L/(N*m):.5f}") print(" -> 0.426 is NOT a separate constant: it is C_F/(c L/(N m)) with c L/(N m) = 1.10.") print(" The 1.10 itself is the Landau constant; 0.426 = 0.4696/1.102.") if __name__ == "__main__": main()