https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=User%3AYeetcode%2Fsandbox User:Yeetcode/sandbox - Revision history 2025-05-30T12:28:26Z Revision history for this page on the wiki MediaWiki 1.45.0-wmf.3 https://en.wikipedia.org/w/index.php?title=User:Yeetcode/sandbox&diff=1193998437&oldid=prev Yeetcode: Breaking the generating functions page into two. This sandbox will act as a template for the second page. 2024-01-06T18:11:00Z <p>Breaking the generating functions page into two. This sandbox will act as a template for the second page.</p> <p><b>New page</b></p><div>{{User sandbox}}<br /> &lt;!-- EDIT BELOW THIS LINE --&gt;<br /> {{About|Applications of generating functions in mathematics|generating functions in classical mechanics|Generating function (physics)|generators in computer programming|Generator (computer programming)|the moment generating function in statistics|Moment generating function}}<br /> <br /> ==Applications==<br /> <br /> ===Various techniques: Evaluating sums and tackling other problems with generating functions===<br /> <br /> ====Example 1: A formula for sums of harmonic numbers====<br /> <br /> Generating functions give us several methods to manipulate sums and to establish identities between sums.<br /> <br /> The simplest case occurs when {{math|&#039;&#039;s&lt;sub&gt;n&lt;/sub&gt;&#039;&#039; {{=}} Σ{{su|b=&#039;&#039;k&#039;&#039; {{=}} 0|p=&#039;&#039;n&#039;&#039;}} &#039;&#039;a&lt;sub&gt;k&lt;/sub&gt;&#039;&#039;}}. We then know that {{math|&#039;&#039;S&#039;&#039;(&#039;&#039;z&#039;&#039;) {{=}} {{sfrac|&#039;&#039;A&#039;&#039;(&#039;&#039;z&#039;&#039;)|1 − &#039;&#039;z&#039;&#039;}}}} for the corresponding ordinary generating functions.<br /> <br /> For example, we can manipulate<br /> &lt;math display=&quot;block&quot;&gt;s_n=\sum_{k=1}^{n} H_{k}\,,&lt;/math&gt;<br /> where {{math|&#039;&#039;H&lt;sub&gt;k&lt;/sub&gt;&#039;&#039; {{=}} 1 + {{sfrac|1|2}} + ⋯ + {{sfrac|1|&#039;&#039;k&#039;&#039;}}}} are the [[harmonic number]]s. Let<br /> &lt;math display=&quot;block&quot;&gt;H(z) = \sum_{n = 1}^\infty{H_n z^n}&lt;/math&gt;<br /> be the ordinary generating function of the harmonic numbers. Then<br /> &lt;math display=&quot;block&quot;&gt;H(z) = \frac{1}{1-z}\sum_{n = 1}^\infty \frac{z^n}{n}\,,&lt;/math&gt;<br /> and thus<br /> &lt;math display=&quot;block&quot;&gt;S(z) = \sum_{n = 1}^\infty{s_n z^n} = \frac{1}{(1-z)^2}\sum_{n = 1}^\infty \frac{z^n}{n}\,.&lt;/math&gt;<br /> <br /> Using<br /> &lt;math display=&quot;block&quot;&gt;\frac{1}{(1-z)^2} = \sum_{n = 0}^\infty (n+1)z^n\,,&lt;/math&gt;<br /> [[Generating function#Convolution (Cauchy products)|convolution]] with the numerator yields<br /> &lt;math display=&quot;block&quot;&gt;s_n = \sum_{k = 1}^{n} \frac{n+1-k}{k} = (n+1)H_n - n\,,&lt;/math&gt;<br /> which can also be written as<br /> &lt;math display=&quot;block&quot;&gt;\sum_{k = 1}^{n}{H_k} = (n+1)(H_{n+1} - 1)\,.&lt;/math&gt;<br /> <br /> ====Example 2: Modified binomial coefficient sums and the binomial transform====<br /> <br /> As another example of using generating functions to relate sequences and manipulate sums, for an arbitrary sequence {{math|⟨ &#039;&#039;f&lt;sub&gt;n&lt;/sub&gt;&#039;&#039; ⟩}} we define the two sequences of sums<br /> &lt;math display=&quot;block&quot;&gt;\begin{align}<br /> s_n &amp;:= \sum_{m=0}^n \binom{n}{m} f_m 3^{n-m} \\[4px]<br /> \tilde{s}_n &amp;:= \sum_{m=0}^n \binom{n}{m} (m+1)(m+2)(m+3) f_m 3^{n-m}\,,<br /> \end{align}&lt;/math&gt;<br /> for all {{math|&#039;&#039;n&#039;&#039; ≥ 0}}, and seek to express the second sums in terms of the first. We suggest an approach by generating functions.<br /> <br /> First, we use the [[binomial transform]] to write the generating function for the first sum as<br /> &lt;math display=&quot;block&quot;&gt;S(z) = \frac{1}{1-3z} F\left(\frac{z}{1-3z}\right). &lt;/math&gt;<br /> <br /> Since the generating function for the sequence {{math|⟨ (&#039;&#039;n&#039;&#039; + 1)(&#039;&#039;n&#039;&#039; + 2)(&#039;&#039;n&#039;&#039; + 3) &#039;&#039;f&lt;sub&gt;n&lt;/sub&gt;&#039;&#039; ⟩}} is given by<br /> &lt;math display=&quot;block&quot;&gt;6 F(z) + 18z F&#039;(z) + 9z^2 F&#039;&#039;(z) + z^3 F&#039;&#039;&#039;(z)&lt;/math&gt;<br /> we may write the generating function for the second sum defined above in the form<br /> &lt;math display=&quot;block&quot;&gt;\tilde{S}(z) = \frac{6}{(1-3z)} F\left(\frac{z}{1-3z}\right)+\frac{18z}{(1-3z)^2} F&#039;\left(\frac{z}{1-3z}\right)+\frac{9z^2}{(1-3z)^3} F&#039;&#039;\left(\frac{z}{1-3z}\right)+\frac{z^3}{(1-3z)^4} F&#039;&#039;&#039;\left(\frac{z}{1-3z}\right). &lt;/math&gt;<br /> <br /> In particular, we may write this modified sum generating function in the form of<br /> &lt;math display=&quot;block&quot;&gt;a(z) \cdot S(z) + b(z) \cdot z S&#039;(z) + c(z) \cdot z^2 S&#039;&#039;(z) + d(z) \cdot z^3 S&#039;&#039;&#039;(z), &lt;/math&gt;<br /> for {{math|&#039;&#039;a&#039;&#039;(&#039;&#039;z&#039;&#039;) {{=}} 6(1 − 3&#039;&#039;z&#039;&#039;)&lt;sup&gt;3&lt;/sup&gt;}}, {{math|&#039;&#039;b&#039;&#039;(&#039;&#039;z&#039;&#039;) {{=}} 18(1 − 3&#039;&#039;z&#039;&#039;)&lt;sup&gt;3&lt;/sup&gt;}}, {{math|&#039;&#039;c&#039;&#039;(&#039;&#039;z&#039;&#039;) {{=}} 9(1 − 3&#039;&#039;z&#039;&#039;)&lt;sup&gt;3&lt;/sup&gt;}}, and {{math|&#039;&#039;d&#039;&#039;(&#039;&#039;z&#039;&#039;) {{=}} (1 − 3&#039;&#039;z&#039;&#039;)&lt;sup&gt;3&lt;/sup&gt;}}, where {{math|(1 − 3&#039;&#039;z&#039;&#039;)&lt;sup&gt;3&lt;/sup&gt; {{=}} 1 − 9&#039;&#039;z&#039;&#039; + 27&#039;&#039;z&#039;&#039;&lt;sup&gt;2&lt;/sup&gt; − 27&#039;&#039;z&#039;&#039;&lt;sup&gt;3&lt;/sup&gt;}}.<br /> <br /> Finally, it follows that we may express the second sums through the first sums in the following form:<br /> &lt;math display=&quot;block&quot;&gt;\begin{align}<br /> \tilde{s}_n &amp; = [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]<br /> &amp; = (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}.<br /> \end{align}&lt;/math&gt;<br /> <br /> ====Example 3: Generating functions for mutually recursive sequences====<br /> <br /> In this example, we reformulate a generating function example given in Section 7.3 of &#039;&#039;Concrete Mathematics&#039;&#039; (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 {{math|&#039;&#039;U&lt;sub&gt;n&lt;/sub&gt;&#039;&#039;}}) to tile a 3-by-{{mvar|n}} rectangle with unmarked 2-by-1 domino pieces. Let the auxiliary sequence, {{math|&#039;&#039;V&lt;sub&gt;n&lt;/sub&gt;&#039;&#039;}}, be defined as the number of ways to cover a 3-by-{{mvar|n}} rectangle-minus-corner section of the full rectangle. We seek to use these definitions to give a [[Closed-form expression|closed form]] formula for {{math|&#039;&#039;U&lt;sub&gt;n&lt;/sub&gt;&#039;&#039;}} 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<br /> <br /> &lt;math display=&quot;block&quot;&gt;\begin{align}<br /> U(z) = 1 + 3z^2 + 11 z^4 + 41 z^6 + \cdots, \\<br /> V(z) = z + 4z^3 + 15 z^5 + 56 z^7 + \cdots. <br /> \end{align}&lt;/math&gt;<br /> <br /> If we consider the possible configurations that can be given starting from the left edge of the 3-by-{{mvar|n}} rectangle, we are able to express the following mutually dependent, or &#039;&#039;mutually recursive&#039;&#039;, recurrence relations for our two sequences when {{math|&#039;&#039;n&#039;&#039; ≥ 2}} defined as above where {{math|&#039;&#039;U&#039;&#039;&lt;sub&gt;0&lt;/sub&gt; {{=}} 1}}, {{math|&#039;&#039;U&#039;&#039;&lt;sub&gt;1&lt;/sub&gt; {{=}} 0}}, {{math|&#039;&#039;V&#039;&#039;&lt;sub&gt;0&lt;/sub&gt; {{=}} 0}}, and {{math|&#039;&#039;V&#039;&#039;&lt;sub&gt;1&lt;/sub&gt; {{=}} 1}}:<br /> &lt;math display=&quot;block&quot;&gt;\begin{align}<br /> U_n &amp; = 2 V_{n-1} + U_{n-2} \\<br /> V_n &amp; = U_{n-1} + V_{n-2}.<br /> \end{align}&lt;/math&gt;<br /> <br /> Since we have that for all integers {{math|&#039;&#039;m&#039;&#039; ≥ 0}}, the index-shifted generating functions satisfy{{noteTag|Incidentally, we also have a corresponding formula when {{math|&#039;&#039;m&#039;&#039; &lt; 0}} given by<br /> &lt;math display=&quot;block&quot;&gt;\sum_{n = 0}^\infty g_{n+m} z^n = \frac{G(z) - g_0 -g_1 z - \cdots - g_{m-1} z^{m-1}}{z^m}\,.&lt;/math&gt;}}<br /> &lt;math display=&quot;block&quot;&gt;z^m G(z) = \sum_{n = m}^\infty g_{n-m} z^n\,,&lt;/math&gt;<br /> 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<br /> &lt;math display=&quot;block&quot;&gt;\begin{align}<br /> U(z) &amp; = 2z V(z) + z^2 U(z) + 1 \\<br /> V(z) &amp; = z U(z) + z^2 V(z) = \frac{z}{1-z^2} U(z),<br /> \end{align}&lt;/math&gt;<br /> which then implies by solving the system of equations (and this is the particular trick to our method here) that<br /> &lt;math display=&quot;block&quot;&gt;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}. &lt;/math&gt;<br /> <br /> 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 {{math|&#039;&#039;U&#039;&#039;&lt;sub&gt;2&#039;&#039;n&#039;&#039; + 1&lt;/sub&gt; ≡ 0}} and that<br /> &lt;math display=&quot;block&quot;&gt;U_{2n} = \left\lceil \frac{\left(2+\sqrt{3}\right)^n}{3-\sqrt{3}} \right\rceil\,, &lt;/math&gt;<br /> for all integers {{math|&#039;&#039;n&#039;&#039; ≥ 0}}. We also note that the same shifted generating function technique applied to the second-order [[recurrence relation|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.<br /> <br /> ===Convolution (Cauchy products)===<br /> <br /> A discrete &#039;&#039;convolution&#039;&#039; 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]]).<br /> <br /> #Consider {{math|&#039;&#039;A&#039;&#039;(&#039;&#039;z&#039;&#039;)}} and {{math|&#039;&#039;B&#039;&#039;(&#039;&#039;z&#039;&#039;)}} are ordinary generating functions. &lt;math display=&quot;block&quot;&gt;C(z) = A(z)B(z) \Leftrightarrow [z^n]C(z) = \sum_{k=0}^{n}{a_k b_{n-k}}&lt;/math&gt;<br /> #Consider {{math|&#039;&#039;A&#039;&#039;(&#039;&#039;z&#039;&#039;)}} and {{math|&#039;&#039;B&#039;&#039;(&#039;&#039;z&#039;&#039;)}} are exponential generating functions. &lt;math display=&quot;block&quot;&gt;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}&lt;/math&gt;<br /> #Consider the triply convolved sequence resulting from the product of three ordinary generating functions &lt;math display=&quot;block&quot;&gt;C(z) = F(z) G(z) H(z) \Leftrightarrow [z^n]C(z) = \sum_{j+k+ l=n} f_j g_k h_ l&lt;/math&gt;<br /> #Consider the {{mvar|m}}-fold convolution of a sequence with itself for some positive integer {{math|&#039;&#039;m&#039;&#039; ≥ 1}} (see the example below for an application) &lt;math display=&quot;block&quot;&gt;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}&lt;/math&gt;<br /> <br /> 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 &#039;&#039;pgf&#039;&#039;, of a random variable {{mvar|Z}} is denoted by {{math|&#039;&#039;G&lt;sub&gt;Z&lt;/sub&gt;&#039;&#039;(&#039;&#039;z&#039;&#039;)}}, then we can show that for any two random variables &lt;ref&gt;{{harvnb|Graham|Knuth|Patashnik|1994|loc=§8.3}}&lt;/ref&gt;<br /> &lt;math display=&quot;block&quot;&gt;G_{X+Y}(z) = G_X(z) G_Y(z)\,, &lt;/math&gt;<br /> if {{mvar|X}} and {{mvar|Y}} are independent. Similarly, the number of ways to pay {{math|&#039;&#039;n&#039;&#039; ≥ 0}} cents in coin denominations of values in the set {1,&amp;nbsp;5,&amp;nbsp;10,&amp;nbsp;25,&amp;nbsp;50} (i.e., in pennies, nickels, dimes, quarters, and half dollars, respectively) is generated by the product<br /> &lt;math display=&quot;block&quot;&gt;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}}, &lt;/math&gt;<br /> and moreover, if we allow the {{mvar|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 (mathematics)|partition function]] generating function expanded by the infinite [[q-Pochhammer symbol|{{mvar|q}}-Pochhammer symbol]] product of<br /> &lt;math display=&quot;block&quot;&gt;\prod_{n = 1}^\infty \left(1 - z^n\right)^{-1}\,.&lt;/math&gt;<br /> <br /> ====Example: The generating function for the Catalan numbers====<br /> <br /> 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]], {{math|&#039;&#039;C&lt;sub&gt;n&lt;/sub&gt;&#039;&#039;}}. In particular, this sequence has the combinatorial interpretation as being the number of ways to insert parentheses into the product {{math|&#039;&#039;x&#039;&#039;&lt;sub&gt;0&lt;/sub&gt; · &#039;&#039;x&#039;&#039;&lt;sub&gt;1&lt;/sub&gt; ·⋯· &#039;&#039;x&lt;sub&gt;n&lt;/sub&gt;&#039;&#039;}} so that the order of multiplication is completely specified. For example, {{math|&#039;&#039;C&#039;&#039;&lt;sub&gt;2&lt;/sub&gt; {{=}} 2}} which corresponds to the two expressions {{math|&#039;&#039;x&#039;&#039;&lt;sub&gt;0&lt;/sub&gt; · (&#039;&#039;x&#039;&#039;&lt;sub&gt;1&lt;/sub&gt; · &#039;&#039;x&#039;&#039;&lt;sub&gt;2&lt;/sub&gt;)}} and {{math|(&#039;&#039;x&#039;&#039;&lt;sub&gt;0&lt;/sub&gt; · &#039;&#039;x&#039;&#039;&lt;sub&gt;1&lt;/sub&gt;) · &#039;&#039;x&#039;&#039;&lt;sub&gt;2&lt;/sub&gt;}}. It follows that the sequence satisfies a recurrence relation given by<br /> &lt;math display=&quot;block&quot;&gt;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\,, &lt;/math&gt;<br /> and so has a corresponding convolved generating function, {{math|&#039;&#039;C&#039;&#039;(&#039;&#039;z&#039;&#039;)}}, satisfying<br /> &lt;math display=&quot;block&quot;&gt;C(z) = z \cdot C(z)^2 + 1\,.&lt;/math&gt;<br /> <br /> Since {{math|&#039;&#039;C&#039;&#039;(0) {{=}} 1 ≠ ∞}}, we then arrive at a formula for this generating function given by<br /> &lt;math display=&quot;block&quot;&gt;C(z) = \frac{1-\sqrt{1-4z}}{2z} = \sum_{n = 0}^\infty \frac{1}{n+1}\binom{2n}{n} z^n\,.&lt;/math&gt;<br /> <br /> Note that the first equation implicitly defining {{math|&#039;&#039;C&#039;&#039;(&#039;&#039;z&#039;&#039;)}} above implies that<br /> &lt;math display=&quot;block&quot;&gt;C(z) = \frac{1}{1-z \cdot C(z)} \,, &lt;/math&gt;<br /> which then leads to another &quot;simple&quot; (of form) continued fraction expansion of this generating function.<br /> <br /> ====Example: Spanning trees of fans and convolutions of convolutions====<br /> <br /> A &#039;&#039;fan of order {{mvar|n}}&#039;&#039; is defined to be a graph on the vertices {{math|{0, 1, ..., &#039;&#039;n&#039;&#039;}&lt;nowiki/&gt;}} with {{math|2&#039;&#039;n&#039;&#039; − 1}} edges connected according to the following rules: Vertex 0 is connected by a single edge to each of the other {{mvar|n}} vertices, and vertex &lt;math&gt;k&lt;/math&gt; is connected by a single edge to the next vertex {{math|&#039;&#039;k&#039;&#039; + 1}} for all {{math|1 ≤ &#039;&#039;k&#039;&#039; &lt; &#039;&#039;n&#039;&#039;}}.&lt;ref&gt;{{harvnb|Graham|Knuth|Patashnik|1994|loc=Example 6 in §7.3}} for another method and the complete setup of this problem using generating functions. This more &quot;convoluted&quot; approach is given in Section 7.5 of the same reference.&lt;/ref&gt; 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 {{math|&#039;&#039;f&lt;sub&gt;n&lt;/sub&gt;&#039;&#039;}} of a fan of order {{mvar|n}} are possible for each {{math|&#039;&#039;n&#039;&#039; ≥ 1}}.<br /> <br /> As an observation, we may approach the question by counting the number of ways to join adjacent sets of vertices. For example, when {{math|&#039;&#039;n&#039;&#039; {{=}} 4}}, we have that {{math|&#039;&#039;f&#039;&#039;&lt;sub&gt;4&lt;/sub&gt; {{=}} 4 + 3 · 1 + 2 · 2 + 1 · 3 + 2 · 1 · 1 + 1 · 2 · 1 + 1 · 1 · 2 + 1 · 1 · 1 · 1 {{=}} 21}}, which is a sum over the {{mvar|m}}-fold convolutions of the sequence {{math|&#039;&#039;g&lt;sub&gt;n&lt;/sub&gt;&#039;&#039; {{=}} &#039;&#039;n&#039;&#039; {{=}} [&#039;&#039;z&lt;sup&gt;n&lt;/sup&gt;&#039;&#039;] {{sfrac|&#039;&#039;z&#039;&#039;|(1 − &#039;&#039;z&#039;&#039;)&lt;sup&gt;2&lt;/sup&gt;}}}} for {{math|&#039;&#039;m&#039;&#039; ≔ 1, 2, 3, 4}}. More generally, we may write a formula for this sequence as<br /> &lt;math display=&quot;block&quot;&gt;f_n = \sum_{m &gt; 0} \sum_{\scriptstyle k_1+k_2+\cdots+k_m=n\atop\scriptstyle k_1, k_2, \ldots,k_m &gt; 0} g_{k_1} g_{k_2} \cdots g_{k_m}\,, &lt;/math&gt;<br /> from which we see that the ordinary generating function for this sequence is given by the next sum of convolutions as<br /> &lt;math display=&quot;block&quot;&gt;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}\,,&lt;/math&gt;<br /> from which we are able to extract an exact formula for the sequence by taking the [[partial fraction expansion]] of the last generating function.<br /> <br /> ===Implicit generating functions and the Lagrange inversion formula===<br /> {{expand section|This section needs to be added to the list of techniques with generating functions|date=April 2017}}<br /> <br /> ===Introducing a free parameter (snake oil method)===<br /> Sometimes the sum {{math|&#039;&#039;s&lt;sub&gt;n&lt;/sub&gt;&#039;&#039;}} is complicated, and it is not always easy to evaluate. The &quot;Free Parameter&quot; method is another method (called &quot;snake oil&quot; by H. Wilf) to evaluate these sums.<br /> <br /> Both methods discussed so far have {{mvar|n}} as limit in the summation. When n does not appear explicitly in the summation, we may consider {{mvar|n}} as a &quot;free&quot; parameter and treat {{math|&#039;&#039;s&lt;sub&gt;n&lt;/sub&gt;&#039;&#039;}} as a coefficient of {{math|&#039;&#039;F&#039;&#039;(&#039;&#039;z&#039;&#039;) {{=}} Σ &#039;&#039;s&lt;sub&gt;n&lt;/sub&gt;&#039;&#039; &#039;&#039;z&lt;sup&gt;n&lt;/sup&gt;&#039;&#039;}}, change the order of the summations on {{mvar|n}} and {{mvar|k}}, and try to compute the inner sum.<br /> <br /> For example, if we want to compute<br /> &lt;math display=&quot;block&quot;&gt;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\,,&lt;/math&gt;<br /> we can treat {{mvar|n}} as a &quot;free&quot; parameter, and set<br /> &lt;math display=&quot;block&quot;&gt;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\,.&lt;/math&gt;<br /> <br /> Interchanging summation (&quot;snake oil&quot;) gives<br /> &lt;math display=&quot;block&quot;&gt;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}}\,.&lt;/math&gt;<br /> <br /> Now the inner sum is {{math|{{sfrac|&#039;&#039;z&#039;&#039;&lt;sup&gt;&#039;&#039;m&#039;&#039; + 2&#039;&#039;k&#039;&#039;&lt;/sup&gt;|(1 − &#039;&#039;z&#039;&#039;)&lt;sup&gt;&#039;&#039;m&#039;&#039; + 2&#039;&#039;k&#039;&#039; + 1&lt;/sup&gt;}}}}. Thus<br /> &lt;math display=&quot;block&quot;&gt;\begin{align} F(z)<br /> &amp;= \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]<br /> &amp;= \frac{z^m}{(1-z)^{m+1}}\sum_{k = 0}^\infty{C_k\left(\frac{-z}{(1-z)^2}\right)^k} &amp;\text{where } C_k = k\text{th Catalan number} \\[4px]<br /> &amp;= \frac{z^m}{(1-z)^{m+1}}\frac{1-\sqrt{1+\frac{4z}{(1-z)^2}}}{\frac{-2z}{(1-z)^2}} \\[4px]<br /> &amp;= \frac{-z^{m-1}}{2(1-z)^{m-1}}\left(1-\frac{1+z}{1-z}\right) \\[4px]<br /> &amp;= \frac{z^m}{(1-z)^m} = z\frac{z^{m-1}}{(1-z)^m}\,.<br /> \end{align}&lt;/math&gt;<br /> <br /> Then we obtain<br /> &lt;math display=&quot;block&quot;&gt;s_n = \begin{cases}<br /> \displaystyle\binom{n-1}{m-1} &amp; \text{for } m \geq 1 \,, \\ {}<br /> [n = 0] &amp; \text{for } m = 0\,.<br /> \end{cases}&lt;/math&gt;<br /> <br /> It is instructive to use the same method again for the sum, but this time take {{mvar|m}} as the free parameter instead of {{mvar|n}}. We thus set<br /> &lt;math display=&quot;block&quot;&gt;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\,.&lt;/math&gt;<br /> <br /> Interchanging summation (&quot;snake oil&quot;) gives<br /> &lt;math display=&quot;block&quot;&gt;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}\,.&lt;/math&gt;<br /> <br /> Now the inner sum is {{math|(1 + &#039;&#039;z&#039;&#039;)&lt;sup&gt;&#039;&#039;n&#039;&#039; + &#039;&#039;k&#039;&#039;&lt;/sup&gt;}}. Thus<br /> &lt;math display=&quot;block&quot;&gt;\begin{align} G(z)<br /> &amp;= (1+z)^n \sum_{k = 0}^\infty \frac{1}{k+1}\binom{2k}{k}\left(\frac{-(1+z)}{z^2}\right)^k \\[4px]<br /> &amp;= (1+z)^n \sum_{k = 0}^\infty C_k \,\left(\frac{-(1+z)}{z^2}\right)^k &amp;\text{where } C_k = k\text{th Catalan number} \\[4px]<br /> &amp;= (1+z)^n \,\frac{1-\sqrt{1+\frac{4(1+z)}{z^2}}}{\frac{-2(1+z)}{z^2}} \\[4px]<br /> &amp;= (1+z)^n \,\frac{z^2-z\sqrt{z^2+4+4z}}{-2(1+z)} \\[4px]<br /> &amp;= (1+z)^n \,\frac{z^2-z(z+2)}{-2(1+z)} \\[4px]<br /> &amp;= (1+z)^n \,\frac{-2z}{-2(1+z)} = z(1+z)^{n-1}\,.<br /> \end{align}&lt;/math&gt;<br /> <br /> Thus we obtain<br /> &lt;math display=&quot;block&quot;&gt;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}\,,&lt;/math&gt;<br /> for {{math|&#039;&#039;m&#039;&#039; ≥ 1}} as before.<br /> <br /> ===Generating functions prove congruences===<br /> We say that two generating functions (power series) are congruent modulo {{mvar|m}}, written {{math|&#039;&#039;A&#039;&#039;(&#039;&#039;z&#039;&#039;) ≡ &#039;&#039;B&#039;&#039;(&#039;&#039;z&#039;&#039;) (mod &#039;&#039;m&#039;&#039;)}} if their coefficients are congruent modulo {{mvar|m}} for all {{math|&#039;&#039;n&#039;&#039; ≥ 0}}, i.e., {{math|&#039;&#039;a&lt;sub&gt;n&lt;/sub&gt;&#039;&#039; ≡ &#039;&#039;b&lt;sub&gt;n&lt;/sub&gt;&#039;&#039; (mod &#039;&#039;m&#039;&#039;)}} for all relevant cases of the integers {{mvar|n}} (note that we need not assume that {{mvar|m}} is an integer here—it may very well be polynomial-valued in some indeterminate {{mvar|x}}, for example). If the &quot;simpler&quot; right-hand-side generating function, {{math|&#039;&#039;B&#039;&#039;(&#039;&#039;z&#039;&#039;)}}, is a rational function of {{mvar|z}}, then the form of this sequence suggests that the sequence is [[periodic function|eventually periodic]] modulo fixed particular cases of integer-valued {{math|&#039;&#039;m&#039;&#039; ≥ 2}}. For example, we can prove that the [[Euler numbers]],<br /> &lt;math display=&quot;block&quot;&gt;\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}\,,&lt;/math&gt;<br /> satisfy the following congruence modulo 3:&lt;ref&gt;{{harvnb|Lando|2003|loc=§5}}&lt;/ref&gt;<br /> &lt;math display=&quot;block&quot;&gt;\sum_{n = 0}^\infty E_n z^n = \frac{1-z^2}{1+z^2} \pmod{3}\,. &lt;/math&gt;<br /> <br /> 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 {{math|&#039;&#039;p&lt;sup&gt;k&lt;/sup&gt;&#039;&#039;}}) is given in the section on continued fraction representations of (even non-convergent) ordinary generating functions by {{mvar|J}}-fractions above. We cite one particular result related to generating series expanded through a representation by continued fraction from Lando&#039;s &#039;&#039;Lectures on Generating Functions&#039;&#039; as follows:<br /> {{math theorem | name = Theorem: congruences for series generated by expansions of continued fractions<br /> | math_statement = Suppose that the generating function {{math|&#039;&#039;A&#039;&#039;(&#039;&#039;z&#039;&#039;)}} is represented by an infinite [[continued fraction]] of the form<br /> &lt;math display=&quot;block&quot;&gt;A(z) = \cfrac{1}{1-c_1z - \cfrac{p_1z^2}{1-c_2z - \cfrac{p_2 z^2}{1-c_3z - {\ddots}}}}&lt;/math&gt;<br /> and that {{math|&#039;&#039;A&lt;sub&gt;p&lt;/sub&gt;&#039;&#039;(&#039;&#039;z&#039;&#039;)}} denotes the {{mvar|p}}th convergent to this continued fraction expansion defined such that {{math|&#039;&#039;a&lt;sub&gt;n&lt;/sub&gt;&#039;&#039; {{=}} [&#039;&#039;z&lt;sup&gt;n&lt;/sup&gt;&#039;&#039;] &#039;&#039;A&lt;sub&gt;p&lt;/sub&gt;&#039;&#039;(&#039;&#039;z&#039;&#039;)}} for all {{math|0 ≤ &#039;&#039;n&#039;&#039; &lt; 2&#039;&#039;p&#039;&#039;}}. Then:<br /> <br /> # the function {{math|&#039;&#039;A&lt;sub&gt;p&lt;/sub&gt;&#039;&#039;(&#039;&#039;z&#039;&#039;)}} is rational for all {{math|&#039;&#039;p&#039;&#039; ≥ 2}} where we assume that one of divisibility criteria of {{math|&#039;&#039;p&#039;&#039; {{!}} &#039;&#039;p&#039;&#039;&lt;sub&gt;1&lt;/sub&gt;, &#039;&#039;p&#039;&#039;&lt;sub&gt;1&lt;/sub&gt;&#039;&#039;p&#039;&#039;&lt;sub&gt;2&lt;/sub&gt;, &#039;&#039;p&#039;&#039;&lt;sub&gt;1&lt;/sub&gt;&#039;&#039;p&#039;&#039;&lt;sub&gt;2&lt;/sub&gt;&#039;&#039;p&#039;&#039;&lt;sub&gt;3&lt;/sub&gt;}} is met, that is, {{math|&#039;&#039;p&#039;&#039; {{!}} &#039;&#039;p&#039;&#039;&lt;sub&gt;1&lt;/sub&gt;&#039;&#039;p&#039;&#039;&lt;sub&gt;2&lt;/sub&gt;⋯&#039;&#039;p&#039;&#039;&lt;sub&gt;&#039;&#039;k&#039;&#039;&lt;/sub&gt;}} for some {{math|&#039;&#039;k&#039;&#039; ≥ 1}}; and <br /> # if the integer {{mvar|p}} divides the product {{math|&#039;&#039;p&#039;&#039;&lt;sub&gt;1&lt;/sub&gt;&#039;&#039;p&#039;&#039;&lt;sub&gt;2&lt;/sub&gt;⋯&#039;&#039;p&#039;&#039;&lt;sub&gt;&#039;&#039;k&#039;&#039;&lt;/sub&gt;}}, then we have {{math|&#039;&#039;A&#039;&#039;(&#039;&#039;z&#039;&#039;) ≡ &#039;&#039;A&lt;sub&gt;k&lt;/sub&gt;&#039;&#039;(&#039;&#039;z&#039;&#039;) (mod &#039;&#039;p&#039;&#039;)}}.}}<br /> <br /> 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 (mathematics)|partition function {{math|&#039;&#039;p&#039;&#039;(&#039;&#039;n&#039;&#039;)}}]] which show the versatility of generating functions in tackling problems involving [[integer sequences]].<br /> <br /> ====The Stirling numbers modulo small integers====<br /> <br /> The [[Stirling numbers of the first kind#Congruences|main article]] on the Stirling numbers generated by the finite products<br /> &lt;math display=&quot;block&quot;&gt;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\,, &lt;/math&gt;<br /> <br /> provides an overview of the congruences for these numbers derived strictly from properties of their generating function as in Section 4.6 of Wilf&#039;s stock reference &#039;&#039;Generatingfunctionology&#039;&#039;.<br /> We repeat the basic argument and notice that when reduces modulo 2, these finite product generating functions each satisfy<br /> <br /> &lt;math display=&quot;block&quot;&gt;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}\,, &lt;/math&gt;<br /> <br /> which implies that the parity of these [[Stirling numbers]] matches that of the binomial coefficient<br /> <br /> &lt;math display=&quot;block&quot;&gt;\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}\,, &lt;/math&gt;<br /> <br /> and consequently shows that {{math|{{resize|150%|[}}{{su|p=&#039;&#039;n&#039;&#039;|b=&#039;&#039;k&#039;&#039;|a=c}}{{resize|150%|]}}}} is even whenever {{math|&#039;&#039;k&#039;&#039; &lt; ⌊ {{sfrac|&#039;&#039;n&#039;&#039;|2}} ⌋}}.<br /> <br /> 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<br /> &lt;math display=&quot;block&quot;&gt;\begin{align}<br /> \begin{bmatrix} n \\ m \end{bmatrix} &amp; \equiv<br /> [x^m] \left(<br /> x^{\left\lceil \frac{n}{3} \right\rceil} (x+1)^{\left\lceil \frac{n-1}{3} \right\rceil}<br /> (x+2)^{\left\lfloor \frac{n}{3} \right\rfloor}<br /> \right) &amp;&amp; \pmod{3} \\<br /> &amp; \equiv<br /> \sum_{k=0}^{m} \begin{pmatrix} \left\lceil \frac{n-1}{3} \right\rceil \\ k \end{pmatrix}<br /> \begin{pmatrix} \left\lfloor \frac{n}{3} \right\rfloor \\ m-k - \left\lceil \frac{n}{3} \right\rceil \end{pmatrix} \times<br /> 2^{\left\lceil \frac{n}{3} \right\rceil + \left\lfloor \frac{n}{3} \right\rfloor -(m-k)} &amp;&amp; \pmod{3}\,.<br /> \end{align}&lt;/math&gt;<br /> <br /> ====Congruences for the partition function====<br /> <br /> 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 &#039;&#039;the&#039;&#039; [[partition function (number theory)|partition function]] {{math|&#039;&#039;p&#039;&#039;(&#039;&#039;n&#039;&#039;)}} is generated by the reciprocal infinite [[q-Pochhammer symbol|{{mvar|q}}-Pochhammer symbol]] product (or {{mvar|z}}-Pochhammer product as the case may be) given by<br /> &lt;math display=&quot;block&quot;&gt;\begin{align}<br /> \sum_{n = 0}^\infty p(n) z^n &amp; = \frac{1}{\left(1-z\right)\left(1-z^2\right)\left(1-z^3\right) \cdots} \\[4pt]<br /> &amp; = 1 + z + 2z^2 + 3 z^3 + 5z^4 + 7z^5 + 11z^6 + \cdots.<br /> \end{align}&lt;/math&gt;<br /> <br /> This partition function satisfies many known [[Ramanujan&#039;s congruences|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:&lt;ref&gt;{{harvnb|Hardy|Wright|Heath-Brown|Silverman|2008|loc=§19.12}}&lt;/ref&gt;<br /> &lt;math display=&quot;block&quot;&gt;\begin{align}<br /> p(5m+4) &amp; \equiv 0 \pmod{5} \\<br /> p(7m+5) &amp; \equiv 0 \pmod{7} \\<br /> p(11m+6) &amp; \equiv 0 \pmod{11} \\<br /> p(25m+24) &amp; \equiv 0 \pmod{5^2}\,.<br /> \end{align}&lt;/math&gt;<br /> <br /> 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.<br /> <br /> First, we observe that in the binomial coefficient generating function<br /> &lt;math display=block&gt;\frac{1}{(1-z)^5} = \sum_{i=0}^\infty \binom{4+i}{4}z^i\,,&lt;/math&gt;<br /> all of the coefficients are divisible by 5 except for those which correspond to the powers {{math|1, &#039;&#039;z&#039;&#039;&lt;sup&gt;5&lt;/sup&gt;, &#039;&#039;z&#039;&#039;&lt;sup&gt;10&lt;/sup&gt;, ...}} and moreover in those cases the remainder of the coefficient is 1 modulo 5. Thus, <br /> &lt;math display=&quot;block&quot;&gt;\frac{1}{(1-z)^5} \equiv \frac{1}{1-z^5} \pmod{5}\,,&lt;/math&gt; <br /> or equivalently<br /> &lt;math display=&quot;block&quot;&gt; \frac{1-z^5}{(1-z)^5} \equiv 1 \pmod{5}\,.&lt;/math&gt;<br /> It follows that<br /> &lt;math display=&quot;block&quot;&gt;\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}\,. &lt;/math&gt;<br /> <br /> Using the infinite product expansions of <br /> &lt;math display=&quot;block&quot;&gt;z \cdot \frac{\left(1-z^5\right)\left(1-z^{10}\right) \cdots }{\left(1-z\right)\left(1-z^2\right) \cdots } =<br /> 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}\,,&lt;/math&gt;<br /> it can be shown that the coefficient of {{math|&#039;&#039;z&#039;&#039;&lt;sup&gt;5&#039;&#039;m&#039;&#039; + 5&lt;/sup&gt;}} in {{math|&#039;&#039;z&#039;&#039; · ((1 − &#039;&#039;z&#039;&#039;)(1 − &#039;&#039;z&#039;&#039;&lt;sup&gt;2&lt;/sup&gt;)⋯)&lt;sup&gt;4&lt;/sup&gt;}} is divisible by 5 for all {{mvar|m}}.&lt;ref&gt;{{cite book |last1=Hardy |first1=G.H. |last2=Wright |first2=E.M.|title=An Introduction to the Theory of Numbers}} p.288, Th.361&lt;/ref&gt; Finally, since<br /> &lt;math display=&quot;block&quot;&gt;\begin{align}<br /> \sum_{n = 1}^\infty p(n-1) z^n &amp; = \frac{z}{(1-z)\left(1-z^2\right) \cdots} \\[6px]<br /> &amp; = 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<br /> \end{align}&lt;/math&gt;<br /> we may equate the coefficients of {{math|&#039;&#039;z&#039;&#039;&lt;sup&gt;5&#039;&#039;m&#039;&#039; + 5&lt;/sup&gt;}} in the previous equations to prove our desired congruence result, namely that {{math|&#039;&#039;p&#039;&#039;(5&#039;&#039;m&#039;&#039; + 4) ≡ 0 (mod 5)}} for all {{math|&#039;&#039;m&#039;&#039; ≥ 0}}.</div> Yeetcode