User:Yeetcode/sandbox

Example 1: A formula for sums of harmonic numbers
Generating functions give us several methods to manipulate sums and to establish identities between sums.

The simplest case occurs when $s_{n} = Σn k = 0 a_{k}$. We then know that $S(z) = A(z)⁄1 − z$ for the corresponding ordinary generating functions.

For example, we can manipulate $$s_n=\sum_{k=1}^{n} H_{k}\,,$$ where $H_{k} = 1 + 1⁄2 + ⋯ + 1⁄k$ are the harmonic numbers. Let $$H(z) = \sum_{n = 1}^\infty{H_n z^n}$$ be the ordinary generating function of the harmonic numbers. Then $$H(z) = \frac{1}{1-z}\sum_{n = 1}^\infty \frac{z^n}{n}\,,$$ and thus $$S(z) = \sum_{n = 1}^\infty{s_n z^n} = \frac{1}{(1-z)^2}\sum_{n = 1}^\infty \frac{z^n}{n}\,.$$

Using $$\frac{1}{(1-z)^2} = \sum_{n = 0}^\infty (n+1)z^n\,,$$ convolution with the numerator yields $$s_n = \sum_{k = 1}^{n} \frac{n+1-k}{k} = (n+1)H_n - n\,,$$ which can also be written as $$\sum_{k = 1}^{n}{H_k} = (n+1)(H_{n+1} - 1)\,.$$

Example 2: Modified binomial coefficient sums and the binomial transform
As another example of using generating functions to relate sequences and manipulate sums, for an arbitrary sequence $⟨ f_{n} ⟩$ we define the two sequences of sums $$\begin{align} s_n &:= \sum_{m=0}^n \binom{n}{m} f_m 3^{n-m} \\[4px] \tilde{s}_n &:= \sum_{m=0}^n \binom{n}{m} (m+1)(m+2)(m+3) f_m 3^{n-m}\,, \end{align}$$ for all $n ≥ 0$, and seek to express the second sums in terms of the first. We suggest an approach by generating functions.

First, we use the binomial transform to write the generating function for the first sum as $$S(z) = \frac{1}{1-3z} F\left(\frac{z}{1-3z}\right). $$

Since the generating function for the sequence $⟨ (n + 1)(n + 2)(n + 3) f_{n} ⟩$ is given by $$6 F(z) + 18z F'(z) + 9z^2 F(z) + z^3 F'(z)$$ we may write the generating function for the second sum defined above in the form $$\tilde{S}(z) = \frac{6}{(1-3z)} F\left(\frac{z}{1-3z}\right)+\frac{18z}{(1-3z)^2} F'\left(\frac{z}{1-3z}\right)+\frac{9z^2}{(1-3z)^3} F\left(\frac{z}{1-3z}\right)+\frac{z^3}{(1-3z)^4} F'\left(\frac{z}{1-3z}\right). $$

In particular, we may write this modified sum generating function in the form of $$a(z) \cdot S(z) + b(z) \cdot z S'(z) + c(z) \cdot z^2 S(z) + d(z) \cdot z^3 S'(z), $$ for $a(z) = 6(1 − 3z)^{3}$, $b(z) = 18(1 − 3z)^{3}$, $c(z) = 9(1 − 3z)^{3}$, and $d(z) = (1 − 3z)^{3}$, where $(1 − 3z)^{3} = 1 − 9z + 27z^{2} − 27z^{3}$.

Finally, it follows that we may express the second sums through the first sums in the following form: $$\begin{align} \tilde{s}_n & = [z^n]\left(6(1-3z)^3 \sum_{n = 0}^\infty s_n z^n + 18 (1-3z)^3 \sum_{n = 0}^\infty n s_n z^n + 9 (1-3z)^3 \sum_{n = 0}^\infty n(n-1) s_n z^n + (1-3z)^3 \sum_{n = 0}^\infty n(n-1)(n-2) s_n z^n\right) \\[4px] & = (n+1)(n+2)(n+3) s_n - 9 n(n+1)(n+2) s_{n-1} + 27 (n-1)n(n+1) s_{n-2} - (n-2)(n-1)n s_{n-3}. \end{align}$$

