## Number sequences

A _number sequence_ is simply an infinite sequence $h_0,h_1,h_2,...$ of numbers.
For us, $h_i$ will typically be an integer.

Examples:
    1,2,3,4,5,...
    2,4,8,16,32,...
    2,3,5,7,13,...
    1,1,2,3,5,8,13,...
    1,5,10,10,5,1,0,0,0,0,...

# Generating functions

The _generating function_ of a number sequence $h_0,h_1,....$ is the formal
power series $g(x) = \sum_{n=0}^\infty h_n x^n$.

Technical remark:
    Despite the notation and terminology, we do not assume any convergence;
    we do not need $g(a)$ to make sense for $a$ a real number,
    so $g$ doesn't really have to be a function in the usual sense.
    for example, $\sum_{n=0}^\infty n^n x^n$ doesn't converge for $x!=0$,
    but it's a perfectly good generating function.

    We use the usual algebraic notation for generating functions. We can make
    sense of algebraic operations as follows:\;

    Given formal power series $g(x)=\sum_{n=0}^\infty h_n x^n$ and
    $g'(x)=\sum_{n=0}^\infty h'_n x^n$, and a number $c$, we define
    :	$g(x)+g'(x) := \sum_{n=0}^\infty (h_n+h'_n) x^n$
    :	$c g(x) := \sum_{n=0}^\infty ch_n x^n$
    :	$g(x)g'(x) := \sum_{n=0}^\infty (\sum_{j=0}^n h_jh'_{n-j}) x^n$.

    We also write $c(x) = \frac{a(x)}{b(x)}$ to mean that $a(x) = b(x)c(x)$
    (this is well-defined).

We can often use this algebraic structure to write generating functions compactly.

Example 1:
    Consider the binomial coefficients ${m\choose n}$ for a fixed $m$.
    This is a finite number sequence, but we can make it infinite by appending
    0s,
    :	${m\choose 0},{m\choose 1},...,{m\choose m},0,0,0,...$.

    So the generating function is
    :	${m\choose 0} + {m\choose 1}x + {m \choose 2} x^2 ... + {m \choose m} x^m$
    :	    $= (x+1)^m$

Example 2:
    The generating function of the number sequence
    :   $1,1,1,...$
    is $g(x) = \sum_{n=0}^\infty x^n = 1+x+x^2+...$.

    Now, multiplying out,
    :   $(1+x+x^2+...)(1-x) = 1 + (-1+1)x + (-1+1) x^2 + ...$,
    so $g(x) = 1/(1-x)$.
    \qed

Generating functions provide an efficient notation for describing and
manipulating classes of combinatorial problems.

