# A Primer: Generating Functions: Part I : RMO/INMO 2019

GENERATING FUNCTIONS and RECURRENCE RELATIONS:

The concept of a generating function is one of the most useful and basic concepts in the theory of combinatorial enumeration. If we want to count a collection of objects that depend in some way on n objects and if the desired value is say, $\phi (n)$, then a series in powers of t such as $\sum \phi (n) t^{n}$ is called a generating function for $\phi (n)$. The generating functions arise in two different ways. One is from the investigation of recurrence relations and another is more straightforward: the generating functions arise as counting devices, different terms being specifically included to account for specific situations which we wished to count or ignore. This is a very fundamental, though difficult, technique in combinatorics. It requires considerable ingenuity for its success. We will have a look at the bare basics of such stuff.

We start here with the common knowledge:

$(1+\alpha_{1}t)(1+\alpha_{2}t)\ldots (1+\alpha_{n}t)=1+a_{1}t+a_{2}t^{2}+ \ldots + a_{n}t^{n}$….(2i) where $a_{r}=$sum of the products of the $\alpha$‘s taken r at a time. …(2ii)

Incidentally, the $a$‘s thus defined in (2ii) are called the elementary symmetric functions associated with the $a$‘s. We will re-visit these functions later.

Let us consider the algebraic identity (2i) from a combinatorial viewpoint. The explicit expansion in powers of t of the RHS of (2i) is symbolically a listing of the various combinations of the $\alpha$‘s in the following sense:

$a_{1}=\sum \alpha_{1}$ represents all the 1-combinations of the $\alpha$‘s
$a_{2}=\sum \alpha_{1}\alpha_{2}$ represents all the 2-combinations of the $\alpha$‘s
and so on.

In other words, if we want the r-combinations of the $\alpha$‘s, we have to look only at the coefficients of $t^{r}$. Since the LHS of (2i) is an expression which is easily constructed and its expansion generates the combinations in the said manner,we say that the LHS of (2i) is a Generating Function (GF) for the combinations of the $\alpha$‘s. It may happen that we are interested only in the number of combinations and not in a listing or inventory of them. Then, we need to look for only the number of terms in each coefficient above and this number will be easily obtained if we set each $\alpha$ as 1. Thus, the GF for the number of combinations is $(1+t)(1+t)(1+t)\ldots (1+t)$ n times;

and this is nothing but $(1+t)^{n}$. We already know that the expansion of this gives $n \choose r$ as the coefficient of $t^{r}$ and this tallies with the fact that the number of r-combinations of the $\alpha$‘s is $n \choose r$. Abstracting these ideas, we make the following definition:

Definition I:
The Ordinary Generating Function (OGF) for a sequence of symbolic expressions $\phi(n)$ is the series

$f(t)=\sum_{n}\phi (n)t^{n}$ …(2iii)

If $\phi (n)$ is a number which counts a certain type of combinations or permutations, the series $f(t)$ is called the Ordinary Enumeration (OE) or counting series for $\phi (n)$ for $n=1,2,\ldots$

Example 2:
The OGF for the combinations of five symbols a, b, c, d, e is $(1+at)(1+bt)(1+ct)(1+dt)(1+et)$

The OE for the same is $(1+t)^{5}$. The coefficient of $t^{4}$ in the first expression is

(*) abcd+abce+ abde+acde+bcde.

The coefficient of $t^{4}$ in the second expression is $5 \choose 4$, that is, 5 and this is the number of terms in (*).

Example 3:

The OGF for the elementary symmetric functions $a_{1}, a_{2}, \ldots$ in the symbols $\alpha_{1},\alpha_{2}, \alpha_{3}, \ldots$ is $(1+\alpha_{1}t)(1+\alpha_{2}t)(1+\alpha_{3}t)\ldots$ ….(2iv)

This is exactly the algebraic result with which we started this section.

Remark:

The fact that the series on the HRS of (2iii) is an infinite series should not bother us with questions of convergence and the like. For, throughout (combinatorics) we shall be working only in the framework of “formal power series” which we now elaborate.

*THE ALGEBRA OF FORMAL POWER SERIES*

The vector space of infinite sequences of real numbers is well-known. If $(\alpha_{k})$ and $\beta_{k}$ are two sequences, their sum is the sequence $(\alpha_{k}+\beta_{k})$, and a scalar multiple of the sequence $(\alpha_{k})$ is $(c\alpha_{k})$. We now identify the sequence $(\alpha_{k})$ with $k=0,1,2, \ldots$ with the “formal” series

$f = \sum_{k=0}^{\infty}\alpha_{k}t^{k}$….(2v)