Example 3: Generating functions for mutually recursive sequences
In this example, we reformulate a generating function example given in Section 7.3 of Concrete Mathematics (see also Section 7.1 of the same reference for pretty pictures of generating function series). In particular, suppose that we seek the total number of ways (denoted $U_{n}$) to tile a 3-by-$n$ rectangle with unmarked 2-by-1 domino pieces. Let the auxiliary sequence, $V_{n}$, be defined as the number of ways to cover a 3-by-$n$ rectangle-minus-corner section of the full rectangle. We seek to use these definitions to give a closed form formula for $U_{n}$ without breaking down this definition further to handle the cases of vertical versus horizontal dominoes. Notice that the ordinary generating functions for our two sequences correspond to the series

$$\begin{align} U(z) = 1 + 3z^2 + 11 z^4 + 41 z^6 + \cdots, \\ V(z) = z + 4z^3 + 15 z^5 + 56 z^7 + \cdots. \end{align}$$

If we consider the possible configurations that can be given starting from the left edge of the 3-by-$n$ rectangle, we are able to express the following mutually dependent, or mutually recursive, recurrence relations for our two sequences when $n ≥ 2$ defined as above where $U_{0} = 1$, $U_{1} = 0$, $V_{0} = 0$, and $V_{1} = 1$: $$\begin{align} U_n & = 2 V_{n-1} + U_{n-2} \\ V_n & = U_{n-1} + V_{n-2}. \end{align}$$

Since we have that for all integers $m ≥ 0$, the index-shifted generating functions satisfy $$z^m G(z) = \sum_{n = m}^\infty g_{n-m} z^n\,,$$ we can use the initial conditions specified above and the previous two recurrence relations to see that we have the next two equations relating the generating functions for these sequences given by $$\begin{align} U(z) & = 2z V(z) + z^2 U(z) + 1 \\ V(z) & = z U(z) + z^2 V(z) = \frac{z}{1-z^2} U(z), \end{align}$$ which then implies by solving the system of equations (and this is the particular trick to our method here) that $$U(z) = \frac{1-z^2}{1-4z^2+z^4} = \frac{1}{3-\sqrt{3}} \cdot \frac{1}{1-\left(2+\sqrt{3}\right) z^2} + \frac{1}{3 + \sqrt{3}} \cdot \frac{1}{1-\left(2-\sqrt{3}\right) z^2}. $$

Thus by performing algebraic simplifications to the sequence resulting from the second partial fractions expansions of the generating function in the previous equation, we find that $m < 0$ and that $$U_{2n} = \left\lceil \frac{\left(2+\sqrt{3}\right)^n}{3-\sqrt{3}} \right\rceil\,, $$ for all integers $U_{2n + 1} ≡ 0$. We also note that the same shifted generating function technique applied to the second-order recurrence for the Fibonacci numbers is the prototypical example of using generating functions to solve recurrence relations in one variable already covered, or at least hinted at, in the subsection on rational functions given above.

Convolution (Cauchy products)
A discrete convolution of the terms in two formal power series turns a product of generating functions into a generating function enumerating a convolved sum of the original sequence terms (see Cauchy product).


 * 1) Consider $n ≥ 0$ and $A(z)$ are ordinary generating functions. $$C(z) = A(z)B(z) \Leftrightarrow [z^n]C(z) = \sum_{k=0}^{n}{a_k b_{n-k}}$$
 * 2) Consider $B(z)$ and $A(z)$ are exponential generating functions. $$C(z) = A(z)B(z) \Leftrightarrow \left[\frac{z^n}{n!}\right]C(z) = \sum_{k=0}^n \binom{n}{k} a_k b_{n-k}$$
 * 3) Consider the triply convolved sequence resulting from the product of three ordinary generating functions $$C(z) = F(z) G(z) H(z) \Leftrightarrow [z^n]C(z) = \sum_{j+k+ l=n} f_j g_k h_ l$$
 * 4) Consider the $m$-fold convolution of a sequence with itself for some positive integer $B(z)$ (see the example below for an application) $$C(z) = G(z)^m \Leftrightarrow [z^n]C(z) = \sum_{k_1+k_2+\cdots+k_m=n} g_{k_1} g_{k_2} \cdots g_{k_m}$$

