"""Regenerates every number shown on the Ramanujan EN page and its appendix (mpmath, 60 digits)."""
from mpmath import mp, mpf, sqrt, pi, factorial, ellipk, ellipe, sin, cos, agm, hyp2f1, hyp3f2, rf, nstr, radians, quad
mp.dps = 60
def ram(N):
    s = sum(factorial(4*n)*(1103+26390*n)/(factorial(n)**4*mpf(396)**(4*n)) for n in range(N))
    return 1/(2*sqrt(2)/9801*s)
def correct_digits(x):
    # number of significant digits of pi reproduced by x when both are truncated
    a, b = nstr(pi, 50, strip_zeros=False).replace('.',''), nstr(x, 50, strip_zeros=False).replace('.','')
    k = 0
    while a[k] == b[k]: k += 1
    return k, abs(x-pi)
for N in (1,2,3):
    x = ram(N); k, err = correct_digits(x)
    print(f"Ramanujan {N} term(s): {nstr(x,30)}  |err|={nstr(err,3)}  matching significant digits={k}  decimals={k-1}")
print("pi =", nstr(pi,30))
# Archimedes: published bounds 3 10/71 < pi < 3 1/7, and exact 96-gon values
print("Archimedes published:", nstr(3+mpf(10)/71,8), nstr(3+mpf(1)/7,8))
print("96-gon inscribed/circumscribed:", nstr(96*sin(pi/96),8), nstr(96*mp.tan(pi/96),8))
# Legendre relation, normalised (mpmath ellipk uses m = k^2)
for th in (20,45,60,90,135,160):
    k2 = sin(radians(th)/2)**2; kc2 = 1-k2
    K,E,Kp,Ep = ellipk(k2), ellipe(k2), ellipk(kc2), ellipe(kc2)
    print(f"theta={th}: K E' + E K' - K K' - pi/2 = {nstr(K*Ep+E*Kp-K*Kp-pi/2,3)}")
# Pochhammer identity and 396^4
n=3; print("Pochhammer n=3:", nstr(rf(mpf(1)/4,n)*rf(mpf(1)/2,n)*rf(mpf(3)/4,n)/factorial(n)**3,20), nstr(factorial(4*n)/(factorial(n)**4*256**n),20))
print("256*99^4 == 396^4:", 256*99**4 == 396**4, "  9801 == 99^2:", 9801 == 99**2)
print("2206=2*1103:", 2206==2*1103, " 52780=2*26390:", 52780==2*26390)
# Clausen
x = mpf('0.1'); print("Clausen residual:", nstr(hyp2f1(0.25,0.75,1,x)**2 - hyp3f2(0.25,0.5,0.75,1,1,4*x*(1-x)),3))
# pendulum via AGM
th0 = radians(90); print("K via AGM vs ellipk (90 deg):", nstr(pi/(2*agm(1,cos(th0/2))) - ellipk(sin(th0/2)**2),3))
print("log phi =", nstr(mp.log((1+sqrt(5))/2),8))
print("1/pi =", nstr(1/pi,12))
# Peg (g = 1): release from 3 deg on the long side (L = 1), string wraps a peg leaving length l = 0.25.
L, l, th0 = mpf(1), mpf('0.25'), radians(3)
th1 = mp.acos(1 - L*(1-cos(th0))/l)                     # energy conservation
T_exact = 2*sqrt(L)*ellipk(sin(th0/2)**2) + 2*sqrt(l)*ellipk(sin(th1/2)**2)
print("peg: exact period", nstr(T_exact,6), " small-amplitude pi(sqrt L + sqrt l) =", nstr(pi*(sqrt(L)+sqrt(l)),6), " second-side amplitude deg =", nstr(th1*180/pi,5))
# Gauss mean form of the period
th = radians(60); print("T/(2pi sqrt(L/g)) = 1/M(1,cos(th/2)) check:", nstr(4*ellipk(sin(th/2)**2) - 2*pi/agm(1,cos(th/2)),3))
# Brent-Salamin: correct decimals after each iteration
a, b, t, p = mpf(1), 1/sqrt(2), mpf('0.25'), mpf(1)
out = []
for i in range(4):
    an = (a+b)/2; b = sqrt(a*b); t -= p*(a-an)**2; a = an; p *= 2
    x = (a+b)**2/(4*t); out.append(correct_digits(x)[0]-1)
print("Brent-Salamin correct decimals per iteration:", out)
# Link 2 branch: 2F1(1/2,1/2;1;k2) vs (1-w)^(-1/4) 2F1(1/4,3/4;1;w/(w-1)), w = 4 k2 (1-k2)
for k2 in (mpf('0.09'), mpf('0.49'), mpf('0.8')):
    w = 4*k2*(1-k2)
    lhs = hyp2f1(0.5,0.5,1,k2); rhs = (1-w)**(-0.25)*hyp2f1(0.25,0.75,1,w/(w-1))
    print(f"link 2, k2={k2}: lhs={nstr(lhs,6)} rhs={nstr(rhs,6)} diff={nstr(lhs-rhs,3)}")