where $t^{k}$ only means the following:

$t^{0}=1$, $t^{k}t^{l}=t^{k+l}$.

In the same way, $(\beta_{k})$, where $k=0,1,2,\ldots$ corresponds to the formal series:

$g=\sum_{k=0}^{\infty}\beta_{k}t^{k}$ and

we define: $f+g = \sum (\alpha_{k}+\beta_{k})t^{k}$, and $cf= \sum (c\alpha_{k})t^{k}$.

The set of all power series f now becomes a vector space isomorphic to the space of infinite sequences of real numbers. The zero element of this space is the series with every coefficient zero.

Now, let us define a product of two formal power series. Given f and g as above, we write $fg=\sum_{k=0}^{\infty}\gamma_{k} t^{k}$ where

$\gamma_{k}=\alpha_{0}\beta_{k}+\alpha_{1}\beta_{k-1}+\ldots + \alpha_{k}\beta_{0} = \sum (\alpha_{i}\beta_{j})$, where $i+j=k$.

The multiplication is associative, commutative, and also distributive wrt addition. (the students/readers can take up this as an appetizer exercise !!) In fact, the set of all formal power series becomes an algebra. It is called the algebra of formal power series over the real s. It is denoted by $\bf\Re[t]$, where $\bf\Re$ means the algebra of reals. We further postulate that $f=g$ in $\bf\Re[t]$ iff $\alpha_{k}=\beta_{k}$ for all $k=0,1,2,\ldots$. As we do in polynomials, we shall agree that the terms not present indicate that the coefficients are understood to be zero. The elements of $\bf\Re$ may be considered as elements of $\bf\Re[t]$. In particular, the unity 1 of $\bf\Re$ is also the unity of $\bf\Re[t]$. Also, the element $t^{n}$ with $n>0$ belongs to $\bf\Re$, it being the formal power series $\sum \alpha_{k}t^{k}$ with $\alpha_{n}=1$ and all other $\alpha$‘s zero. We now have the following important proposition which is the only tool necessary for working with formal power series as far as combinatorics is concerned:

Proposition : 2_4:
The element f of $\bf\Re[t]$ given by (2v) has an inverse in $\bf\Re[t]$ iff $\alpha_{0}$ has an inverse in $\bf\Re$.

Proof:
If $g=\sum \beta_{k}t^{k}$ is such that $fg=1$, the multiplication rule in $\bf\Re[t]$ tells us that $\alpha_{0}\beta_{0}=1$ so that $\beta_{0}$ is the inverse of $\alpha_{0}$. Hence, the “only if” part is proved.

To prove the “if” part, let $\alpha_{0}$ have an inverse $\alpha_{0}^{-1}$ in $\bf\Re$. We will show that it is possible to find $g=\sum \beta_{k}t^{k}$ in $\bf\Re[t]$ such that $fg=1$. If such a g were to exist, then the following equations should hold in order that $fg=1$, that is,

$\alpha_{0}\beta_{0}=1$
$\alpha_{0}\beta_{1}+\alpha_{1}\beta_{0}=0$
$\alpha_{0}\beta_{2}+\alpha_{1}\beta_{1}+\alpha_{2}\beta_{0}=0$
$\vdots$

So we have $\beta_{0}=\alpha_{0}^{-1}$ from the first equation. Substituting this value of $\beta_{0}$ in the second equation, we get $\beta_{1}$ in terms of the $\alpha$‘s. And, so on, by the principle of mathematical induction, all the $\beta$‘s are uniquely determined. Thus, f is invertible in $\bf\Re$. QED.

Note that it is the above proposition which justifies the notation in $\bf\Re[t]$, equalities such as

$\frac{1}{1-t}=1+t+t^{2}+t^{3}+\ldots$

The above is true because the RHS has an inverse and $(1-t)(1+t+t^{2}+t^{3}+\ldots)=1$

So, the unique inverse of $1+t+t^{2}+t^{3}+\ldots$ is $(1-t)$ and vice versa. Hence, the expansion of $\frac{1}{1-t}$ as above. Similarly, we have

$\frac{1}{1+t}=1-t+t^{2}-\ldots$
$\frac{1}{1-t^{2}}=1+t^{2}+t^{4}+\ldots$ and many other such familiar expansions.

There is a differential operator in $D$ in $\bf\Re[t]$, which behaves exactly like the differential operator of calculus.

Define: $(Df)(\alpha)=\sum_{k=0}^{\infty}(k+1)\alpha_{k+1}t^{k}$