Example 3:
    Given $t$, let $h_n$ be the number of $n$-combinations of a multiset with
    $t$ types and infinite multiplicity for each type.

    We know $h_n = {n+t-1\choose t-1}$, so
    the generating function is
    :   $g(x) = \sum_{n=0}^\infty h_n x^n = \sum_{n=0}^\infty {n+t-1\choose t-1} x^n$.

    But note also that 
    :   $g(x) = (1+x+x^2+...)^t$,
    since when we multiply the right hand side out,
    the coefficient of $x^n$ is precisely the number of ways of obtaining
    $x^n$ as $x^{e_1}x^{e_2}...x^{e_t}$,
    which is the number of solutions in non-negative integers to
    $e_1+...+e_k=n$,
    which (as we've seen before) is $h_n$.

    So as in the previous example,
    :   $g(x) = (1+x+x^2+...)^t = \left(\frac{1}{1-x}\right)^t = \frac{1}{(1-x)^t}$.
    \qed

Note we found here the power series expansion of $\frac 1{(1-x)^t}$, which
will come in handy later.

Lemma 1:
    :	$\frac{1}{(1-x)^t} = \sum_{n=0}^\infty {n+t-1 \choose t-1} x^n$

If we have restrictions on how many of each type we're allowed to take in a
combination, we can incorporate these into an algebraic expression for the
generating function.

Example 4:
    {\it Find the generating function for the number $h_n$ of bags of $n$ marbles
    consisting of an even number of red marbles, at least 1 green marble, at
    most 36 blue marbles, and an odd number of yellow marbles.}

    Arguing as in the previous example, the generating function is
    :   $g(x) = (1+x^2+x^4+...)(x+x^2+x^3+...)(1+x+x^2+...+x^36)(x+x^3+x^5+...)$
    :       $= \frac 1{1-x^2}\frac x{1-x}\frac{1-x^{37}}{1-x}\frac x{1-x^2}$.
    :       $= \frac{x^2(1-x^{37})}{(1-x^2)^2(1-x)^2}$.

Example 5:
    {\it Find the generating function for the number $h_n$ of bags of $n$ marbles
    consisting of an even number of red marbles, a multiple of 3 of green marbles, at
    most 2 blue marbles, and at most one yellow marble. Hence explicitly
    determine $h_n$.}

    :   $g(x) = (1+x^2+x^4+...)(1+x^3+x^6+...)(1+x+x^2)(1+x)$
    :       $= \frac 1{1-x^2}\frac 1{1-x^3}\frac{1-x^3}{1-x}(1+x)$
    :       $= \frac{1+x}{(1-x^2)(1-x)}$
    :       $= \frac{1+x}{(1+x)(1-x)(1-x)}$
    :       $= \frac{1}{(1-x)^2}$
    :       $= \sum_{n=0}^\infty {n+2-1\choose 2-1} x^n$ (by Lemma 1) 
    :       $= \sum_{n=0}^\infty (n+1) x^n$.

    So there are $n+1$ such bags of $n$ marbles!

    (Exercise: find a direct proof of this, without going via generating
    functions.)

Example 6:
    {\it Find the generating function for the number $h_n$ of ways of making
    $n$ cents out of Canadian coins.}

    The coins in current circulation are worth 5,10,25,100, and 200 cents
    each.

    So $h_n$ is the number of solutions in non-negative integers to
    :   $5N+10D+25Q+100L+200T=n$.
    Equivalently, $h_n$ is the number of solutions to
    :   $e_1+e_2+e_3+e_4+e_5=n$
    where $e_1$ is a multiple of 5, $e_2$ is a multiple of 10, etc.

    So as above,
    :   $g(x) = (x^5+x^{10}+x^{15}+...)(x^{10}+x^{20}+...)...(x^{200}+x^{400}+...)$
    :       $= \frac 1{(1-x^5)(1-x^{10})...(1-x^{200})}$.

# Exponential Generating Functions

The _exponential generating function_ of a number sequence $h_0,h_1,...$ is
the formal power series
:   $g^{(e)}(x) = \sum_{n=0}^\infty h_n \frac{x^n}{n!}$.

While ordinary generating functions are useful for counting combinations,
exponential generating functions are useful for counting permutations.

Example 7:
    The exponential generating function of
    :   $(m,0),P(m,1),...,P(m,m),0,0,0,...$
    is
    :   $g^{(e)} = \sum_{n=0}^m \frac{m!}{(m-n)!}\frac{x^n}{n!}$
    :       $= \sum_{n=0}^m {m\choose n}x^n$
    :       $= (1+x)^m$

Example 8:
    Let $h_n$ be the number of $n$-permutations of a multiset with $k$
    different types, each with infinite multiplicity,
    :   $\{\infty\cdot a_1,..., \infty \cdot a_k\}$.


    So $h_n=k^n$.
    Then the exponential generating function is
    : $g^{(e)}(x) = \sum_{n=0}^\infty \frac{k^nx^n}{n!} = e^{kx}$.

(remark for anyone who might worry what exactly we mean by this last
equality: we can just _define_ $e^{ax}$ to be the formal power series
$\sum_{n=0}^\infty \frac{a^n}{n!}x^n$. This obeys the usual law $e^{ax}e^{bx}
= e^{(a+b)x}$. We could define more, but this will suffice for our purposes.)

Theorem:
    Let $h_n$ be the number of $n$-permutations of the multiset
    :   $S := \{n_1\cdot a_1,..., n_k \cdot a_k\}$,
    with $n_i \in \|N\cup\{\infty\}$.

    Then the exponential generating function is
    :   $g^{(e)} = f_{n_1}(x)f_{n_2}(x)...f_{n_k}(x)$
    where
    :   $f_{n}(x) = \sum_{i=0}^n \frac{x^i}{i!}$
    and in particular, $f_\infty(x) = e^x$.

Proof:
    :   $h_n = \sum_{\textrm{$S'$ an $n$-combination of $S$}} (\textrm{number of permutations of $S'$})$
    :       $= \sum_{\{(m_1,...,m_k) | m_1+...+m_k=n,\; 0<=m_i<=n_i \}}$
    :       \;\;\;\; $(\textrm{number of permutations of $\{m_1 * a_1,...,m_k*a_k\}$}$
    :       $= \sum_{\{(m_1,...,m_k) | m_1+...+m_k=n,\; 0<=m_i<=n_i \}} \frac{n!}{m_1!...m_k!}$
    :       $= n! \sum_{\{(m_1,...,m_k) | m_1+...+m_k=n,\; 0<=m_i<=n_i \}} \frac{1}{m_1!...m_k!}$

    Meanwhile, if we multiply out $f_{n_1}(x)f_{n_2}(x)...f_{n_k}(x)$, we find
    the coefficient of $x^n$ is 
    :       $= \sum_{\{(m_1,...,m_k) | m_1+...+m_k=n,\; 0<=m_i<=n_i \}} \frac{1}{m_1!...m_k!}$.

    So this is indeed the exponential generating function.
    \qed

Just as we saw with ordinary generating functions, if we have restrictions on
how many of each type we are allowed in a permutation, we can incorporate
these restrictions into the factors in the above expression for the
exponential generating function, by only including the appropriate powers of
$x$.

Often, expanding out the resulting power series will give us a solution to the
combinatorial problem, as the following example demonstrates.

Example 9:
    {\it How many $n$-digit numbers can be written using only the digits
    '1','2', and '3', using an even number of '2's and at least 1 '3'?}

    The exponential generating function is
    :   $g^{(e)}(x) = (\sum_{n=0}^\infty \frac{x^n}{n!})(\sum_{n=0}^\infty \frac{x^{2n}}{(2n)!})(\sum_{n=1}^\infty \frac{x^n}{n!})$
    :       $= (e^x)(\frac{e^x+e^{-x}}2)(e^x-1)$
    :       $= \frac 12 (e^{3x}+e^x-e^{2x}-1)$
    :       $= \sum_{n=1}^\infty \frac{3^n + 1 - 2^n}{2n!}$

    So the answer is $\frac{3^n + 1 - 2^n}2$