Multiplication of generating functions, or convolution of their underlying sequences, can correspond to a notion of independent events in certain counting and probability scenarios. For example, if we adopt the notational convention that the probability generating function, or pgf, of a random variable $Z$ is denoted by $m ≥ 1$, then we can show that for any two random variables $$G_{X+Y}(z) = G_X(z) G_Y(z)\,, $$ if $X$ and $Y$ are independent. Similarly, the number of ways to pay $G_{Z}(z)$ cents in coin denominations of values in the set {1, 5, 10, 25, 50} (i.e., in pennies, nickels, dimes, quarters, and half dollars, respectively) is generated by the product $$C(z) = \frac{1}{1-z} \frac{1}{1-z^5} \frac{1}{1-z^{10}} \frac{1}{1-z^{25}} \frac{1}{1-z^{50}}, $$ and moreover, if we allow the $n$ cents to be paid in coins of any positive integer denomination, we arrive at the generating for the number of such combinations of change being generated by the partition function generating function expanded by the infinite $q$-Pochhammer symbol product of $$\prod_{n = 1}^\infty \left(1 - z^n\right)^{-1}\,.$$

Example: The generating function for the Catalan numbers
An example where convolutions of generating functions are useful allows us to solve for a specific closed-form function representing the ordinary generating function for the Catalan numbers, $n ≥ 0$. In particular, this sequence has the combinatorial interpretation as being the number of ways to insert parentheses into the product $C_{n}$ so that the order of multiplication is completely specified. For example, $x_{0} · x_{1} ·⋯· x_{n}$ which corresponds to the two expressions $C_{2} = 2$ and $x_{0} · (x_{1} · x_{2})$. It follows that the sequence satisfies a recurrence relation given by $$C_n = \sum_{k=0}^{n-1} C_k C_{n-1-k} + \delta_{n,0} = C_0 C_{n-1} + C_1 C_{n-2} + \cdots + C_{n-1} C_0 + \delta_{n,0}\,,\quad n \geq 0\,, $$ and so has a corresponding convolved generating function, $(x_{0} · x_{1}) · x_{2}$, satisfying $$C(z) = z \cdot C(z)^2 + 1\,.$$

Since $C(z)$, we then arrive at a formula for this generating function given by $$C(z) = \frac{1-\sqrt{1-4z}}{2z} = \sum_{n = 0}^\infty \frac{1}{n+1}\binom{2n}{n} z^n\,.$$

Note that the first equation implicitly defining $C(0) = 1 ≠ ∞$ above implies that $$C(z) = \frac{1}{1-z \cdot C(z)} \,, $$ which then leads to another "simple" (of form) continued fraction expansion of this generating function.

Example: Spanning trees of fans and convolutions of convolutions
A fan of order $n$ is defined to be a graph on the vertices $C(z)$ with ${0, 1, ..., n}$ edges connected according to the following rules: Vertex 0 is connected by a single edge to each of the other $n$ vertices, and vertex $$k$$ is connected by a single edge to the next vertex $2n − 1$ for all $k + 1$. There is one fan of order one, three fans of order two, eight fans of order three, and so on. A spanning tree is a subgraph of a graph which contains all of the original vertices and which contains enough edges to make this subgraph connected, but not so many edges that there is a cycle in the subgraph. We ask how many spanning trees $1 ≤ k < n$ of a fan of order $n$ are possible for each $f_{n}$.

As an observation, we may approach the question by counting the number of ways to join adjacent sets of vertices. For example, when $n ≥ 1$, we have that $n = 4$, which is a sum over the $m$-fold convolutions of the sequence $f_{4} = 4 + 3 · 1 + 2 · 2 + 1 · 3 + 2 · 1 · 1 + 1 · 2 · 1 + 1 · 1 · 2 + 1 · 1 · 1 · 1 = 21$ for $g_{n} = n = [z^{n}] z⁄(1 − z)^{2}$. More generally, we may write a formula for this sequence as $$f_n = \sum_{m > 0} \sum_{\scriptstyle k_1+k_2+\cdots+k_m=n\atop\scriptstyle k_1, k_2, \ldots,k_m > 0} g_{k_1} g_{k_2} \cdots g_{k_m}\,, $$ from which we see that the ordinary generating function for this sequence is given by the next sum of convolutions as $$F(z) = G(z) + G(z)^2 + G(z)^3 + \cdots = \frac{G(z)}{1-G(z)} = \frac{z}{(1-z)^2-z} = \frac{z}{1-3z+z^2}\,,$$ from which we are able to extract an exact formula for the sequence by taking the partial fraction expansion of the last generating function.