Then, one can easily prove that $D: f \rightarrow Df$ is linear on $\bf\Re[t]$, and further
$D^{r}f(t)=\sum_{k=0}^{\infty}(k+r)(k+r-1)\ldots(k+1)\alpha_{k+r}t^{k}$ from which we get the term “Taylor-MacLaurin” expansion

$f(t)=f(0)+Df(0)+\frac{D^{2}f(0)}{2!}+ \ldots$…(2vi)

In the same manner, one can obtain, from $f(t)=\frac{1}{1-\alpha t}$, which in turn is equal to
$1+ \alpha t + \alpha^{2} t^{2}+ \alpha^{3} t^{3} + \ldots$

the result which mimics the logarithmic differentiation of calculus, viz.,

$\frac{(Df)(t)}{f(t)} = \alpha + \alpha^{2} t+ \alpha^{3}t^{2}+ \alpha^{4}t^{3}+\ldots$…(2vii)

The truth of this in $\bf\Re[t]$ is seen by multiplying the series on the RHS of (2vii) by the series for $f(t)$, and thus obtaining the series for $(Df)(t)$.

Let us re-consider generating functions now. We saw that the GF for combinations of $\alpha_{1}, \alpha_{2}, \ldots, \alpha_{n}$ is $(1+\alpha_{1}t)(1+\alpha_{2}t)\ldots(1+\alpha_{n}t)$.

Let us analyze this and find out why it works. After all, what is a combination of the symbols : $\alpha_{1}, \alpha_{2}, \ldots, \alpha_{n}$? It is the result of a decision process involving a sequence of independent decisions as we move down the list of the $\alpha$‘s. The decisions are to be made on the following questions: Do we choose $\alpha_{1}$ or not? Do we choose $\alpha_{2}$ or not? $\ldots$ Do we choose $\alpha_{n}$ or not? And, if it is an r-combination that we want, we say “yes” to r of the questions above and say “no” to the remaining. The factor $(1+\alpha_{1}t)$ in the expression (2ii) is an algebraic indication of the combinatorial fact that there are only two mutually exclusive alternatives available for us as far as the symbol $\alpha_{1}$ is concerned. Either we choose $\alpha_{1}$ or not. Choosing “$\alpha_{1}$” corresponds to picking the term $\alpha_{1}t$ and choosing “not $-\alpha_{1}$” corresponds to picking the term 1. This correspondence is justified by the fact that in the formation of products in the expression of (2iv), each term in the expansion has only one contribution from $1+\alpha_{1}t$ and that is either $1$ or $\alpha_{t}$.

The product $(1+\alpha_{1}t)(1+\alpha_{2}t)$ gives us terms corresponding to all possible choices of combinations of the symbols $\alpha_{1}$ and $\alpha_{2}$ — these are:

$1.1$ standing for the choice “not-$\alpha_{1}$” and “not-$\alpha_{2}$

$\alpha_{1}t . 1$ standing for the choice of $\alpha_{1}$ and “not-$\alpha_{2}$

$1.\alpha_{2}t$ standing for the choice of “not-$\alpha_{1}$” and $\alpha_{2}$.

$\alpha_{1}t . \alpha_{2}t$ standing for the choice of $\alpha_{1}$ and $\alpha_{2}$.

This is, in some sense, the rationale for (2iv) being the OGF for the several r-combinations of $\alpha_{1}, \alpha_{2}, \ldots, \alpha_{n}$.

We shall now complicate the situation a little bit. Let us ask for the combinations of the symbols $\alpha_{1}, \alpha_{2}, \ldots, \alpha_{n}$ with repetitions of each symbol allowed once more in the combinations.

To be discussed in the following article,

Regards,
Nalin Pithwa.

Reference:
Combinatorics, Theory and Applications, V. Krishnamurthy, East-West Press.
https://www.amazon.in/Combinatorics-Theory-Applications-Krishnamurthy-V/dp/8185336024/ref=sr_1_5?keywords=V+Krishnamurthy&qid=1553718848&s=books&sr=1-5

# Fermat-Kraitchik Factorization Method for factoring large numbers: training for RMO

Reference: Elementary Number Theory, David M. Burton, 6th Edition.

In a fragment of a letter in all probability to Father Marin Mersenne in 1643, Fermat described a technique of his for factoring large numbers. This represented the first real improvement over the classical method of attempting to find a factor of n by dividing by all primes not exceeding $\sqrt{n}$. Fermat’s factorization scheme has at its heart the observation that the search for factors of an odd integer n (because powers of 2 are easily recognizable and may be removed at the outset, there is no loss in assuming that n is odd) is equivalent to obtaining integral solutions of x and y of the equation $n = x^{2} - y^{2}$.

