Gosper's algorithm finds a hypergeometric antidifference exactly when one exists full
This proof shows: correctness, termination.
Let K be a field of characteristic 0 (the implementation uses K=Q), and let t be a hypergeometric term with ratio r(n)=t(n+1)/t(n)=num(n)/den(n), where num,den∈K[n] are nonzero. A hypergeometric antidifference of t is a hypergeometric term z with z(n+1)−z(n)=t(n).
Lemma 1 (reduction to a rational equation). t has a hypergeometric antidifference if and only if there is a rational function y∈K(n) with
r(n)y(n+1)−y(n)=1.(1) If y solves (1), then z=yt is an antidifference.
Proof. If y solves (1) and z=yt, then z(n+1)−z(n)=y(n+1)r(n)t(n)−y(n)t(n)=t(n), and z is hypergeometric because z(n+1)/z(n)=r(n)y(n+1)/y(n) is rational. Conversely, let z be a hypergeometric antidifference with ratio ρ=z(n+1)/z(n)∈K(n). Then t=z(ρ−1), and ρ=1 since t=0. So y=z/t=1/(ρ−1) is rational, and yt=z gives (1). □
Lemma 2 (the Gosper form). Lines L1 to L4 terminate, and afterwards
r(n)=b(n)a(n)⋅c(n)c(n+1),gcd(a(n),b(n+h))=1 for every integer h≥0.(2) Proof. After line L1, a/b=r and c=1, so (2) holds in its first part. Suppose an update at line L4 is made with g=gcd(a(n),b(n+h)), so g(n)∣a(n) and g(n−h)∣b(n). Let C(n)=∏i=1hg(n−i). Then C(n+1)/C(n)=g(n)/g(n−h) by telescoping the product, so
b(n)a(n)=b(n)/g(n−h)a(n)/g(n)⋅C(n)C(n+1), and replacing a, b, c by a/g, b/g(n−h), cC keeps the first part of (2). Each update lowers dega by degg≥1, so there are at most degnum updates, and the loops stop.
For the coprimality, note that a and b are only ever divided, so their roots are among the roots of num and den (in an algebraic closure). If gcd(a(n),b(n+h))=1 for some integer h≥0, there is a root α of a with b(α+h)=0, so h=β−α for a root β of b, and h≤∣α∣+∣β∣≤H by Fujiwara's bound, which is what root_bound computes. So only h≤H can fail. The loop of line L2 visits each such h, and the inner loop at line L3 runs until gcd(a(n),b(n+h))=1. Later updates divide a and b further, which cannot create a common factor of a(n) and b(n+h′) for an h′ already visited. □
Lemma 3 (the polynomial equation). With a,b,c as in (2) and B(n)=b(n−1), a rational y solves (1) if and only if y=B(n)x(n)/c(n) for a polynomial x with
a(n)x(n+1)−B(n)x(n)=c(n).(3) Proof. Put x=cy/B, a rational function. Substituting y=Bx/c and r=(a/b)(c(n+1)/c(n)) into (1):
b(n)a(n)c(n)c(n+1)⋅c(n+1)b(n)x(n+1)−c(n)b(n−1)x(n)=1, which after multiplying by c(n) is (3). So (1) and (3) are equivalent; it remains to show that a rational solution x of (3) is a polynomial.
Write x=f/g in lowest terms with g monic, and suppose degg≥1. Multiplying (3) by g(n)g(n+1) gives
a(n)f(n+1)g(n)−B(n)f(n)g(n+1)=c(n)g(n)g(n+1). Hence g(n+1) divides a(n)f(n+1)g(n), and since gcd(f(n+1),g(n+1))=1, g(n+1)∣a(n)g(n). In the same way g(n)∣B(n)g(n+1).
Let S be the finite set of roots of g in an algebraic closure. Choose γ∈S and m≥0 such that γ,γ+1,…,γ+m∈S, γ−1∈/S and γ+m+1∈/S; this exists because S is finite and nonempty. The point n=γ−1 is a root of g(n+1) but not of g(n), so by g(n+1)∣a(n)g(n) it is a root of a: a(γ−1)=0. The point n=γ+m is a root of g(n) but not of g(n+1), so by g(n)∣B(n)g(n+1) it is a root of B: b(γ+m−1)=0. Then a(n) and b(n+m) have the common root n=γ−1, which contradicts (2) with h=m≥0. So g=1 and x is a polynomial. □
Lemma 4 (degree bound). Every polynomial solution x of (3) has degree at most D, the value computed at line L5.
Proof. Since c=0, x=0; let e=degx and x=xene+⋯ with xe=0. Write d1=dega, d2=degB, λa=LC(a) and λB=LC(B). Note x(n+1)=xene+(exe+xe−1)ne−1+⋯.
- If d1=d2, the left side of (3) has degree exactly max(d1,d2)+e, with leading coefficient λaxe or −λBxe. So e=degc−max(d1,d2).
- If d1=d2=d and λa=λB, the coefficient of nd+e is (λa−λB)xe=0, so e=degc−d.
- If d1=d2=d and λa=λB=λ, the coefficient of nd+e cancels. Let α and β be the coefficients of nd−1 in a and B (0 if d=0). The coefficient of nd+e−1 is λ(exe+xe−1)+αxe−λxe−1−βxe=xe(λe+α−β). If e=(β−α)/λ, this is nonzero, so d+e−1=degc and e=degc−d+1. Otherwise e=(β−α)/λ, which is then a nonnegative integer.
In each case e≤D, as computed by degree_bound. □
Theorem. GOSPER(num,den) terminates. It returns a rational y that solves (1) if such a y exists, and returns None otherwise. In the first case z=yt is a hypergeometric antidifference of t; in the second, t has none.
Proof. Termination: Lemma 2 for lines L1 to L4, and the rest is straight-line code with one linear system. By Lemma 2 the form (2) holds at line L5. If D<0, Lemma 4 shows that (3) has no polynomial solution, and line L6 returns None. Otherwise line L7 sets up (3) as a linear system for the coefficients x0,…,xD, which by Lemma 4 contains every polynomial solution; line L8 returns None exactly when there is none. By Lemma 3, (1) has a rational solution exactly when (3) has a polynomial one, and line L9 returns y=Bx/c, reduced, which solves (1). Lemma 1 turns both answers into the statements about antidifferences. ■