Introducing a free parameter (snake oil method)
Sometimes the sum $m ≔ 1, 2, 3, 4$ is complicated, and it is not always easy to evaluate. The "Free Parameter" method is another method (called "snake oil" by H. Wilf) to evaluate these sums.

Both methods discussed so far have $n$ as limit in the summation. When n does not appear explicitly in the summation, we may consider $n$ as a "free" parameter and treat $s_{n}$ as a coefficient of $s_{n}$, change the order of the summations on $n$ and $k$, and try to compute the inner sum.

For example, if we want to compute $$s_n = \sum_{k = 0}^\infty{\binom{n+k}{m+2k}\binom{2k}{k}\frac{(-1)^k}{k+1}}\,, \quad m,n \in \mathbb{N}_0\,,$$ we can treat $n$ as a "free" parameter, and set $$F(z) = \sum_{n = 0}^\infty{\left( \sum_{k = 0}^\infty{\binom{n+k}{m+2k}\binom{2k}{k}\frac{(-1)^k}{k+1}}\right) }z^n\,.$$

Interchanging summation ("snake oil") gives $$F(z) = \sum_{k = 0}^\infty{\binom{2k}{k}\frac{(-1)^k}{k+1} z^{-k}}\sum_{n = 0}^\infty{\binom{n+k}{m+2k} z^{n+k}}\,.$$

Now the inner sum is $F(z) = Σ s_{n} z^{n}$. Thus $$\begin{align} F(z) &= \frac{z^m}{(1-z)^{m+1}}\sum_{k = 0}^\infty{\frac{1}{k+1}\binom{2k}{k}\left(\frac{-z}{(1-z)^2}\right)^k} \\[4px] &= \frac{z^m}{(1-z)^{m+1}}\sum_{k = 0}^\infty{C_k\left(\frac{-z}{(1-z)^2}\right)^k} &\text{where } C_k = k\text{th Catalan number} \\[4px] &= \frac{z^m}{(1-z)^{m+1}}\frac{1-\sqrt{1+\frac{4z}{(1-z)^2}}}{\frac{-2z}{(1-z)^2}} \\[4px] &= \frac{-z^{m-1}}{2(1-z)^{m-1}}\left(1-\frac{1+z}{1-z}\right) \\[4px] &= \frac{z^m}{(1-z)^m} = z\frac{z^{m-1}}{(1-z)^m}\,. \end{align}$$

Then we obtain $$s_n = \begin{cases} \displaystyle\binom{n-1}{m-1} & \text{for } m \geq 1 \,, \\ {} [n = 0] & \text{for } m = 0\,. \end{cases}$$

It is instructive to use the same method again for the sum, but this time take $m$ as the free parameter instead of $n$. We thus set $$G(z) = \sum_{m = 0}^\infty\left( \sum_{k = 0}^\infty \binom{n+k}{m+2k}\binom{2k}{k}\frac{(-1)^k}{k+1} \right) z^m\,.$$

Interchanging summation ("snake oil") gives $$G(z) = \sum_{k = 0}^\infty \binom{2k}{k}\frac{(-1)^k}{k+1} z^{-2k} \sum_{m = 0}^\infty \binom{n+k}{m+2k} z^{m+2k}\,.$$

