# verify3.py
"""Exact verification for three OEIS D-finite conjectures (R. J. Mathar).
Each proof: (1) symbolic identity for the generating function / ratio,
(2) the recurrence checked against terms computed from the definition."""
from sympy import (symbols, exp, Rational, series, factorial, simplify, binomial,
                   gammasimp, combsimp, log, sqrt, diff, Poly, S)
x, n, m = symbols('x n m')
N = 60

def egf_terms(f, N):
    s = series(f, x, 0, N).removeO()
    return [s.coeff(x, k) * factorial(k) for k in range(N)]

# ---- A094822: e.g.f. exp(3x)/(1-3x)^(1/3)
f = exp(3*x) * (1 - 3*x)**Rational(-1, 3)
assert simplify((1 - 3*x)*diff(f, x) - (4 - 9*x)*f) == 0   # ODE
a = egf_terms(f, N)
assert all(a[k] - (3*k + 1)*a[k-1] + 9*(k-1)*a[k-2] == 0 for k in range(2, N))
print("A094822 first terms", a[:8], "ODE ok, recurrence ok for n<%d" % N)

# ---- A053532: e.g.f. (1-x)^(-1/2) exp(-x/2 - x^2/4 - x^3/6)
f = (1 - x)**Rational(-1, 2) * exp(-x/2 - x**2/4 - x**3/6)
assert simplify(2*(1 - x)*diff(f, x) - x**3*f) == 0        # ODE
a = egf_terms(f, N)
assert all(2*a[k] - 2*(k-1)*a[k-1] - (k-1)*(k-2)*(k-3)*a[k-4] == 0 for k in range(4, N))
print("A053532 first terms", a[:8], "ODE ok, recurrence ok for n<%d" % N)

# ---- A348618: a(n) = [n even] 4^n C(3n/2-1, n) + [n odd] C((3n-1)/2, n) C(3n-1,(3n-1)/2)/C(n-1,(n-1)/2)
def A348618(k):
    if k % 2 == 0:
        return 4**k * binomial(Rational(3*k, 2) - 1, k)
    return binomial((3*k - 1)//2, k) * binomial(3*k - 1, (3*k - 1)//2) / binomial(k - 1, (k - 1)//2)
# even n = 2m: ratio a(2m)/a(2m-2) as gamma expression
ev = lambda t: 4**(2*t) * factorial(3*t - 1) / (factorial(2*t) * factorial(t - 1))
r_even = gammasimp(ev(m) / ev(m - 1))
target_even = 12*(3*(2*m) - 2)*(3*(2*m) - 4) / ((2*m)*(2*m - 1))
assert simplify(r_even - target_even) == 0
# odd n = 2m+1: (3n-1)/2 = 3m+1, (n-1)/2 = m
od = lambda t: (factorial(3*t + 1) / (factorial(2*t + 1) * factorial(t))) * \
               (factorial(6*t + 2) / (factorial(3*t + 1)**2)) / (factorial(2*t) / factorial(t)**2)
r_odd = gammasimp(od(m) / od(m - 1))
k_ = 2*m + 1
target_odd = 12*(3*k_ - 2)*(3*k_ - 4) / (k_*(k_ - 1))
assert simplify(r_odd - target_odd) == 0
a = [A348618(k) for k in range(N)]
assert [k for k in range(2, N) if k*(k-1)*a[k] - 12*(3*k-2)*(3*k-4)*a[k-2] != 0] == [2]  # holds for n>=3 only
print("A348618 first terms", a[:10], "ratio identities ok (m>=2 even, m>=1 odd); recurrence ok for 3<=n<%d, fails at n=2" % N)

# verify_more.py
from sympy import symbols, exp, log, sqrt, series, factorial, simplify, diff, expand, Poly, together, radsimp, cancel
x = symbols('x')
N = 60
# ---- A306948: e.g.f. (1+x) log(1+x) e^x
f = (1 + x)*log(1 + x)*exp(x)
D = lambda k: diff(f, x, k)
ode = (1 + x)*(D(3) - 3*D(2) + 3*D(1) - f) + (D(2) - 2*D(1) + f)
assert simplify(ode) == 0
s = series(f, x, 0, N).removeO(); a = [s.coeff(x, k)*factorial(k) for k in range(N)]
assert all(a[k] + (k-5)*a[k-1] + (-3*k+10)*a[k-2] + 3*(k-3)*a[k-3] + (-k+3)*a[k-4 if k>=4 else 0] == 0 for k in range(3, N))  # n=3: a(-1) has coefficient 0
print("A306948", a[:9], "ODE ok; recurrence ok 4<=n<%d" % N)
# ---- A182959: o.g.f. 2(1+x)^2/(1-2x+sqrt(1-8x))
A = 2*(1 + x)**2/(1 - 2*x + sqrt(1 - 8*x))
xA = x*A
E = diff(xA, x) - x*(7*diff(xA, x) - 3*A) - x**2*(8*diff(xA, x) - 12*A)
E = radsimp(together(E)); E = simplify(E)
print("A182959 E(x) =", E)
s = series(A, x, 0, N).removeO(); a = [s.coeff(x, k) for k in range(N)]
bad = [k for k in range(2, N) if (k+1)*a[k] + (-7*k+3)*a[k-1] + 4*(-2*k+5)*a[k-2] != 0]
print("A182959", a[:9], "recurrence fails at n in", bad)

# verify_a220433.py
from sympy import symbols, Rational, simplify, factor, series, factorial, Function, solve
t, x = symbols('t x')
# y(x) is the compositional inverse of X(t) = t - t^2 + t^3/6, so x = X(y).
X = t - t**2 + t**3/6
P = X.diff(t)                      # dx/dy
yp = 1/P                           # y'  as function of y=t
ypp = -P.diff(t)*yp/P**2           # y'' = d/dx (1/P) = -P'(y) y'/P^2
ode = (4 - 12*X - 9*X**2)*ypp - (6 + 9*X)*yp + t
print("ODE residual in terms of y:", factor(simplify(ode)))
# terms: invert series
N = 25
# iterative reversion: y = x + y^2 - y^3/6
y = 0
for _ in range(N):
    y = series(x + y**2 - y**3/6, x, 0, N).removeO()
a = [y.coeff(x, k)*factorial(k) for k in range(N)]
print(a[:12])
bad = [k for k in range(2, N) if 4*a[k] + 6*(-2*k+3)*a[k-1] - (3*k-5)*(3*k-7)*a[k-2] != 0]
print("recurrence fails at", bad)

# OUTPUT
# A094822 first terms [1, 4, 19, 118, 1021, 12088, 183727, 3389242] ODE ok, recurrence ok for n<60
# A053532 first terms [1, 0, 0, 0, 3, 12, 60, 360] ODE ok, recurrence ok for n<60
# A348618 first terms [1, 2, 16, 140, 1280, 12012, 114688, 1108536, 10813440, 106234700] ratio identities ok (m>=2 even, m>=1 odd); recurrence ok for 3<=n<60, fails at n=2
# A306948 [0, 1, 3, 5, 8, 9, 19, -15, 216] ODE ok; recurrence ok 4<=n<60
# A182959 E(x) = 4*x**3 + 9*x**2 + 6*x + 1
# A182959 [1, 5, 20, 96, 528, 3136, 19584, 126720, 841984] recurrence fails at n in [2, 3]
# ODE residual in terms of y: 2
# [0, 1, 2, 11, 100, 1270, 20720, 413000, 9726640, 264279400, 8137329200, 280012733000]
# recurrence fails at [2]