If n is the difference of two squares, then it is apparent that n can be factored as $n = x^{2}-y^{2} = (x+y)(x-y)$.

Conversely, when n has the factorization $n=ab$, with $a \geq b \geq 1$, then we may write $n = (\frac{a+b}{2})^{2}-(\frac{a-b}{2})^{2}$

Moreover, because n is taken to be an odd integer, a and b are themselves odd, hence, $\frac{a+b}{2}$ and $\frac{a-b}{2}$ will be nonnegative integers.

One begins the search for possible x and y satisfying the equation $n=x^{2}-y^{2}$ or what is the same thing, the equation $x^{2}-n=y^{2}$ by first determining the smallest integer k for which $k^{2} \geq n$. Now, look successively at the numbers $k^{2}-n$, $(k+1)^{2}-n$, $(k+2)^{2}-n$, $(k+3)^{2}-n$, $\ldots$ until a value of $m \geq n$ is found making $m^{2}-n$ a square. The process cannot go on indefinitely, because we eventually arrive at $(\frac{n+1}{2})^{2}-n=(\frac{n-1}{2})^{2}$ the representation of n corresponding to the trivial factorization $n=n.1$. If this point is reached without a square difference having been discovered earlier, then n has no other factors other than n and 1, in which case it is a prime.

Fermat used the procedure just described to factor $2027651281=44021.46061$ in only 11 steps, as compared with making 4580 divisions by the odd primes up to 44021. This was probably a favourable case designed on purpose to show the chief virtue of this method: it does not require one to know all the primes less than $\sqrt{n}$ to find factors of n.

$\bf{Example}$

To illustrate the application of Fermat’s method, let us factor the integer $n=119143$. From a table of squares, we find that $345^{2}<119143<346^{2}$; thus it suffices to consider values of $k^{2}-119143$ for those k that satisfy the inequality $346 \leq k < (119143+1)/2=59572$. The calculations begin as follows:

$346^{2}-119143=119716-119143=573$

$347^{2}-119143=120409-119143=1266$

$348^{2}-119143=121104-119143=1961$

$349^{2}-119143=121801-119143=2658$

$350^{2}-119143=122500-119143=3357$

$351^{2}-119143=123201-119143=4058$

$352^{2}-119143=123904-119143=4761=69^{2}$

This last line exhibits the factorization $119143=352^{2}-69^{2}=(352+69)(352-69)=421.283$, where both the factors are prime. In only seven steps, we have obtained the prime factorization of the number 119143. Of course, one does not always fare so luckily — it may take many steps before a difference turns out to be a square.

Fermat’s method is most effective when the two factors of n are of nearly the same magnitude, for in this case, a suitable square will appear quickly. To illustrate, let us suppose that $n=233449$ is to be factored. The smallest square exceeding n is $154^{2}$ so that the sequence $k^{2}-n$ starts with:

$154^{2}-23449=23716-23449=267$

$155^{2}-23449=24025-23449=576=24^{2}$. Hence, the factors of 23449 are $23449=(155+24)(155-24)=131$

When examining the differences $k^{2}-n$ as possible squares, many values can be immediately excluded by inspection of the final digits. We know, for instance, that a square must end in one of the six digits 0,1,4,5,6,9. This allows us to exclude all the values in the above example, save for 1266, 1961, 4761. By calculating the squares of the integers from 0 to 99 modulo 100, we see further that, for a square, the last two digits are limited to the following 22 possibilities:

00; 01, 04; 09; 16; 21; 24; 25; 29; 36; 41; 44; 49; 56; 61; 64; 69; 76; 81; 84; 89; 96.

The integer 1266 can be eliminated from consideration in this way. Because 61 is among the last two digits allowable in a square, it is only necessary to look at the numbers 1961 and 4761; the former is not a square, but $4761=69^{2}$.

There is a generalization of Fermat’s factorization method that has been used with some success. Here, we look for distinct integers x and y such that $x^{2}-y^{2}$ is a multiple of n rather than n itself, that is, $x^{2} \equiv y^{2} \pmod {n}$
.
Having obtained such integers $d=gcd(x-y,n)$ (or, $d=gcd(x+y,n)$) can be calculated by means of the Euclidean Algorithm. Clearly, d is a divisor of n, but is it a non-trivial divisor? In other words, do we have $1?