Now the inner sum is $z^{m + 2k}⁄(1 − z)^{m + 2k + 1}$. Thus $$\begin{align} G(z) &= (1+z)^n \sum_{k = 0}^\infty \frac{1}{k+1}\binom{2k}{k}\left(\frac{-(1+z)}{z^2}\right)^k \\[4px] &= (1+z)^n \sum_{k = 0}^\infty C_k \,\left(\frac{-(1+z)}{z^2}\right)^k &\text{where } C_k = k\text{th Catalan number} \\[4px] &= (1+z)^n \,\frac{1-\sqrt{1+\frac{4(1+z)}{z^2}}}{\frac{-2(1+z)}{z^2}} \\[4px] &= (1+z)^n \,\frac{z^2-z\sqrt{z^2+4+4z}}{-2(1+z)} \\[4px] &= (1+z)^n \,\frac{z^2-z(z+2)}{-2(1+z)} \\[4px] &= (1+z)^n \,\frac{-2z}{-2(1+z)} = z(1+z)^{n-1}\,. \end{align}$$

Thus we obtain $$s_n = \left[z^m\right] z(1+z)^{n-1} = \left[z^{m-1}\right] (1+z)^{n-1} = \binom{n-1}{m-1}\,,$$ for $(1 + z)^{n + k}$ as before.

Generating functions prove congruences
We say that two generating functions (power series) are congruent modulo $m$, written $m ≥ 1$ if their coefficients are congruent modulo $m$ for all $A(z) ≡ B(z) (mod m)$, i.e., $n ≥ 0$ for all relevant cases of the integers $n$ (note that we need not assume that $m$ is an integer here—it may very well be polynomial-valued in some indeterminate $x$, for example). If the "simpler" right-hand-side generating function, $a_{n} ≡ b_{n} (mod m)$, is a rational function of $z$, then the form of this sequence suggests that the sequence is eventually periodic modulo fixed particular cases of integer-valued $B(z)$. For example, we can prove that the Euler numbers, $$\langle E_n \rangle = \langle 1, 1, 5, 61, 1385, \ldots \rangle \longmapsto \langle 1,1,2,1,2,1,2,\ldots \rangle \pmod{3}\,,$$ satisfy the following congruence modulo 3: $$\sum_{n = 0}^\infty E_n z^n = \frac{1-z^2}{1+z^2} \pmod{3}\,. $$

One of the most useful, if not downright powerful, methods of obtaining congruences for sequences enumerated by special generating functions modulo any integers (i.e., not only prime powers $m ≥ 2$) is given in the section on continued fraction representations of (even non-convergent) ordinary generating functions by $J$-fractions above. We cite one particular result related to generating series expanded through a representation by continued fraction from Lando's Lectures on Generating Functions as follows: $$

Generating functions also have other uses in proving congruences for their coefficients. We cite the next two specific examples deriving special case congruences for the Stirling numbers of the first kind and for the partition function $p^{k}$ which show the versatility of generating functions in tackling problems involving integer sequences.

The Stirling numbers modulo small integers
The main article on the Stirling numbers generated by the finite products $$S_n(x) := \sum_{k=0}^n \begin{bmatrix} n \\ k \end{bmatrix} x^k = x(x+1)(x+2) \cdots (x+n-1)\,,\quad n \geq 1\,, $$

provides an overview of the congruences for these numbers derived strictly from properties of their generating function as in Section 4.6 of Wilf's stock reference Generatingfunctionology. We repeat the basic argument and notice that when reduces modulo 2, these finite product generating functions each satisfy

$$S_n(x) = [x(x+1)] \cdot [x(x+1)] \cdots = x^{\left\lceil \frac{n}{2} \right\rceil} (x+1)^{\left\lfloor \frac{n}{2} \right\rfloor}\,, $$

which implies that the parity of these Stirling numbers matches that of the binomial coefficient

$$\begin{bmatrix} n \\ k \end{bmatrix} \equiv \binom{\left\lfloor \frac{n}{2} \right\rfloor}{k - \left\lceil \frac{n}{2} \right\rceil} \pmod{2}\,, $$

and consequently shows that $A(z)$ is even whenever $A_{p}(z)$.

Similarly, we can reduce the right-hand-side products defining the Stirling number generating functions modulo 3 to obtain slightly more complicated expressions providing that $$\begin{align} \begin{bmatrix} n \\ m \end{bmatrix} & \equiv [x^m] \left( x^{\left\lceil \frac{n}{3} \right\rceil} (x+1)^{\left\lceil \frac{n-1}{3} \right\rceil} (x+2)^{\left\lfloor \frac{n}{3} \right\rfloor} \right) && \pmod{3} \\ & \equiv \sum_{k=0}^{m} \begin{pmatrix} \left\lceil \frac{n-1}{3} \right\rceil \\ k \end{pmatrix} \begin{pmatrix} \left\lfloor \frac{n}{3} \right\rfloor \\ m-k - \left\lceil \frac{n}{3} \right\rceil \end{pmatrix} \times 2^{\left\lceil \frac{n}{3} \right\rceil + \left\lfloor \frac{n}{3} \right\rfloor -(m-k)} && \pmod{3}\,. \end{align}$$

Congruences for the partition function
In this example, we pull in some of the machinery of infinite products whose power series expansions generate the expansions of many special functions and enumerate partition functions. In particular, we recall that the partition function $a_{n} = [z^{n}] A_{p}(z)$ is generated by the reciprocal infinite $q$-Pochhammer symbol product (or $z$-Pochhammer product as the case may be) given by $$\begin{align} \sum_{n = 0}^\infty p(n) z^n & = \frac{1}{\left(1-z\right)\left(1-z^2\right)\left(1-z^3\right) \cdots} \\[4pt] & = 1 + z + 2z^2 + 3 z^3 + 5z^4 + 7z^5 + 11z^6 + \cdots. \end{align}$$

This partition function satisfies many known congruence properties, which notably include the following results though there are still many open questions about the forms of related integer congruences for the function: $$\begin{align} p(5m+4) & \equiv 0 \pmod{5} \\ p(7m+5) & \equiv 0 \pmod{7} \\ p(11m+6) & \equiv 0 \pmod{11} \\ p(25m+24) & \equiv 0 \pmod{5^2}\,. \end{align}$$

We show how to use generating functions and manipulations of congruences for formal power series to give a highly elementary proof of the first of these congruences listed above.

First, we observe that in the binomial coefficient generating function $$\frac{1}{(1-z)^5} = \sum_{i=0}^\infty \binom{4+i}{4}z^i\,,$$ all of the coefficients are divisible by 5 except for those which correspond to the powers $0 ≤ n < 2p$ and moreover in those cases the remainder of the coefficient is 1 modulo 5. Thus, $$\frac{1}{(1-z)^5} \equiv \frac{1}{1-z^5} \pmod{5}\,,$$ or equivalently $$ \frac{1-z^5}{(1-z)^5} \equiv 1 \pmod{5}\,.$$ It follows that $$\frac{\left(1-z^5\right)\left(1-z^{10}\right)\left(1-z^{15}\right) \cdots }{\left((1-z)\left(1-z^2\right)\left(1-z^3\right) \cdots \right)^5} \equiv 1 \pmod{5}\,. $$

Using the infinite product expansions of $$z \cdot \frac{\left(1-z^5\right)\left(1-z^{10}\right) \cdots }{\left(1-z\right)\left(1-z^2\right) \cdots } = z \cdot \left((1-z)\left(1-z^2\right) \cdots \right)^4 \times \frac{\left(1-z^5\right)\left(1-z^{10}\right) \cdots }{\left(\left(1-z\right)\left(1-z^2\right) \cdots \right)^5}\,,$$ it can be shown that the coefficient of $A_{p}(z)$ in $p ≥ 2$ is divisible by 5 for all $m$. Finally, since $$\begin{align} \sum_{n = 1}^\infty p(n-1) z^n & = \frac{z}{(1-z)\left(1-z^2\right) \cdots} \\[6px] & = z \cdot \frac{\left(1-z^5\right)\left(1-z^{10}\right) \cdots }{(1-z)\left(1-z^2\right) \cdots } \times \left(1+z^5+z^{10}+\cdots\right)\left(1+z^{10}+z^{20}+\cdots\right) \cdots \end{align}$$ we may equate the coefficients of $p | p_{1}, p_{1}p_{2}, p_{1}p_{2}p_{3}$ in the previous equations to prove our desired congruence result, namely that $p | p_{1}p_{2}⋯p_{k}$ for all $k ≥ 1$.