In practice, n is usually the product of two primes p and q, with $p so that d is equal to 1, p, q, or pq. Now, the congruence $x^{2} \equiv y^{2} \pmod{n}$ translates into $pq|(x-y)(x+y)$. Euclid's lemma tells us that p and q must divide one of the factors. If it happened that $p|x-y$ and $q|x-y$, or expressed as a congruence $x \equiv y \pmod{n}$. Also, $p|x+y$ and $q|x+y$ yield $x \equiv -y \pmod{n}$. By seeking integers x and y satisfying $x^{2} \equiv y^{2} \pmod{n}$, where $x \not\equiv \pm \pmod{n}$, these two situations are ruled out. The result of all this is that d is either p or q, giving us a non-trivial divisor of n.

$\bf{Example}$

Suppose we wish to factor the positive integer $n=2189$ and happen to notice that $579^{2} \equiv 18^{2} \pmod{2189}$. Then, we compute $gcd(579-18,2189)=gcd(561,2189)=11$ using the Euclidean Algorithm:

$2189=3.561+506$
$561=1.506+55$
$506=9.55+11$
$55=5.11$

This leads to the prime divisor 11 of 2189. The other factor, namely 199, can be obtained by observing that $gcd(579+18,2189)=gcd(597,2189)=199$

The reader might wonder how we ever arrived at a number, such as 579, whose square modulo 2189 also turns out to be a perfect square. In looking for squares close to multiples of 2189, it was observed that $81^{2} -3.2189 = -6$ and $155^{2}-11.2189=-54$ which translates into $81^{2} \equiv -2.3 \pmod{2189}$ and $155^{2} \equiv -2.3^{3} \pmod{2189}$.

When these congruences are multiplied, they produce $(81.155)^{2} \equiv (2.3^{2})^{2} \pmod{2189}$. Because the product $81.155 = 12555 \equiv -579 \pmod{2189}$, we ended up with the congruence $579^{2} \equiv 18^{2} \pmod{2189}$.

The basis of our approach is to find several $x_{i}$ having the property that each $x_{i}^{2}$ is, modulo n, the product of small prime powers, and such that their product’s square is congruent to a perfect square.

When n has more than two prime factors, our factorization algorithm may still be applied; however, there is no guarantee that a particular solution of the congruence $x^{2} \equiv y^{2} \pmod{n}$, with $x \not\equiv \pm \pmod{n}$ will result in a nontrivial divisor of n. Of course, the more solutions of this congruence that are available, the better the chance of finding the desired factors of n.

Our next example provides a considerably more efficient variant of this last factorization method. It was introduced by *Maurice Kraitchik* in the 1920’s and became the basis of such modern methods as the *quadratic sieve algorithm*.

$\bf{Example}$

Let $n=12499$ be the integer to be factored. The first square just larger than n is $112^{2} = 12544$. So. we begin by considering the sequence of numbers $x^{2}-n$ for $x=112, 113, \ldots$. As before, our interest is in obtaining a set of values $x_{1}, x_{2}, x_{3}, \ldots x_{k}$ for which the product $(x_{1}-n)(x_{2}-n)\ldots (x_{k}-n)$ is a square, say $y^{2}$. Then, $(x_{1}x_{2}\ldots x_{k})^{2} \equiv y^{2} \pmod{n}$, which might lead to a non-factor of n.

A short search reveals that $112^{2}-12499=45$; $117^{2}-12499=1190$; $121^{2}-12499=2142$; or, written as congruences, $112^{2} \equiv 3^{2}.5 \pmod{12499}$ ; $117^{2} \equiv 2.5.7.17 \pmod{12499}$; $121^{2} \equiv 2.3^{2}.7.17 \pmod{12499}$. Multiplying these together results in the congruence: $(112.117.121)^{2} \equiv (2.3^{2}.5.7.17)^{2} \pmod{12499}$, that is, $1585584^{2} \equiv 10710^{2}\pmod{12499}$. But, we are unlucky with this square combination. Because $1585584 \equiv 10710 \pmod{12499}$ only a trivial divisor of 12499 will be found. To be specific,

$gcd(1585584+10710,21499)=1$

$gcd(1585584-10710,12499)=12499$

After further calculation, we notice that

$113^{2} \equiv 2.5.3^{3} \pmod{12499}$

$127^{2} \equiv 2.3.5.11^{2} \pmod{12499}$

which gives rise to the congruence $(113.127)^{2} \equiv (2.3^{2}.5.11)^{2} \pmod{12499}$.

This reduce modulo 12499 to $1852^{2} \equiv 990^{2} \pmod{12499}$ and fortunately, $1852 \not\equiv \pm {990}\pm\pmod{12499}$. Calculating

$gcd(1852-990,12499)=gcd(862,12499)=431$ produces the factorization $12499 =29.431$

Problem to Practise:

Use Kraitchik’s method to factor the number 20437.

Cheers,
Nalin Pithwa