Trophy Hunting Infinite Jungle Finitude: Janne Junnila
Trophy Hunting Infinite Jungle Finitude: Janne Junnila
in the
Infinite Jungle
of
Finitude
Janne Junnila
Compiled on December 3, 2015
1
[Link]
Foreword 6
i number theory 8
1 Algebraic structure of u� 9
1.1 Commutative rings 9
1.2 Ideals and quotient rings 11
1.3 Euclidean domains 13
1.4 Unique factorization 17
1.5 Primes and factoring in u� 20
1.6 Ring homomorphisms 22
1.7 Chinese remainder theorem 23
2 Working modulo u� 26
2.1 Euler’s totient function 26
2.2 Structure of the unit group 27
2.3 Primitive roots 29
2.4 Squares and square roots 30
4 Gaussian integers 44
4.1 Structure of u�[u�] 44
4.2 Sums of squares 46
4.3 Pythagorean triples 47
5 Continued fractions 49
5.1 Definitions and examples 49
5.2 Best rational approximations 55
5.3 Quadratic irrationals 61
6 Pell’s equation
√ 67
6.1 Rings u�[ u�] √ 67
6.2 Calculating the CF-expansion of u� 68
7 Arithmetic functions 72
7.1 Dirichlet ring 72
7.2 Möbius inversion 73
ii algebra 77
1 Groups 78
1.1 Basic definitions 78
1.2 Group actions 84
1.3 Conjugacy classes 85
2 Permutations and ordered sets 87
2.1 Permutation group u�u� 87
2.2 Ordered sets and inversions 90
2.3 Conjugates and commuting elements 91
3 Linear recurrences 93
3.1 Basic theory 93
3.2 Generating functions 95
3.3 Components of the recursion and closed formulas 97
iii combinatorics 98
1 Generating functions 99
1.1 Formal power series 99
1.2 Analytic functions 100
1.3 Solving recurrence relations and sums 102
2 Combinatorial species 106
2.1 Definition 106
2.2 Combinatorial equality, embeddings and coverings 107
2.3 Associated generating functions 107
2.4 Operations on species 108
2.5 Basic species 110
2.6 Derived species 112
3 Pólya theory 116
3.1 Colored species 116
3.2 Quotient species 119
iv game theory 124
1 Impartial games 125
1.1 Nimbers 125
1.2 Sum games 126
Index 127
foreword
Janne Junnila
number theory
P
Classical number theory deals with properties of integers. The set
of all integers is usually denoted by the symbol u�, and the set of
all non-negative integers is denoted by u�.
In this first part of the book we will approach classical number
theory with modern methods. When digested, ring theory provides
a unified way of thinking about many topics that traditionally were
A
hard problems of their own. This is why we will introduce the basics
of commutative rings at the same time as we navigate through the
foundations of elementary number theory.
The algebra developed will serve as a foundation for further
topics where such tools become indispensable. The methods will be
used to reason about diophantine equations (Pythagorean triples,
Pell’s equation, etc.) by extending the ring of integers and exam-
R
ining the equations in these extensions.
T
I
algebraic structure of u�
1
In this chapter we will focus on the algebraic structure of the in-
tegers. In other words we will look at the interplay of addition
and multiplication, exploring topics such as greatest common divi-
sors or prime numbers. Many of these topics may already be quite
familiar to most of the readers. Therefore the emphasis is on a co-
herent and powerful point of view based on notions from abstract
algebra. To accomplish this, we will introduce some basic theory of
commutative rings as we go.
Definition 1.1 A set u� together with binary operations + and ⋅ form a commu-
tative ring if the following properties hold:
identity elements − there exist elements 0 ∈ u� and 1 ∈ u� such that for all u� ∈ u�,
u� + 0 = 0 + u� = u�
u� ⋅ 1 = 1 ⋅ u� = u�
additive inverses − for every u� ∈ u� there exists an element −u� ∈ u� such that
u� + (−u�) = 0
u� + u� = u� + u�,
u� ⋅ u� = u� ⋅ u�
It is easy to see that the integers under the usual addition and
multiplication form a commutative ring. Other examples include
the rational (u�), real (u�) and complex (u�) numbers.
One difference between u� and the other three rings mentioned
is the following: An element u� ∈ u� is called invertible (or a unit)
if there exists an element u� ∈ u� such that u�u� = 1. Clearly the only
invertible integers are 1 and −1. Contrast to that, in the rings u�,
u� and u� all elements except 0 are invertible.
Definition 1.2 A non-zero (i.e. u� ≠ {0}) commutative ring where every non-zero
element is invertible is called a field.
Exercise 1.5 Show that u� is an integral domain if and only if it has the following
cancellative property: if u�, u�, u� ∈ u� and u� ≠ 0, then the equality
u�u� = u�u� implies that u� = u�.
Exercise 1.6 Let u� = {0, 1, u�, u�} be a ring of 4 elements such that 1 + 1 = u�.
Show that such a ring exists and is unique and that it is not an
integral domain.
We will next describe ideals, which are a way to extend the notion
of divisibility. Assume that u� and u� are two integers that are both
divisible by some integer u� ≠ 0. Then also u� + u� is divisible by
u�. Moreover you cannot remove this factor by multiplying: u�u� and
u�u� are still going to be divisible by u� for any integer u�. These two
properties are abstracted away into the definition of an ideal.
Example 1.8 The set {…, −12, −6, 0, 6, 12, …} is an ideal in u�. It is generated
by 6, and a number is divisible by 6 if and only if it lies in this set.
We will see in the next section that u� is a PID. Every field is also
a PID, in fact look at the following exercise.
Let u�, u� ∈ u�. If u� ∈ ⟨u�⟩, we say that u� divides u�, which we also
write as u� | u�. This is the same as saying that there exists u� ∈ u�
such that u� = u�u�. If u� is an integral domain and u� ≠ 0, such u� is
unique when it exists. (Why?)
One of the reasons we are interested in ideals is that we can
form new rings by considering the elements modulo u� for some ideal
u�.
Definition 1.13 Let u� ⊂ u� be an ideal. Two elements u�, u� ∈ u� are said to be equiv-
alent modulo u� if u� − u� ∈ u�. Let u�/u� denote the set of equivalence
classes [u�] under this relation. Then u�/u� can be made into a ring
by defining [u�] + [u�] = [u� + u�] and [u�] ⋅ [u�] = [u� ⋅ u�]. Rings obtained
this way are called quotient rings.
Exercise 1.14 Show that the definition makes sense. First show that u� ∼ u� ⇔
u� − u� ∈ u� really is an equivalence relation. Then show that the def-
initions of addition and multiplication do not depend on the choice
of representatives.
• u� ≠ u�.
is the set of prime numbers, which are the positive prime ele-
ments.
Theorem 1.17 An ideal u� is a prime ideal if and only if u�/u� is an integral domain.
Proof. Exercise. □
Proof. Assume first that u� is a maximal ideal. Let [u�] ∈ u�/u�. Assume,
to obtain a contradiction, that there does not exist [u�] ∈ u�/u� such
that [u�] ⋅ [u�] = [1]. Consider the ideal ⟨[u�]⟩ ⊂ u�/u�. It is not the
whole u�/u� because [1] ∉ ⟨[u�]⟩. Let u� = {u� ∈ u� : [u�] ∈ ⟨[u�]⟩}. Then
u� is an ideal and u� ⊊ u� ⊊ u�, which is a contradiction.
Assume then that u�/u� is a field. Suppose that u� ⊊ u� ⊊ u� for
some ideal u�. Then there exists u� ∈ u� ∖ u�. Because u�/u� is a field,
[u�] ∈ u�/u� has an inverse [u�] ∈ u�/u� meaning that u�u� − 1 ∈ u� ⊂ u�.
But this means that 1 ∈ u�, so u� = u�, a contradiction. □
Let us end the section by noting that since fields are integral do-
mains, every maximal ideal is a prime ideal by the previous theo-
rems.
Euclidean domains are rings for which there exists a Euclidean al-
gorithm. For the Euclidean algorithm to work, it is necessary that
division yields remainders of decreasing size. In an arbitrary ring
Euclidean domains 14
u� = u�u� + u�
Example 1.23 In u� the numbers 5 and −5 are greatest common divisors of the
numbers −15 and 35. These are actually the only greatest common
divisors of the two numbers and they differ by multiplication by −1.
The least common multiples of the numbers −15 and 35 are −105
and 105.
Proof. Exercise. □
In the case of u�, it is customary to choose gcd(u�, u�) and lcm(u�, u�)
to be the non-negative variants.
Euclidean domains 15
− If u� and u� are two ideals, so is u� ∩ u�. In a PID the ideal ⟨u�⟩ ∩ ⟨u�⟩
is generated by any least common multiple of u� and u�.
Example 1.25 One can check that ⟨6⟩ + ⟨15⟩ = ⟨3⟩ and that ⟨6⟩ ∩ ⟨15⟩ = ⟨30⟩.
Theorem 1.26 If u� is a Euclidean domain, then every pair of elements u�, u� ∈ u�∖{0}
has a greatest common divisor u� ∈ u� and there exist u�, u� ∈ u� such
that
Proof. Let u�0 = u� and u�1 = u�. Then we can find a sequence of elements
u�1 , u�2 , …, u�u� and u�2 , u�3 , …, u�u� such that
where u�u� = 0 and u�(u�u�−1 ) < u�(u�u�−2 ) < … < u�(u�2 ) < u�(u�1 ).
Let u� ≔ u�u�−1 . Then u� divides u�u�−2 , and it also divides u�u�−3
because u�u�−3 = u�u�−2 u�u�−2 + u�u�−1 . Continuing this reasoning induc-
tively up the equation chain we see that u� divides every u�u� , including
u�0 = u� and u�1 = u�.
Conversely any common divisor u�′ of u� and u� must divide each
u�u� , so in particular u�′ | u�. This means that u� is a greatest common
divisor. Solving u�2 = u�0 − u�1 u�1 , substituting it to the next equa-
tion and solving u�3 and continuing like this, we will end up with a
representation (1.1). □
Algorithm 1.27 (Extended Euclidean algorithm) The following calculates the great-
est common divisor u� of u� and u� as well as u� and u� such that
u�u� + u�u� = u�.
Now we finally see that all the ideals of u� are of the form ⟨u�⟩ for
some u� ∈ u�.
Let us close the section with the following longish (but impor-
tant if you haven’t seen it before) exercise.
Exercise 1.29 An equation of the form u�u� + u�u� = u� where u�, u�, u� ∈ u� is called a
linear diophantine equation. Assume that u�, u�, u� are non-zero.
The term composite In u� the irreducible elements are the same as the prime elements.
seems to be non- This is not the case for all commutative rings in general, although
standard in ring for integral domains prime elements are irreducible. In u� reducible
theory. I like it elements are also called composite. We will use the same term
in the context of also in unique factorization domains that will be defined shortly.
UFDs, however.
Unique factorization 18
Proof. Exercise. □
The happy thing is that the converse holds in principal ideal do-
mains, and more generally in unique factorization domains (which
will be defined soon).
As we saw above, this chain must settle, so at some point u�u�1 u�2 …u�u�
and u�u�1 u�2 …u�u�+1 generate the same ideal. This means that u�u�1 u�2 …u�u� =
u�u�u�1 u�2 …u�u�+1 for some unit u�, which is a contradiction. Thus the
expanding of terms ends, and every u� ∈ u� admits a factorization
into irreducibles.
We will conclude the proof by showing that the factorization is
unique. Assume that u� = u�u�1 u�2 …u�u� = u�u�1 u�2 …u�u� where u�1 , …, u�u�
and u�1 , …, u�u� are irreducibles and u� and u� are units. Because u�1
divides u�1 u�2 …u�u� , and because u�1 is a prime, there must exist u�u� such
that u�1 | u�u� . Since u�u� is irreducible, we see that when we cancel u�1
and u�u� , we are left with a unit. We can therefore reduce to the
case where there are 1 less factors on both sides and continue by
induction. □
2 One might ask why we proved this for PIDs in the first place. The reason is
that we used the result in the proof that a PID is a UFD.
Primes and factoring in u� 20
By unique factorization one of the u�1 …u�u� u�1 …u�u� must equal u� mul-
tiplied by a unit. If it is one of the u�u� s, then u� ∈ ⟨u�⟩, otherwise
u� ∈ ⟨u�⟩. Thus ⟨u�⟩ is a prime ideal and u� is a prime element. □
It is time to take a break from the algebraic mumbo jumbo and see
how what we have established can be used in the setting of u�.
Since u� is a PID, we know that the prime elements are the
same as the irreducibles. Suppose that we want to check if u� ∈ u�
is a prime. Without loss of generality we can assume that u� ≥ 2.
Since u� is a UFD, we can write
u� u�
u� = u�1 1 …u�u�u�
where u�1 < … < u�u� are positive primes and u�1 , …, u�u� ≥ 1. This
is called the prime factorization of u�. If u� was composite, we
would either have u�1 ≥ 2, in which case u�12 ≤ u�, or we would
have u�1 = 1 and u� ≥ 2, in which case u�12 < u�1 u�√
2 ≤ u�. Thus every
composite u� has a prime factor of size at most ⌊ u�⌋. This leads to
the following simple algorithm for checking whether a given number
is prime.
bool is_prime(int64_t m) {
for(int64_t i=2;i*i<=m;i++) {
if(m % i == 0) return false;
}
return true;
}
√
The running time of the algorithm is u�( u�). It should be noted
that (much) more efficient tests exist.
Algorithm 1.37 (Sieve of Eratosthenes) The idea is to start from 2 and rule out
composite numbers whenever new primes are found. Indeed, first
we mark 4, 6, 8, … as composites. Then we move on to 3 and mark
3, 6, 9, 12, 15, … Since we have already marked 4, we skip it and
move to 5, which is our third prime. This is repeated until we
reach u�.
vector<int64_t> sieve(N+1,0);
for(int64_t p=2;p<=N;p++) {
if(sieve[p] == 0) {
for(int64_t i=p;i<=N;i+=p) {
sieve[i] = p;
}
}
}
Algorithm 1.38 (Factoring using a sieve) Assume that we have built a factor sieve
up to u� and want to factor u� ≤ u�. This is easily done as follows.
vector<P> factors;
while(m != 1) {
int64_t p = sieve[m];
int64_t a = 0;
while(m%p == 0) {
m/=p;
a++;
}
factors.push_back(P(p, a));
}
Ring homomorphisms 22
In the end factors will contain a pair (u�u� , u�u� ) for each prime factor
u�
u�u�u� of u�.
Definition 1.39 Let u� and u� be two commutative rings. A map u�∶ u� → u� is called
a ring homomorphism if u�(0) = 0, u�(1) = 1 and for all u�, u� ∈ u�
we have
u� u�
u�(u� + u�u�) = ( )
−u� u�
Definition 1.41 Given a ring homomorphism u�∶ u� → u�, the set of elements mapping
to 0 under u� is denoted by Ker u� and called the kernel of u�.
For example the kernel of the map u�u� above is simply u�.
Theorem 1.42 Let u�∶ u� → u� be a ring homomorphism. Then Ker u� is an ideal and
˜ u�/Ker u� → u�
there exists a unique injective ring homomorphism u�∶
˜
such that u� = u� ∘ u�Ker u� .
Proof. Exercise. □
Exercise 1.43 Show that the inverse mapping u� −1 of a ring isomorphism is also a
ring isomorphism.
Definition 1.44 Let u�∶ u� → u� be a ring homomorphism. The image of u� is the set
Im u� ≔ u�(u�).
Proof. Exercise. □
Definition 1.46 If u� and u� are two commutative rings, then we can form their
product ring u� × u� by taking all the tuples (u�, u�) with u� ∈ u� and
u� ∈ u� and defining the ring operations component wise, i.e.
Definition 1.47 Let u� and u� be ideals in u�. Their product ideal u�u� is the ideal
generated by all products u�u�, u� ∈ u� and u� ∈ u�, i.e.
Chinese remainder theorem 24
u�u� ≔ {u�1 u�1 u�1 + … + u�u� u�u� u�u� : 1 ≤ u� ≤ u�, u�u� ∈ u�, u�u� ∈ u�, u�u� ∈ u� }.
Theorem 1.48 Let u� and u� be two coprime ideals of a commutative ring u�. Then
u� ∩ u� = u�u� and there exists a ring isomorphism
Proof. Let us first show that u� ∩ u� = u�u�. If u� ∈ u�u�, then it is of the form
u� = u�1 u�1 u�1 + … + u�u� u�u� u�u� , where u�u� ∈ u�, u�u� ∈ u� and u�u� ∈ u�. It is
clear that such an element belongs both to u� and u�. If u� ∈ u� ∩ u�,
then since u� + u� = u�, we have u� + u� = 1 for some u� ∈ u�, u� ∈ u�, and
thus u� = u�u� + u�u� ∈ u�u�.
Consider the map u�∶ u� → u�/u� × u�/u� given by
for all u� ∈ u�. It is clear that Ker u� = u� ∩ u� = u�u�. Thus the claim
will follow from the factorization theorem once we show that u� is
surjective. Let u�, u� ∈ u� be arbitrary and set u� = u�u� + u�u� where
u� ∈ u� and u� ∈ u� are such that u� + u� = 1. Then u�u� (u�) = u�u� (u�) and
u�u� (u�) = u�u� (u�). Thus the arbitrary element (u�u� (u�), u�u� (u�)) belongs
to Im u� and u� is surjective. □
Theorem 1.49 Let u� be a PID and u�1 , …, u�u� pairwise coprime elements of u�. Then
there exists an isomorphism
This lets us calculate u� for any integer for which we know its prime
factorization.
Lagrange’s theorem for groups tells us that u�u�(u�) = 1 for all
u� ∈ (u�/u�u�)∗ . In this setting this is known as Euler’s theorem. In
the case where u� = u� is a prime number it is also called Fermat’s
little theorem, which states that u�u�−1 = 1 for all u� ∈ u�/u�u�, u� ≠ 0.
{ u�1 ,
⎧ if u� = 1,
u�
(u�/2 u�) ≅ u�2 ,
⎨
∗
if u� = 2,
{
⎩ u�2 × u�2u�−2 , if u� ≥ 3.
squaring, we see that there exist odd numbers u�0 , …, u�u�−2 such
that
0
52 = 1 + u�0 ⋅ 22 ,
1
52 = (1 + u�0 ⋅ 22 )2 = 1 + u�1 ⋅ 23 ,
2
52 = (1 + u�1 ⋅ 23 )2 = 1 + u�2 ⋅ 24 ,
⋮
u�−3
52 = 1 + u�u�−3 ⋅ 2u�−1 ,
u�−2
52 = 1 + u�u�−2 ⋅ 2u� ,
Structure of the unit group 28
Notice in particular that the group (u�/2u� u�)∗ is not cyclic for any
u� ≥ 3 because every element has order at most 2u�−2 .
Let us next consider the case where u� > 2. We will begin by
showing that (u�/u�u�)∗ is cyclic. First two small lemmas.
Proof. Exercise. □
Theorem 2.3 Let u� be a finite abelian group. If u� is the least common multiple
of the orders of the elements in u�, then u� contains an element of
order u�.
u� u�
Proof. Let u�1 1 …u�u�u� be the prime factorization of u�. Then there exist
u�
u�˜1 , …, u�˜u� ∈ u� such that u�u� u� divides the order of u�˜u� . Define u�u� =
u�u�
ord(˜
u� )/(u� ) u�
u�˜u� u� u�
. Then each u�u� has order u�u� u� . Clearly the subgroups
generated by u�u� intersect only at {1}, for an element belonging to
u�
the intersection of ⟨u�u� ⟩ and ⟨u�u� ⟩ has order that divides both u�u� u� and
u�
u�u� u� . Therefore u� contains a subgroup isomorphic to ⟨u�1 ⟩×⋯×⟨u�u� ⟩,
which is a cyclic group of order u�. □
Proof. Let u� be the least common multiple of the orders of the elements
in (u�/u�u�)∗ . Then since there exists an element of order u� and
because its order divides u� − 1, we must have u� ≤ u� − 1.
On the other hand consider the polynomial u� (u�) = u�u� − 1.
Every element of (u�/u�u�)∗ is a root of u� because their orders divide
u�. The number of roots is at most u�, so we must have u� − 1 ≤ u�.
This means that u� = u� − 1 and the group must be cyclic. □
Finally, let us tackle the general case (u�/u�u� u�)∗ for odd prime u� and
u� ≥ 1.
Primitive roots 29
Theorem 2.5 Let u� be an odd prime and u� ≥ 1. Then (u�/u�u� u�)∗ ≅ u�(u�−1)u�u�−1 .
Proof. The case u� = 1 was proved above. Let u� ≥ 2. We can use a similar
method as we used with u� = 2. There we were able to start with
5 and repeatedly squared it, but here we will have to be a little bit
more careful. Let u� be a generator for (u�/u�u�)∗ . Then we claim
that either u�u�−1 or (u� + u�)u�−1 is of the form 1 + u�0 u�, where u�0 is
not divisible by u�. Assume that u�u�−1 is not of this form. Then by
the binomial theorem
where the rest of the terms are divisible by u�2 . Now by assumption
u�u�−1 = 1 + u�u� with u� divisible by u�, so we see that (u� + u�)u�−1 is
of the wanted form. The rest of the argument goes as with u� = 2,
but instead of squaring we raise the previous number to power u�
repeatedly. Finishing the proof is an exercise. □
u� u�
Let us collect the results of this section. If u� = u�1 1 …u�u�u� , then
u� u�
(u�/u�u�)∗ ≅ (u�/u�1 1 u�)∗ × ⋯ × (u�/u�u�u� u�)∗ , which is cyclic if and
only if u� is one of 2, 4, u�u� , 2u�u� where u� is an odd prime and u� ≥ 1.
exponentiation by squaring.
Definition 2.8 Let u� ≥ 1 be an odd number and u� ∈ (u�/u�u�)∗ . Consider the map
u�u� ∶ (u�/u�u�)∗ → (u�/u�u�)∗ given by u�u� (u�) = u�u�. The Jacobi symbol
( u�u� ) is defined to be equal to sgn(u�u� ), the sign of the permutation
u�u� .
u� 1, if u� is a square,
( )={
u� −1, if u� is a non-square.
u�−1
Proof. Let u� ∈ (u�/u�u�)∗ . Clearly u� 2 = ±1 because u�/u�u� is a field. The
u�−1
map u�∶ u� ↦ u� (modu�) can therefore thought of as a homomor-
2
u� u� u�−1 u�−1
( ) ( ) = (−1) 2 ⋅ 2 .
u� u�
Proof. Identify each of the rings u�/u�u�, u�/u�u� and u�/u�u�u� with the nat-
ural numbers {0, 1, …, u� − 1}, {0, 1, …, u� − 1} and {0, 1, …, u�u� − 1}
respectively. Then we can give each of the rings a total order in-
duced by the usual order on natural numbers.
Next consider the ring u�/u�u� × u�/u�u�. We can give it two
different total orders:
− The u�-major order <u� where (u�, u�) <u� (u�′ , u�′) if and only if
u� < u�′ or u� = u�′ and u� < u�′ .
− The u�-major order <u� where (u�, u�) <u� (u�′ , u�′) if and only if
u� < u�′ or u� = u�′ and u� < u�′ .
Now there exist unique maps u�u� , u�u� ∶ u�/u�u� × u�/u�u� → u�/u�u�u�
that are isomorphisms from the orders <u� and <u� respectively to
the order defined on u�/u�u�u�. These maps are given by
u�u� (u�, u�) = (u�u� + u�, u�) and u�u� (u�, u�) = (u�, u� + u�u�).
Squares and square roots 33
The sign of the permutation u�u� is the same as the sign of the
permutation u� ↦ u�u� + u� on u�/u�u� because u� is odd and u�u� is
constant in u�/u�u�. The permutation u� ↦ u�u� + u� is the composition
of the two permutations u� ↦ u�u� and u� ↦ u� + u�. The first one has
u� u�
sign ( u� ) and the second one has sign 1, so sgn(u�u� ) = ( u� ) and
similarly sgn(u�u� ) = ( u�
u� ).
Consider now the permutation u� = u�−1 u�
∘ u�u� = u�u�−1 ∘ u�u� .
The sign of u� is the product of the signs of u�u� and u�u� , which
u�
is ( u� ) ( u�
u� ). We will now double count this sign. Notice that u�
is the unique isomorphism between the total orders <u� and <u� .
To count the sign of the permutation u�, it is enough to count the
number of inversions with respect to the order <u� . By definition
we are looking for the number of pairs (u�, u�), (u�′ , u�′) such that
(u�, u�) <u� (u�′ , u�′) and u�(u�, u�) >u� u�(u�′ , u�′).
Now u�(u�, u�) >u� u�(u�′ , u�′) is equivalent with (u�, u�) >u� (u�′ , u�′), so
by the definition of <u� and <u� we must have u�′ < u� and u� < u�′ .
There are (u� 2 )( 2 ) solutions to these inequalities, so
u�
What about ( u�
u� ) when u� is even? The following gives a starting
point.
2 u�2 −1
( ) = (−1) 8 .
u�
u�−1
are no inversions for pairs (u�, u�) in the ranges 1 ≤ u� < u� ≤ 2 or
u�+1
2 ≤ u� < u� ≤ u� − 1. If 1 ≤ u� ≤ u�−1 u�+1
2 and 2 ≤ u� ≤ u� − 1, then
2u� > 2u� − u� if and only if u� ≤ u� + u�−1
2 . Thus we have
u�−1 u�−1
2 2 u�−1 u�+1
u� − 1 u� + 1 2 2 u�2 − 1
∑(u� + − + 1) = ∑ u� = =
u�=1
2 2 u�=1
2 8
u�2 −1
inversions in total, giving sgn(u�2 ) = (−1) 8 . □
The final piece in the repertoire for Jacobi symbols is that it is
completely multiplicative in both the top argument and the bottom
argument.
Theorem 2.13 Let u� ≥ 1 be odd and u�, u� ∈ (u�/u�u�)∗ . Then we have
u�u� u� u�
( ) = ( )( ).
u� u� u�
where on the right hand side we have identified u� with its im-
ages in (u�/u�u�)∗ and (u�/u�u�)∗ under the canonical quotient maps
u�/u�u�u� → u�/u�u� and u�/u�u�u� → u�/u�u�.
Proof. The first claim is trivial by the definition of Jacobi symbol.
The second claim follows from the first one by picking odd
representatives for u� and using the law of quadratic reciprocity.
□
the multiplicativity.
Squares and square roots 35
u� u� u�2 −1
3. Any term of the form ( 2u� ) equals ( u�2 ) = (−1)u� 8 .
4. For terms of the form ( u� u� ) with both u� and u� odd we can use
the law of quadratic reciprocity to swap the places of u� and u�,
u�−1 u�−1
multiply by (−1) 2 2 and start over from step 1.
Theorem 2.14 The only squares modulo 4 are 0 and 1. Let u� = 2u� with u� ≥ 3.
Then u� is a square modulo u� if and only if it is of the form 4u� (8u�+1)
for some u� ≥ 0 and u� ∈ u�.
Proof. Exercise. □
Proof. Exercise. □
solve the square root of (8u� + 1) and multiply the obtained number
by 2u� to get a square root of u�.
Similar ‘lifting’ works also for odd prime powers. Assume that
u� = u�u� , u� is not divisible by u�, and we have already solved u�2 = u� in
u�/u�u�−1 u�, pick a representative of u� in u�/u�u� u� and let u� be such that
u�2 +u�u�u�−1 = u� in u�/u�u� u�. Then (u�+u�2−1 u�−1 u�u�−1 )2 = u�2 +u�u�u�−1 ,
so u� + u�2−1 u�−1 u�u�−1 is our solution in u�/u�u� u�. In total there will
be two solutions in this case since there are two solutions in u�/u�u�.
Thus all that is left is to handle the base case u� = u�, where u�
is an odd prime. In the case u� ≡ 3 (mod 4) this is easy.
Theorem 2.16 If u� ≡ 3 (mod 4) and ( u�u� ) = 1, then the solutions to u�2 = u� are
u�+1
given by ±u� 4 .
Proof. Exercise. □
Then u�u� lies in the subgroup generated by u�, and we can thus write
for some integer u�. Because u� is a square, also u�u� must be a square,
implying that u� must be even. Thus if we are capable of solving the
discrete logarithm problem in the 2u� element subgroup of (u�/u�u�)∗ ,
u�+1 u�
then u� 2 u�− 2 is the square root we are after and we are done.
So here is the algorithm. We will set u�0 = u�u� and u�0 = u�.
On each step u� ≥ 0 our goal is to recursively solve the discrete
logarithm problem u�u�u� = u�u� for u�(u�) . We assume that the order of
(u�)
Squares and square roots 37
u�u� is 2u�u� for some u�u� ≥ 0, that u�u� lies in the subgroup generated by
u�u� and that u�(u�) will be even, so that we may write it in binary as
(u�) (u�) (u�)
u�(u�) = u�1 21 + u�2 22 + … + u�u�u� −1 2u�u� −1 .
(u�) (u�)
(u�) u�u� u�1 2u�u� +1 +…+u�u� 2u�u� −1
1 = u�u�u� 2
= u�u� u� −u�u� −1
,
(u�) (u�)
so we must have u�1 = … = u�u�u� −u�u� −1 = 0. Similarly since u�2u� u�
u� −1
≠ 1,
we must have
(u�)
(u�) u�u� −1 u�u�u� −u�u� 2u�u� −1
1 ≠ u�u�u� 2
= u�u� ,
(u�)
so u�u�u� −u�u� = 1. This means in particular that
(u�) (u�)
u�u� −u�u� u�u� 2u�u� −u�u� +1 +…+u�u� −1 2u�u� −1
u�u�−2 u�u� = u�u� u� −u�u� +1 u�
,
{
if(p % 4 == 3) {
return power_mod(a, (p+1)/4, p);
}
int64_t odd=p-1;
int64_t s=0;
while(odd%2 == 0) {
odd/=2;
s++;
}
int64_t g=2;
while(g < p) {
if(power_mod(g, (p-1)/2, p) == p-1) break;
g++;
}
g = power_mod(g, odd, p);
int64_t tmp=power_mod(a, (odd - 1)/2, p);
int64_t x=tmp*tmp%p*a%p;
int64_t r=tmp*a%p;
while(x != 1) {
int64_t t=1;
int64_t tmp=x*x%p;
while(tmp != 1) {
tmp=tmp*tmp%p;
t++;
}
tmp=power_mod(g, (1 << (s-t-1)), p);
r=r*tmp%p;
g=tmp*tmp%p;
x=x*g%p;
s=t;
}
return r;
}
more about primes
3
In this chapter we will look at a few problems specifically related
to the primes in u�, such as testing whether a number is prime or
counting the number of primes less than some given number u�.
− u�u� = 1, or
= −1 for some 0 ≤ u� ≤ u� − 1.
u�
− u�2 u�
Algorithm 3.1 (Miller–Rabin primality test) Let u� be the number we want to test
for primality. We will randomly choose elements u� ∈ (u�/u�u�)∗ and
see if both of the conditions above fail. In this case we will know
that u� is not a prime. Otherwise after suitably many elements u�
have been tested, we will conclude that u� is probably a prime.
Notice that if u� ∈ u�/u�u� and u� does not belong to the multi-
plicative group, then u� will automatically fail the test, so we can
simply choose our u� from the range 2 ≤ u� ≤ u� − 1.
Miller–Rabin primality test 40
bool is_prime_miller_rabin(uint64_t p) {
uint64_t oddpart=p-1;
uint64_t evenpart=1;
while(oddpart%2 == 0) { evenpart*=2; oddpart/=2; }
std::default_random_engine gen;
std::uniform_int_distribution<int64_t> unif(2, p-1);
for(uint64_t i=0;i<NUMBER_OF_WITNESSES;i++)
if(is_witness(unif(gen), evenpart, oddpart, p))
return false;
return true;
}
One may ask how reliable the test is. It is possible to show that if
u� is the number of potential witnesses we test, the probability that
we claim that a composite number is prime is less than 4−u� .
We can make a deterministic version of Miller–Rabin by choos-
ing a fixed set of numbers u� to test. Indeed at [Link]
-[Link] there are precalculated sets of numbers
available that work for large ranges of numbers. In particular the
seven number set
Counting prime numbers 41
is claimed to work for every number of size at most 264 . The al-
gorithm has to be modified so that we test the numbers in the set
in order, and if a number is divisible by u�, then we report that u�
is prime. The set has been chosen so that the previous numbers
would have rejected any number u� that is composite and for which
the current u� is divisible by u�, so that no false-positives occur. Be-
low is an implementation of this fast primality checking algorithm.
bool is_prime_miller_rabin_deterministic(uint64_t p) {
uint64_t odd=p-1;
uint64_t even=1;
while(odd%2 == 0) { even*=2; odd/=2; }
for(uint64_t i=0;i<7;i++) {
uint64_t a = bases[i]%p;
if(a == 0) return true;
if(is_witness(a, even, odd, p)) return false;
}
return true;
}
which
√ follows from the fact that every number u�u� ≤ u� with u�, u� >
3
u� will be counted
√ twice√ in the sum, except when u� = u�, which
the term u�(⌊ u�⌋) − u�(⌊ u�⌋) corrects.
3
The function u�(u�, u�) on the other hand satisfies the recursion
u�
u�(u�, u�) = u�(u�, u� − 1) − u�( , u� − 1).
u�u�
4
In this chapter we will use the ring theory we have developed in the
context of Gaussian integers. Gaussian integers are numbers of
the form u� + u�u�, where u� and u� are integers. The set of all Gaussian
integers is denoted by u�[u�]. They are useful for many tasks in
classical number theory, mainly because they allow factoring u�2 +u�2
as (u� + u�u�)(u� − u�u�).
Proof. Define u�∶ u�[u�] → u� by setting u�(u� + u�u�) = |u� + u�u�|2 = u�2 + u�2 . We
must show that for any u�, u� ∈ u�[u�], u� ≠ 0, there exist u�, u� ∈ u�[u�]
such that
Notice that there But it is clear that this happens, because if we choose u� to be
may be up to four a nearest Gaussian integer to u�u� in the complex plane, then its
nearest Gaussian in- distance to u�
cannot be more than half of the diagonal of the unit
u�
tegers for a given √
2
complex number. square, which is 2 < 1. □
Here it is enough to
It follows immediately that many of the good properties we are used
choose one of them.
to while working with integers apply to Gaussian integers too. In
particular they are a PID and a UFD.
Let u� (u�) = |u�|2 for all u� ∈ u�[u�]. The function u� ∶ u�[u�] → u�
is called a norm. Its main property is that u� (u�u�) = u� (u�)u� (u�).
Therefore if u� ∈ u�[u�] is a unit, we must have
Structure of u�[u�] 45
Theorem 4.2 Let u� ∈ u�[u�]. Then u� a prime element if and only if it is one of the
following up to multiplication by a unit:
− 1 + u�,
where u� are primes that are 1 modulo 4 and u� are primes that
are 3 modulo 4. The number u� is a sum of squares if and only if
there exists u� ∈ u�[u�] such that u� (u�) = u�u� = u�. But by unique
factorization this means that u� must have a factor 1 + u� occuring
u�0 times, a split factor occuring u�u� times for 1 ≤ u� ≤ u� and u�u�
occuring u�u� /2 times for 1 ≤ u� ≤ u�. This is possible if and only if all
u�u� are even.
It is also easy to count the number of ways an integer u� can
be expressed as a sum of squares. Let
u� u� u� u�
u� = 2u�0 u�1 1 …u�u� u� u�1 1 …u�u� u� ,
Pythagorean triples 47
where u�u� are primes that are 1 modulo 4 and u� are primes that
are 3 modulo 4. Let u�(u�) be the number of ways to write u� =
u�2 + u�2 where u�, u� ∈ u�. Then if one of u�u� is odd, we must have
u�(u�) = 0. Otherwise we must consider all possible ways of writing
u� /2
u� = (u� + u�u�)(u� − u�u�). Now in any case u� + u�u� must have u�u� u� as a
factor, so there is no choice involved there. Similarly it must have
(1 + u�)u�0 as a factor. Let u� + u� = u�u� for some 1 ≤ u� ≤ u�. To have
u�
u�u� u� in the prime factorization of (u� + u�u�)(u� − u�u�), we can choose
(u�u� + u�u� u�)u� (u�u� − u�u� u�)u� with u� + u� = u�u� to be a factor of u� + u�u�. Here
we have written u�u� = (u�u� + u�u� u�)(u�u� − u�u� u�) and the exponents u� and
u� can be chosen in u�u� + 1 different ways for each u�. Finally we can
multiply our number by any of the 4 units, which gives us in total
ways.
Now notice that u� + u�u� and u� − u�u� are coprime. Indeed, if this was
not the case, then 2u� = (u� + u�u�) + (u� − u�u�) and u� + u�u� would have
a common factor. Because u� and u� are coprime, this implies that
Pythagorean triples 48
1 2 2 −1 2 2 1 −2 2
⎛
⎜2 1 2⎞⎟, ⎛
⎜−2 1 2⎞
⎟, ⎛
⎜2 −1 2⎞
⎟
⎝2 2 3⎠ ⎝−2 1 3⎠ ⎝2 −2 3⎠
Suppose that u� is a real number and let u�0 ≔ u�. If u�0 is not an
integer, then we may write
u�0 = u�0 +
1
,
u�1
1
where u�0 = ⌊u�0 ⌋ and u�1 = u� −⌊u� > 1. If u�1 is not an integer,
0 0⌋
then we may continue and write
1
u�1 = u�1 + ,
u�2
1
where u�1 = ⌊u�1 ⌋ and u�2 = u�1 −⌊u�1 ⌋ > 1. Continuing like this we get
the (formal) equality
1
u� = u�0 + 1
,
u�1 + 1
u�2 + …
Exercise 5.1 Show that if u� is not an integer and the continued fraction expan-
sion of u� is finite, then the last coefficient of the expansion is at
least 2.
Definitions and examples 50
Theorem 5.2 The continued fraction expansion of a real number u� has finitely
many coefficients if and only if u� is rational.
has infinitely many terms. Note however that 0 ≤ u� − u� ⌊ u�u� ⌋ < u�,
which is a contradiction. □
Let [u�0 , u�1 , u�2 , …] be a continued fraction. Then its u�th conver-
gent is defined to be the continued fraction [u�0 , u�1 , u�2 , …, u�u� ]. If
the coefficients u�0 , …, u�u� are integers, it is a rational number and
we will use the convention that its numerator and denominator (in
lowest terms) are denoted by u�u� and u�u� respectively.
Definitions and examples 51
Theorem 5.3 The u�u� and u�u� satisfy the recursion relations
Proof. Our first claim is that if u�u� and u�u� are defined via the given recur-
sion relations, then u�u�u� = [u�0 , u�1 , …, u�u� ]. The proof will proceed by
u�
induction on the length of the continued fraction and we do not as-
sume that u�0 , u�1 , …, u�u� are integers. It is clear that the claim holds
for all continued fractions of length 1. Assume that the recursion
relations hold for all continued fractions of length u�. Then we can
bunch together the last two coefficients of the continued fraction
[u�0 , u�1 , …, u�u� ] and write it as [u�0 , u�1 , …, u�u�−2 , u�u�−1 + u�1 ], which is
u�
now a continued fraction of length u�. It has the same initial seg-
ment [u�0 , u�1 , …, u�u�−2 ] as [u�0 , u�1 , …, u�u� ] does, so the convergents up
to u�u�u�−2 are equal for them. By induction [u�0 , u�1 , …, u�u�−2 , u�u�−1 + u�1 ]
u�−2 u�
is given by
1
(u�u�−1 + u�u� )u�u�−2 + u�u�−3 u�u� (u�u�−1 u�u�−2 + u�u�−3 ) + u�u�−2
=
(u�u�−1 + 1 u�u� (u�u�−1 u�u�−2 + u�u�−3 ) + u�u�−2
u�u� )u�u�−2 + u�u�−3
u�u� u�u�−1 + u�u�−2
= ,
u�u� u�u�−1 + u�u�−2
which shows that the claim holds for continued fractions of length
u� + 1.
The second claim is that u�u� and u�u� are coprime integers if
u�0 , u�1 , …, u�u� are integers. That u�u� and u�u� are integers is obvious
from the recursion. The coprimality follows from the next theorem.
□
Theorem 5.4 The numbers u�u� and u�u� satisfy u�u� u�u�−1 − u�u�−1 u�u� = (−1)u�−1 for all
u� ≥ −1.
Definitions and examples 52
Proof. It is trivial to check that u�−1 u�−2 − u�−2 u�−1 = 1. Assume then that
u� ≥ 0 and that the claim holds for u� − 1. We have
u�u� u�u�−1 − u�u�−1 u�u� = (u�u� u�u�−1 + u�u�−2 )u�u�−1 − u�u�−1 (u�u� u�u�−1 + u�u�−2 )
= − (u�u�−1 u�u�−2 − u�u�−2 u�u�−1 ) = (−1)u�−1 ,
Theorem 5.5 The convergents of any infinite continued fraction [u�0 , u�1 , u�2 , …]
with integer coefficients such that u�u� > 0 for u� ≥ 1 converge to
some number u�. Moreover every even convergent is strictly less
than u�, every odd convergent is strictly larger than u�, and the
distance to u� goes to 0 monotonically.
Theorem 5.7 There is a bijective correspondence between the real numbers and
continued fractions in the canonical form given by the mapping
from a number to its continued fraction expansion.
Proof. From Theorem 5.2 we already know that the rational numbers and
finite continued fractions in the canonical form are in a one-to-one
correspondence.
Recall from the proof Let u� be an irrational number. By the definition of the
of Theorem 5.3 that continued fraction expansion and the convergent recursion we have
the convergent re-
cursion works also
u�u� u�u�+1 + u�u�−1
u� = [u�0 , u�1 , …, u�u� , u�u�+1 ] =
for non-integers. u�u� u�u�+1 + u�u�−1
for some real number u�u�+1 > 1. From Theorem 5.4 it follows that
easy to see (compare Theorem 5.3) that both u� and u� satisfy the
recursion relations
As a word of warning, the same obviously does not hold for u�.
Instead, we have
u� u�′
∣u� − ∣ < ∣u� − ′ ∣ .
u� u�
where 0 < u� < u�u� and u� ≥ 1. Notice that the case u� = 0 corre-
sponds to the convergent u�u�u�−2 and the case u� = u�u� corresponds to
u�−2
the convergent u�u�u� . It is easy to check that the rest of the semicon-
u�
vergents lie between these two extremes and get closer to u� as u�
increases. Notice that like convergents, semiconvergents are always
irreducible fractions. (Why?)
2/1
3/2
4/3
13/10 22/17 31/24 40/31 49/38 58/45 125/97 192/149 259/201 326/253
67/52
9/7
5/4
1/1
Theorem 5.9 Let u� be a real number with continued fraction expansion [u�0 , u�1 , …].
All best rational approximations to u� are given by
u�u�−1 u� u� + u�u�−2
∣ − u�∣ < ∣ u�−1 − u�∣ .
u�u�−1 u�u�−1 u� + u�u�−2
u�u�−1 u�+u�u�−2
(3) Finally we prove that all semiconvergents u�u�−1 u�+u�u�−2 with u� >
u�u�
2 are best rational approximations by showing that
To prove (1), notice that any best rational approximation u�u� that is
not a convergent or semiconvergent lies between two of them. Let
u�u� u� + u�u�−1 u�u� (u� + 1) + u�u�−1
and
u�u� u� + u�u�−1 u�u� (u� + 1) + u�u�−1
u� u� u� + u�u�−1 u�
∣ − u� ∣= ,
u� u�u� u� + u�u�−1 u�(u�u� u� + u�u�−1 )
Because u� ≥ 1, we have
1 1
< ,
u�(u�u� u� + u�u�−1 ) (u�u� (u� + 1) + u�u�−1 )(u�u� u� + u�u�−1 )
1 1 u�
> − u�−2 ,
2 2u�u�+1 2u�u�−1
Theorem 5.10 Every best rational approximation of the second kind is a conver-
gent. Also the converse holds, except for the trivial case u� = u�0 + 12
for some integer u�0 .
1
Proof. The converse does not hold for u� = u�0 + 2 because u�0 and u�0 + 1
are equidistant from u� and both have denominator 1.
Let us first show that every best rational approximation of the
second kind is a convergent. We know by Theorem 5.9 that it is
either a convergent or semiconvergent, so it is enough to rule out
the possibility of it being a semiconvergent. Assume thus that the
semiconvergent u�u�u�−1 u�+u�u�−2
(where 0 < u� < u�u� ) is a best rational
u�−1 u�+u�u�−2
approximation of the second kind. We will show that
u�u�+1 1
|u�u� u� − u�u� | ≤ ∣u�u� − u�u� ∣ =
u�u�+1 u�u�+1
u�u�+1 u�
≤ = ∣u�u�−1 u�+1 − u�u�−1 ∣
u�u�+1 u�u�+1
≤ |u�u�−1 u� − u�u�−1 |.
u�u�+1
If u� = u�u�+1 it follows that u�u�+1 ≠ 1 and the inequality in the center
u�u�+1
is strict. If u� ≠ u�u�+1 then the first and last inequalites are strict by
oscillation. □
Then we have
Quadratic irrationals 61
u� 1 1
∣u� − ∣ ≤ |u�u� − u�| < .
u� u� 2u�u�
Let u�, u�, u� be integers satisfying u� ≠ 0 and u�2 − 4u�u� > 0. Then the
quadratic equation u�u�2 + u�u� + u� = 0 has two real solutions given
by
√
−u� ± u�2 − 4u�u�
u� = .
2u�
If u�2 −4u�u� is not a square, then both of these solutions are irrational
and such irrational numbers are called quadratic irrationals.
Let u� be a quadratic irrational. Then u� is a root of some
polynomial u�u� 2 + u�u� + u� with integer coefficients. The other root of
this polynomial is denoted by u� and is called the conjugate of u�.
Notice that the definition of the conjugate does not depend on the
choice of the polynomial since any such polynomial is divisible by
the minimal polynomial of u�.
√
Theorem 5.12 Let u� be a square √free integer and let u�( u�) denote the numbers √
of the form u� + u� u� with u�, u� ∈ u�. Define conjugation in u�( u�)
√ √ √
by u� + u� u� = u� − u� u�. Then u�( u�) is a field consisting of the
rational numbers and those quadratic irrationals u� for which the
square free part of u�2 − 4u�u� equals u� whenever u�u�2 + u�u� + u� = 0 for
some u�, u�, u� ∈ u�. For quadratic irrationals the conjugation coincides
with the conjugation defined above, and√it satisfies u� + u� = u� + u�,
u�u� = u�u� and u�−1 = u�−1 for all u�, u� ∈ u�( u�).
Proof. Exercise. □
Quadratic irrationals 62
A continued fraction of the form [u�0 , u�1 , …, u�ℓ , u�1 , …, u�u� ] where the
block u�1 , …, u�u� of coefficients repeats ad infinitum is called pe-
riodic. The main goal of this section is to prove the following
alternative characterization of quadratic irrationals.
Theorem 5.13 A real number u� is a quadratic irrational if and only if its continued
fraction expansion is periodic.
The full proof of this will require a couple of steps. We will first
prove a corresponding theorem for purely periodic continued frac-
tions and reduced quadratic irrationals. The proof is then finished
by a bootstrapping argument that lets us reduce the general case
to this case. A continued fraction is said to be purely periodic
if it is of the form [u�0 , u�1 , …, u�u�−1 ] and a quadratic irrational u� is
called reduced if it satisfies u� > 1 and −1 < u� < 0.
Theorem 5.14 A continued fraction is purely periodic if and only if it is the ex-
pansion of a reduced quadratic irrational.
Dividing both sides by −u�2 shows that −1/u� is a root of (5.1) and
thus −1/u� = u�. Because u� > 1 and u� > 1 (since there can be no
zeros in their continued fractions), this shows that u� is a reduced
quadratic irrational.
Assume then that u� is a reduced quadratic irrational. We want
to show that its continued fraction expansion is purely periodic.
First notice that for a given non-square integer u� > 0 there are only
finitely many different reduced quadratic irrationals that satisfy a
quadratic equation u�u� 2 + u�u� + u� = 0 with u� > 0 and u�2 − 4u�u� = u�.
To see this, we can use Vieta’s formulas to write u� = −u�u� − u�u� and
u� = u�u�u� where u� and u� are the roots of the polynomial. If u� is a
reduced √ quadratic irrational, it follows that u� = u� (u�−u�)
2 2
> u�2 , so
|u�| ≤ u�. Because u� and u� have different signs, u� = u�2 − 4u�u� ≥ u�2 ,
√ 2
so |u�| ≤ u�. Finally |u�| = |u�4|u�| −u�|
≤ u�4 . Thus the coefficients
u�, u�, u� are bounded and therefore there are only finitely many such
equations, thus also finitely many reduced quadratic irrationals.
Let u�0 ≔ u� and choose u�0 , u�0 , u�0 ∈ u� so that u�0 u�20 +u�0 u�0 +u�0 =
1
0. Set u� ≔ u�02 − 4u�0 u�0 . Define the sequence u�u� ≔ u� −⌊u� for
u�−1 u�−1 ⌋
building the continued fraction. Notice that if we choose
1
By Theorem 5.12 we have u�u� = u� −⌊u� and we see that −1 <
u�−1 u�−1 ⌋
u�u� < 0, so u�u� is reduced. The sequence u�u� must be periodic by
the observations above, and therefore also the continued fraction
expansion of u� is periodic.
It remains to show that the expansion is purely periodic. We
can do this by showing that if u�u�+1 = u�u�+1 for some u�, u�, then
u�u� = u�u� . Starting from indices where the periodicity holds this lets
us work backwards to the beginning, showing that the periodicity
holds all the time. Notice that u�u�+1 = u�u�+1 implies that u�u� −
⌊u�u� ⌋ = u�u� − ⌊u�u� ⌋. Thus u�u� ≡ u�u� (mod 1). It is therefore
enough to show that if u� is a quadratic irrational, then there is
exactly one u� such that u� + u� is a reduced quadratic irrational.
This is however clear since the conjugate of u� + u� is u� + u�, which
lies in the interval (−1, 0) for exactly one u�. □
Proof. (Proof of Theorem 5.13) Let us first show that a periodic continued
fraction is a quadratic irrational.
Let u� = [u�0 , u�1 , …, u�ℓ , u�1 , …, u�u� ] and write u� = [u�1 , …, u�u� ].
Then by Theorem 5.14 we know that u� is a quadratic irrational. It
is easy to see using Theorem 5.12 that
u�ℓ u� + u�ℓ−1
u� =
u�ℓ u� + u�ℓ−1
where u�u�−1 , u�u�−2 , u�u�−1 , u�u�−2 are integers. Thus by Theorem 5.12
we also have
Quadratic irrationals 65
Notice that the fraction in the parentheses has limit 1 since it tends
to u�−u�
u�−u� . By the way the continued fractions converge it is alternately
greater and less than 1 when u� is large enough. Choose some u� for
which it is less than 1. Because the factor u�u�u�−2 is always less than
u�−1
1, we have that −1 < u�u� < 0, which finishes the proof. □
Let us close this section with a short look at the structure of√the
continued fraction expansions of numbers √ of the form u� = u�.
The conjugate of u� is simply u� = − u�, so it is √ not a reduced
√
quadratic irrational. However the number u� = ⌊ u�⌋ + u� is
reduced and thus we can write the continued fraction expansion of
u� as [u�0 , u�1 , …, u�u� ]. Now we clearly have
√
u� = [u�0 − ⌊ u�⌋, u�1 , …, u�u� , u�0 , u�1 , …, u�u� , u�0 ]
√
and u�0 = 2⌊ u�⌋, so the continued fraction expansion of u� is of the
form
Proof. Only the fact that u�1 , …, u�u� is a palindrome is new. Let us use the
same notation as above. In the proof of Theorem 5.14 we saw that
the continued fraction [u�u� , u�u�−1 , …, u�0 ] corresponds to the number
Quadratic irrationals 66
−1 1√ 1
u� = √
u�−⌊ u�⌋
, which is simply u�1 ≔ u�−⌊u�⌋ , so it has the continued
fraction [u�1 , …, u�u� , 2u�0 ]. Thus we see that u�1 = u�u� = u�u� , …, u�u� =
u�1 = u�1 . □
pell’s equation
6
The diophantine equation
As was the case for the norm of the Gaussian integers defined in
Chapter √ 4, one can easily check that u� (u�u�) = u� (u�)u� (u�) √
for all
u�, u� ∈ u�[ u�]. It is also easy to check that a number u� ∈ u�[ u�] is
a unit if and only if u� (u�) = ±1.
These observations show√that solving (6.1) boils down to find-
ing the group of units in u�[ u�] and among√those units the ones
that have norm 1. We say that a unit u� + u� u� is non-trivial if
u� ≠ 0 and positive if u� > 0 and u� > 0. Notice that all non-trivial
units can be obtained from the positive units by changing the signs
of u� and u�.
√ √
Theorem 6.1 All positive units in u�[ u�] are of the form u�u� + u�u� u�, where u�u�
and u�u� are the numerator and denominator of some convergent u�u�u�
√ u�
and in particular
√ 1 1
|u� − u� u�| = √ <
u� + u� u� 2u�
because u� > u�. The claim now follows from Theorem 5.11. □
√
6.2 calculating the cf-expansion of u�
Proof. We have
√ u�u� u�u�+1 + u�u�−1
u� =
u�u� u�u�+1 + u�u�−1
√
u�u�+1 + u�
u�u�+1 u�u� + u�u�−1
= √
u�u�+1 + u�
u�u�+1 u�u�
+ u�u�−1
√
u�u�+1 u�u� + u�u�u� + u�u�+1 u�u�−1
= √ .
u�u�+1 u�u� + u�u�u� + u�u�+1 u�u�−1
Multiplying the first equation by u�u� and the second one by u�u� and
subtracting gives us
√
Calculating the CF-expansion of u� 70
√ √ √
1 < (u� + u� u�)(u�u� − u�u� u�)u� < u�u� + u�u� u�.
√ √ √
Let u� + u� u� ≔ (u� + u� √u�)(u�u� − u�u� u�)u� . It is clearly a unit.
Moreover
√ we have u� + u� u� > 1, so the inverse satisfies 0 < u� −
u� u� < 1. Adding these two inequalities gives us 2u� > 1, which √
implies that u� ≥ 1. Similarly u� > u�−1
√
u�
≥ 0, so u� ≥ 1. Thus u� + u� u�
√
is a positive unit, strictly smaller than u�u� + u�u� u�, which is a
contradiction. □
√
Theorem 6.4 The first time 2u�0 = 2⌊ u�⌋√appears as a coefficient of the con-
tinued
√ fraction expansion of u� marks the end of the period of
u� = [u�0 , u�1 , …, u�u� , 2u�0 ].
√
u� + u�
√ √
u� + u�
Proof. Because is reduced, we have that u� < u�. Thus ≤
√ √ u� u�
⌊ u�⌋ + u�, which gives us
√
u� + u� √
u�0 = ⌊ ⌋ ≤ 2⌊ u�⌋
u�
√
with equality
√ if and
√ only if u� = ⌊ u�⌋ and u� = 1. We know that
u�u�+1 = ⌊ u�⌋ + u� ends the period so we are done. □
√
Algorithm 6.5 The following algorithm finds the fundamental unit in u�[ u�], where
we assume that u� is a non-square.
7
An arithmetic function is simply a function u�+ → u�, where u�
is the set of complex numbers. An arithmetic function u� is called
multiplicative if
− u�(1) = 1, and
Theorem 7.1 The multiplicative functions are a subgroup of the unit group of
the Dirichlet ring.
Möbius inversion 73
Proof. Exercise. □
u�(u�) = ∑ u�(u�/u�)u�(u�),
u�|u�
then
This is most often used in the case that u� = u� and u�−1 = u� when
we are interested in u� but u� is easier to calculate.
Another version of this is the following: If u� is an invertible
arithmetic function, then
Möbius inversion 74
u�
u�(u�) = ∑ u�(u�)u�(⌊u�/u�⌋),
u�=1
implies
u�
u�(u�) = ∑ u�−1 (u�)u�(⌊u�/u�⌋).
u�=1
Algorithm 7.2 (Fast Möbius inversion) Suppose that we want to calculate u�(u�),
and that we know how to calculate
u�
u�(u�) ≔ ∑ u�(⌊u�/u�⌋),
u�=1
for(int64_t m=0;m<=sqrtn;m++) {
low[m] = g(m) - g(m/2);
int64_t x = 3;
while(x <= m) {
int64_t nextx = m/(m/x) + 1;
if(nextx%2 == 0) nextx++;
low[m] -= (nextx - x)/2 * low[m/x];
x = nextx;
}
}
for(int64_t i=sqrtn/2;i>=0;i--) {
int64_t denom = 2*i + 1;
int64_t m = n/denom;
high[i] = g(m) - g(m/2);
int64_t x = 3;
while(x <= m) {
int64_t nextx = m/(m/x) + 1;
if(nextx%2 == 0) nextx++;
if(m/x <= sqrtn) {
high[i] -= (nextx - x)/2 * low[m/x];
} else {
high[i] -= (nextx - x)/2 * high[denom*x/2];
}
x=nextx;
}
}
return high[0];
}
It should
√ be noted that same kind of tricks (noting that there are
about 2 u� different numbers ⌊u�/u�⌋ etc.) work for calculating many
sums that are not strictly speaking Möbius inversions.
u�
Let us denote u�u� (u�) = ∑u�=1 u�(u�) for any arithmetic function
u�. Notice that if u� = u� ∗ u�, then we have
u�
u�u� (u�) = ∑ u�(u�)u�u� (⌊u�/u�⌋).
u�=1
Möbius inversion 76
Example 7.3 Recall that the Euler totient function u� satisfies u� = Id ∗u�. There-
fore
u� u� ⌊ u�u� ⌋ (⌊ u�u� ⌋ + 1)
u�u� (u�) = ∑ u�(u�)u�Id (⌊u�/u�⌋) = ∑ u�(u�) ,
u�=1 u�=1
2
u�
so u�u� (u�) = ∑u�=1 u�(u�) can be calculated in u�(u�3/4 ) time by using
the fast Möbius Inversion algorithm given above with the function
u�(u�) = u�(u�+1)
2 .
algebra
P
In this second part we will discuss additional topics in algebra.
A
R
T
II
groups
1
One way to think about group theory is to say that it is the study
of invertible transformations, or symmetries and how they compose
to form new symmetries. For example the rotations of the plane
form a group, and combining a 30° rotation with a 45° rotation
yields a 75° rotation.
Symmetries always have an inverse transform; in the above
case a −30° (or 330°) rotation would cancel the 30° rotation. The
trivial symmetry that does not change anything is called the iden-
tity element of the group.
See [Link] Another example of a group could be given by all the possible
.[Link]/wiki transformations on the Rubik’s Cube. The cube has 6 sides, each
/Rubik's_Cube_group of which can be turned by 90°. The different combinations of these
for more information turns form a group with 227 ⋅314 ⋅53 ⋅72 ⋅11 distinct transformations,
on this specific group. which is also the number of possible states of the Rubik’s Cube.
Definition 1.1 A set u� together with a binary operation ∘ is a group if the fol-
lowing three properties hold:
associativity − for all u�, u�, u� ∈ u�, u� ∘ (u� ∘ u�) = (u� ∘ u�) ∘ u�
Example 1.2 The dihedral group u�u� is the group of rotational and reflectional
There are two com- symmetries of a regular polygon with u� sides. We have illustrated
peting notations for the results of applying the symmetries in u�6 to a colored hexagon
dihedral groups: The in Figure 1.1. The first row of hexagons are simply the 6 possible
alternative would de- rotations, and the second row is obtained by reflecting the hexagons
note the group u�u� on the first row about a vertical line.
by u�2u� since there
are 2u� elements.
In general the elements of u�u� can be given as 1, u�, u�2 , …, u�u�−1 and
Here u� ≔ 2u� is u�, u�u�, u�u�2 , …, u�u�u�−1 , where u� is a rotation by u� /u� radians and u� is a
the perimeter of fixed reflection. The multiplication in u�u� is then uniquely defined
the unit circle. by the rules u�2 = 1, u�u� = 1 and u�u�u� = u�−1 .
For example in the case of u�6 the sets {1, u�, u�2 , …, u�5 }, {1, u�} and
{1, u�u�} are subgroups – a patient reader can try listing all of them.
Definition 1.4 Let u� be a subgroup of u�. Then the sets of the form
Basic definitions 80
where u� ∈ u� are called left cosets of u�. Similarly the sets of the
form u�u� are called right cosets.
It is easy to check that the distinct left cosets u�u� are disjoint and
partition u� (same obviously holds for right cosets). In fact they
correspond to the equivalence classes of the equivalence relation
u� ∼u� u� which we can define by
Theorem 1.5 Let u� be a finite group and u� ⊂ u� a subgroup. Then |u�| divides
|u�|.
In general the left and right cosets of a subgroup can differ. For
example the right cosets of {1, u�u�} in u�6 are {1, u�u�}, {u�, u�u�2 },
{u�2 , u�u�3 }, {u�3 , u�u�4 }, {u�4 , u�u�5 }, {u�5 , u�}, which are not the same as the
left cosets we listed above. This motivates the following definition.
Definition 1.6 A subgroup u� ⊂ u� for which u�u� = u�u� for every u� ∈ u� is called a
normal subgroup of u�.
Basic definitions 81
Definition 1.7 Let u� be a normal subgroup of u�. Then u�/u� can be made into
a group by defining u�u� ∘ u�′ u� = (u�u�′)u�. The inverse of u�u� is
thus u�−1 u� and the identity is u�. Such groups are called quotient
groups.
The reader should check that the definition makes sense, i.e. that
the multiplication is well-defined and that the obtained object ac-
tually is a group.
The subgroup u� = {1, u�2 , u�4 } is normal in u�6 with cosets
u� = {1, u�2 , u�4 }, u�u� = {u�, u�u�2 , u�u�4 }, u�u� = {u�, u�3 , u�5 } and u�u�u� =
{u�u�, u�u�3 , u�u�5 }. Thus u�6 /u� is a 4-element quotient group of u�6
whose multiplication table is listed in Table 1.1.
u� u�u� u�u� u�u�u�
u� u� u�u� u�u� u�u�u�
u�u� u�u� u� u�u�u� u�u�
u�u� u�u� u�u�u� u� u�u�
u�u�u� u�u�u� u�u� u�u� u�
Table 1.1 Multiplication
table of u�6 /{1, u�2 , u�4 }
Definition 1.8 Let u� and u�′ be two groups. A map u�∶ u� → u�′ is called a group
homomorphism if u�(u�u�′) = u�(u�)u�(u�′) for all u�, u�′ ∈ u�.
Notice that it follows that u�(1) = 1 and u�(u�−1 ) = u�(u�)−1 . One can
also easily check that the kernel of u�, i.e. the set
Theorem 1.9 Let u�∶ u� → u�′ be a group homomorphism. Then there exists a
˜ u�/Ker u� → Im u� such that u� = u�˜∘ u�
group isomorphism u�∶ Ker u� .
˜
u�(u�Ker u� (u�)) = u�(u�)
provided that we can show that whenever u�Ker u� (u�) = u�Ker u� (u�),
we have u�(u�) = u�(u�). But this is clear because if u� and u� belong
to the same coset, we have u� = u�u� for some u� ∈ Ker u�, so that
u�(u�) = u�(u�u�) = u�(u�)u�(u�) = u�(u�).
It remains to show that u�˜ is injective. Now if u�(u�˜
Ker u� (u�)) =
u�(u�) = 1, then u� ∈ Ker u�, so u�Ker u� (u�) = Ker u�, which means that
the kernel of u�˜ consists of the single coset Ker u�. Thus u�˜ is injective.
□
For example the group homomorphism u�∶ u�6 → u�4 given by u�(u�) =
u�, u�(u�) = u� and extending uniquely to the other elements of u�6 has
as its kernel {1, u�2 , u�4 }, and therefore u�6 /{1, u�2 , u�4 } ≅ u�4 .
A group u� is called cyclic if there exists an element u� ∈ u� such
that every element of u� can be written as u�u� for some u� ∈ u�. The
element u� is called a generator of u�. Finite cyclic groups with u�
elements are denoted by u�u� . The group u�u� is of course isomorphic
to the additive group of u�/u�u�, but we will write the operation in
u�u� multiplicatively. Thus if we want to be concrete, we can define
u�u� to consist of the u�th roots of unity in u�, that is the complex
Basic definitions 83
Proof. Since u� is finite, ord(u�) < ∞, and by Theorem 1.5 we see that
ord(u�) divides |u�|. Moreover u�ord(u�) = 1, since u� is a generator of
a cyclic group of order ord(u�). Thus u�|u�| = (u�ord(u�) )|u�|/ord(u�) = 1.
□
Definition 1.11 If u�1 and u�2 are two groups, we can form their direct prod-
uct u� = u�1 × u�2 by considering all tuples (u�1 , u�2 ) with u�1 ∈ u�1
and u�2 ∈ u�2 and defining the group operation componentwise, i.e.
(u�1 , u�2 ) ∘ (u�1 , u�2 ) ≔ (u�1 u�1 , u�2 u�2 ).
Theorem 1.12 Let u� be a group and assume that u�1 and u�2 are two normal
subgroups of u� such that u�1 ∩u�2 = {u�}. Then u�1 u�2 is a subgroup
of u� isomorphic to u�1 × u�2 .
Proof. Notice that if u�1 ∈ u�1 and u�2 ∈ u�2 , then we have u�1 u�2 u�−1
1
u�−1
2
∈
u�1 ∩ u�2 because u�1 u�2 u�−1 1
∈ u�2 and u�2 u�−1
1
u�−1
2
∈ u�1 . Thus we see
that u�1 u�2 = u�2 u�1 .
Let us now show that u�1 u�2 is a subgroup. Clearly u� ∈
u�1 u�2 . If u�1 , u�′1 ∈ u�1 and u�2 , u�′2 ∈ u�2 , then (u�1 u�2 )(u�′1 u�2 ′) =
(u�1 u�1 ′)(u�2 u�2 ′) ∈ u�1 u�2 , so u�1 u�2 is closed under the group oper-
ation. Moreover (u�1 u�2 )(u�−1 1
u�−1
2
) = (u�1 u�−1
1
)(u�2 u�−1
2
) = u�, so u�1 u�2
is closed under taking inverses, too.
Group actions 84
We can define a map u�∶ u�1 ×u�2 → u�1 u�2 by setting u�((u�1 , u�2 )) =
u�1 u�2 . It is a homomorphism because of the commutativity proven
in the first paragraph. It is clearly a surjection and if u�1 u�2 = u�,
then we must have u�1 = u� = u�2 , so it is also an injection and thus
an isomorphism. □
u� u� ≔ {u� ∈ u� : u� ⋅ u� = u�}.
Proof. Clearly if u� and u� are in the same coset of the stabilizer u�u� , we
must have u� ⋅ u� = u� ⋅ u�. Thus we may define a map u�∶ u�/u�u� → u�
given by u�(u�u�u� ) = u� ⋅ u�.
To prove the claim it is enough to show that u� is an injection.
Assume that u�(u�u�u� ) = u�(u�u�u� ). Then u� ⋅ u� = u� ⋅ u�. Operating by
u�−1 on both sides we get u�−1 ⋅ u� ⋅ u� = u�, so that u�−1 u� ∈ u�u� , which
implies that u�u�u� = u�u�u� . □
Theorem 1.15 (Burnside’s lemma) Let u� be a finite group and u� a finite set.
Assume that u� acts on u�. Then the number of orbits is given by
1
|u�/u�| = ∑ |u� u� |.
|u�| u�∈u�
1 |u� | 1
|u�/u�| = ∑ = ∑ u� = ∑ |u� u� |,
u�∈u�
|u�u�| u�∈u�
|u�| |u�| u�∈u�
where the last equality follows because both sums count each pair
(u�, u�) such that u� ⋅ u� = u� exactly once. □
|u�/u�(u�)| = | Cl(u�)|.
where we have picked exactly one element u�u� from each of the u�
conjugacy classes of u�.
permutations and ordered sets
(u�u�(u�)⋯u�u�−1 (u�))
It is clear that disjoint cycles commute, and one may easily no-
tice that any permutation u� can be written as a product of disjoint
cycles, uniquely up to
− the order of the cycles,
of u� − 1 transpositions.
Theorem 2.1 Assume that u�1 …u�u� = 1, where u�u� are transpositions. Then u� is
even.
Proof. Let u�, u�, u�, u� ∈ u� be distinct. Then the following equations hold:
1 (u�u�)(u�u�) = 1,
2 (u�u�)(u�u�) = (u�u�)(u�u�),
3 (u�u�)(u�u�) = (u�u�)(u�u�),
4 (u�u�)(u�u�) = (u�u�)(u�u�).
Notice that if we rewrite the product u�1 …u�u� by replacing a pair
u�u� u�u�+1 that matches the left hand side of one of (1),(2),(3) or (4)
with the corresponding right hand side, the parity of u� does not
change.
Now pick an element u� ∈ u� that appears in some of the trans-
positions in the product. By using the rules (1)–(4), we may move
Permutation group u�u� 89
every such u� to the left-most transposition. In the end all the u�s
must disappear since the product equals the identity permutation
and we cannot be left with a single u�. If we repeat this for every el-
ement that appears in the product, we will have reduced ourselves
to the case 1 = 1 without changing the parity of the number of
transpositions in the product. □
Algorithm 2.2 (Sign of a permutation) The following algorithm computes the sign
of a permutation by going through the cycles and alterning the sign
based on the length of the cycle. The permutation is assumed to be
on the set {1, …, u�}, so perm[0] is ignored. The function modifies
perm in the process to keep track which cycles have been counted,
but in the end the vector should be the same as in the beginning.
perm[j]=-perm[j];
j=-perm[j];
}
}
return sgn;
}
The cycle structure of the permutation also tells the least number of
transpositions needed to express the permutation. Indeed for a per-
mutation u� = u�1 …u�u� where u�u� are cycles of lengths ℓu� respectively
and ℓ1 + … + ℓu� = u� we can write each cycle as a product of ℓu� − 1
transpositions, so in total we need (ℓ1 − 1) + … + (ℓu� − 1) = u� − u�
transpositions. We leave it as an exercise to show that this is actu-
ally optimal.
Theorem 2.4 Let u� be the number of inversions of u�. Then sgn(u�) = (−1)u� .
Proof. Without loss of generality we can assume that u� = {1, …, u�}. For
any u� ∈ u�u� let
Conjugates and commuting elements 91
be the set of pairs (u�, u�), where u� preserves the order. Consider the
map u�∶ u�u� → u�2 given by u�(u�) = (−1)|u�(u�)| . Notice that u� agrees
with sgn on transpositions, so to prove the claim, it is enough to
show that u�(u� u�) = u�(u� )u�(u�) for all u� , u� ∈ u�u� . Now
Theorem 2.5 The conjugacy classes of u�u� correspond to the different cycle types.
One way to think about conjugation in u�u� is that we are given some
new labels for u�u� , say u�1 , …, u�u� . Our permutation u� works on the
old labels 1, …, u� and u� maps the old labels u� to their corresponding
new labels u�u� . The conjugation u�u� u�−1 first converts the new labels
to the old ones, then performs u�, and finally represents the result
by using the new labels.
Let us now find the centralizer of u� ∈ u�u� . Remember that
u� ∈ u�u� commutes with u� if and only if u�u� u�−1 = u�. Let us compare
the cycle representations of the left and right hand sides. We have
− each u�u� maps u�u�,1 , …, u�u�,u�u� to some u�u�,1 , …, u�u�,u�u� with u�u� = u�u� ,
preserving the cyclic order.
In particular if the cycle type of u� is (u�1 , u�2 , …, u�u� ), where
u�u� is the number of u�-cycles, then there are u�1 !u�2 !…u�u� ! ways to
choose which cycles u� maps to each other and 1u�1 2u�2 …u�u�u� ways
to choose how to map each cycle while preserving the cyclic or-
der. Thus the number of elements in u�u� that commute with u� is
u�1 !u�2 !…u�u� !1u�1 2u�2 …u�u�u� .
Another way to see this would be to recall that |u�/u�(u� )| =
| Cl(u� )| and use a simple counting argument to show that the con-
jugacy class has size
u�!
.
u�1 !u�2 !…u�u� !1u�1 2u�2 …u�u�u�
linear recurrences
One quite slick way of getting hold of these sequences is via the
shift operator u�∶ u� ∞ → u� ∞ defined by setting u�(u�0 , u�1 , …) =
(u�1 , u�2 , …). Then u� = (u�u� )∞
u�=0
is a linear recursive sequence if
and only if there exist constants u�1 , …, u�u� such that
Theorem 3.3 Let u� and u� be finite dimensional u�-vector spaces and assume that
u� ∶ u� → u� is a linear map. If u� ∈ u� ∞ is a linear recursive sequence,
then u� u� = (u� u�u� )∞
u�=0
∈ u� ∞ is also a linear recursive sequence.
Proof. Pick a basis u�1 , …, u�u� for u� and let u�1∗ , …, u�u�∗ be the dual basis. Let
u�
u�u� = u�u�∗ (u�) and consider the intersection u� = ⋂u�=1 u�u� . We may
u�
pick a u� ∈ u� that is not zero and satisfies u�(u�)u�u�∗ (u�) = 0 for all
1 ≤ u� ≤ u�. Now clearly also u�(u�)u� = u�(u�)((u�1∗ (u�u� )u�1 )∞
u�=0
+…+
∗ ∞
(u�u� (u�u� )u�u� )u�=0 ) = 0. □
Remark 3.5 As the proof indicates, in the above theorem it is enough to check
the condition for a dual basis.
Exercise 3.6 Show that sums of linear recursive sequences are linear recursive.
(Hint: Use the above two theorems.)
We will next see our first characterization for linear recursive se-
quences. In a sense they are generated by matrices.
Theorem 3.7 Let u� be a u�-vector space and u�∶ u� → u� a linear map. Then for
any u� ∈ u� the sequence u�u� u� is linear recursive.
Theorem 3.9 Let u� be a linear recursive sequence in some u�-vector space u�. Then
there exists a u�-vector space u�, a linear map u� → u�, u� ∈ u� and
a linear map u�∶ u� → u� such that u�u� = u�(u�u� u�) for all u� ≥ 0.
Generating functions 95
Proof. It is enough to show this in the case where u� = u�, since in the
general case it is possible to proceed coordinate wise and embed
the obtained vector spaces and mappings in a bigger space. Now if
(u�u� )∞
u�=0
∈ u�∞ is a linear recursive sequence, it satisfies an equation
of the form u�u� = u�1 u�u�−1 +…+u�u� u�u�−u� . Pick u� = u�u� and consider
the matrix
0 1 0 0 ⋯ 0
⎛
⎜ 0 0 1 0 ⋯ 0⎞ ⎟
⎜
⎜ ⎟
⋮ ⋮ ⋮ ⋱ ⋯ ⋮⎟
u� = ⎜
⎜
⎜
⎟
⎟
⎜ 0 0 0 0 ⋱ 0⎟ ⎟
⎜
⎜0 ⎟
0 0 0 ⋯ 1⎟
⎝u�u� u�u�−1 u�u�−2 u�u�−3 ⋯ u�1 ⎠
Clearly if u�u� = (u�u� , u�u�+1 , …, u�u�+u�−1 )u� for u� ≥ 0, then u�u�u� = u�u�+1 .
We can therefore choose u� to be the projection on the first coordi-
nate. □
In this and the following sections of the rest of this chapter we will
focus on the case where the linear recursive sequence lies in u�. In
the view of the first section this assumption is not very restrictive,
since the linear recursive sequences in higher dimensional vector
spaces are composed of one dimensional ones.
We will now look at the second characterization of linear re-
cursive sequences via generating functions.
u�=0
ℎ(u�) = u�0 + (u�1 − u�1 u�0 )u� + (u�2 − u�1 u�1 − u�2 u�0 )u�2 + …
+ (u�u�−1 − u�1 u�u�−2 − … − u�u�−1 u�0 )u�u�−1
u�−1 u�
= ∑ (u�u� − ∑ u�u� u�u�−u� )u�u� .
u�=0 u�=1
ℎ(u�)
Proof. Assume first that u�(u�) is a rational function with u�(0) ≠ 0. We
may without loss of generality assume that u� is of the form u�(u�) =
1 − u�1 u� − … − u�u� u�u� . Now let ℎ(u�) = ℎ0 + ℎ1 u� + … + ℎu� u�u� . We may
also assume that ℎ0 ≠ 0, because adding zeros in front of a linear
recursive sequence keeps it still linear recursive. Consider one step
of the long division:
ℎ(u�) (ℎ + u�1 ℎ0 ) + (ℎ2 + u�2 ℎ0 )u� + … + (ℎu� + u�u� ℎ0 )u�u�
= ℎ0 + u� 1 ,
u�(u�) 1 − u�1 u� − … − u�u� u�u�
where we set u�u� = 0 for u� > u�. Thus the long division maybe
modeled as a linear transformation on the space u�u�+1 which maps
(u�0 , …, u�u� ) to (u�1 + u�1 u�0 , u�2 + u�2 u�0 , …, u�u� + u�u� u�0 ). With the initial
vector (ℎ0 , …, ℎu� ) and projection on the first coordinate this gener-
ates a linear recursive sequence corresponding to the coefficients of
the power series.
Assume then that (u�u� )∞ u�=0
is a linear recursive sequence and
set u�(u�) and ℎ(u�) as in the statement of the theorem. It is enough
to check that
are pairwise coprime and irreducible and u�u� ≥ 1. After this one
can do the partial fraction decomposition and obtain
1 ℎ (u�) ℎ (u�)
u� = 1 u� + … + u� u�
1 − u�1 u� − … − u�u� u� u�1 (u�) 1 u�u� (u�) u�
for some polynomials ℎu� (u�). This makes it possible to split a given
linear recursive sequence into its irreducible components. Now let
us go further and move into the splitting field of the denominator.
Then our partial fraction decomposition takes the simple form
1 ℎ1 (u�) ℎu� (u�)
u� = u� +…+ ,
1 − u�1 u� − … − u�u� u� (u� − u�1 ) 1 (u� − u�u� )u�u�
where u�1 , …, u�u� are the roots of the denominator with multiplicities
u�1 , …, u�u� . From this form it is easy to see the following:
where u�1 , …, u�u� are polynomials and u�1 , …, u�u� are the roots of the
minimal polynomial of u�.
P
The third part of the book will focus on counting and enumerating
things. One of the main instruments in modern combinatorics are
generating functions. We will look at them especially in the context
of so called combinatorial species. This is a theoretical framework
that makes it easy to combine simple combinatorial objects into
more complicated ones by forming equational relationships between
A
them. Information on the objects can then be distilled via their
generating functions.
R
T
III
generating functions
1
Generating functions are simply a way of encoding sequences of
numbers that makes it easy to manipulate and analyze certain kinds
of data. They play a significant role in many areas of mathematics
such as combinatorics and number theory, and they are also a useful
tool for solving recurrences and doing various other tasks.
Definition 1.1 Let u� be a fixed commutative ring. A formal power series over
u� is an expression of the form
∞
∑ u�u� u�u� = u�0 + u�1 u� + u�2 u�2 + …
u�=0
Here the symbols u�u� carry no meaning of their own. They serve
only as a way to separate the coefficients u�u� , which for us will
usually be integers or rational numbers. This power series is also
called the (ordinary) generating function for the sequence u�u� .
Addition and multiplication are defined for formal power series
just as if they were analytic power series. Let u�(u�) and u�(u�) be
two formal power series with coefficients u�u� and u�u� respectively.
Then we define the formal power series u�(u�) ± u�(u�) by
∞
u�(u�) ± u�(u�) ≔ ∑(u�u� ± u�u� )u�u�
u�=0
and u�(u�)u�(u�) by
∞ u�
u�(u�)u�(u�) ≔ ∑ ∑(u�u� u�u�−u� )u�u� .
u�=0 u�=0
∞
If u�0 is invertible in u�, then we see that u�(u�) = ∑u�=0 u�u� u�u� is
∞
invertible in u�[[u�]] and its inverse u�(u�) = ∑u�=0 u�u� u�u� is given by
the recursive formula
u�
1
u�0 = u�−1 , u�u� = − ∑ u�u� u�u�−u� (when u� ≥ 1).
0 u�0 u�=1
The formal power series are all fine, but the real fun starts when
they happen to be Taylor series of some analytic functions. From
now on we will therefore simply take u� = u�.
Recall that a function u�∶ u� → u� is analytic in a neighbour-
hood of 0 if it is complex differentiable in that neighbourhood. In
other words, there exists an open disc u�(u�) ≔ {u� ∈ u�∶ |u�| < u�}
such that for all u� ∈ u�(u�) the limit
u�(u�) − u�(u�)
lim = u�′(u�)
u�→u� u� − u�
Analytic functions 101
that converges in the disc u�(u�). The coefficients u�u� are uniquely
1 (u�)
determined by the formula u�u� = u�! u� (0), where u� (u�) is the u�th
derivative of u�.
Whenever our formal power series represents an analytic func-
tion, we may as well work with the function itself. The uniqueness
of the representation ensures that the operations (addition, mul-
tiplication, …) we do are in one-to-one correspondence with the
operations on the series.
Let us now look at some important examples of analytic func-
tions and their power series.
First of all there is the geometric series
∞
1
= ∑ u�u� .
1 − u� u�=0
where
u� u�(u� − 1)…(u� − u� + 1)
( )≔
u� u�!
and a few less often occuring ones around the same theme are
∞ ∞
u�2u� u�2u�+1
cosh(u�) = ∑ , sinh(u�) = ∑
u�=0
(2u�)! u�=0
(2u� + 1)!
and
∞
u�2u�+1
arctan(u�) = ∑(−1)u� .
u�=0
2u� + 1
u�=0 u�=0
∞ ∞
= u�−1 + u� ∑ u�u�−1 u�u�−1 + u�−2 + u�−1 u� + u�2 ∑ u�u�−2 u�u�−2
u�=1 u�=2
2
= u�u� (u�) + u� + u� u� (u�).
Because the factor u� − 1 indicates that the sequence will grow like
u�!, we let u�(u�) be the corresponding exponential generating
function, which is a power series of the form
∞
u�u�
u�(u�) = ∑ u�u� .
u�=0
u�!
so
1 + u� u�
u�′(u�) = u� .
1 − u�
which gives us
u�0 = u�1 = 1,
u�−1
u�u� = 1 + 2 ∑(u� − u�)…(u� − 1) (u� ≥ 2).
u�=1
2
Combinatorial species is an abstract – but at the same time delight-
fully concrete – framework for modeling combinatorial structures.
Computationally its strength arises from its strong connections to
generating functions, while for modeling purposes it brings together
classical combinatorial enumeration and Pólya theory.
2.1 definition
Definition 2.1 A species is a functor u�∶ u�u�u� → u�u�u�u�u�u�, where u�u�u� is the category
of finite sets and bijections, and u�u�u�u�u�u� is the category of finite sets
and functions.
Spelled out for those that do not know category theory, this means
that a species is a rule u� that assigns to every finite set u� a finite set
u�[u� ] and to every bijection u�∶ u� → u� a function u�[u�]∶ u�[u� ] → u�[u� ].
Moreover the rule u� should satisfy the following two properties:
− u� respects composition, i.e. if u�∶ u� → u� and u� ∶ u� → u� are two
bijections, then u�[u� ∘ u�] = u�[u� ] ∘ u�[u�]
u�[u� ] u�[u� ]
u�[u�]
u�u� u�u�
u�[u� ] u�[u� ]
u�[u�]
Here Fix u�[u�] is the set of those u�-structures in u�[u�] ≔ u�[{1, 2, …, u�}]
that are fixed by u�[u�] and u�u� is the number of u�-cycles in u�.
From the cycle index we can recover two basic generating func-
tions associated to the species u�. The first one is given by
Operations on species 108
∞
u�u�
u� (u�) ≔ u�u� (u�, 0, 0, …) = ∑ |u�[u�]| .
u�=0
u�!
̃
Notice that u�(u�) is We claim that the coefficient
an ordinary generat-
1
1
ing function since u�! ∑ | Fix u�[u�]|
u�! u�∈u�
is part of the number u�
u�[u�](u�), if u� ∈ u�[u� ]
− (u� + u�)[u�](u�) ≔ {
u�[u�](u�), if u� ∈ u�[u� ]
It is easy to see that the series of the new species are simply
− u�u� +u� = u�u� + u�u� ,
− (u�̃ ̃
̃ + u�(u�).
+ u�)(u�) = u�(u�)
An intuitive way to think about a sum species u� + u� is that an
(u� + u�)-structure is an u�-structure or a u�-structure.
The second operation is product. If u� and u� are two species,
then their product species u� ⋅ u� is defined by setting
− (u� ⋅ u�)[u� ] ≔ ⋃{u�[u�1 ] × u�[u�2 ] : u� = u�1 ∪ u�2 , u�1 ∩ u�2 = ∅},
− (u�̃ ̃
̃ u�(u�).
⋅ u�)(u�) = u�(u�)
Proving these is left as an exercise. Intuitively one can think that
an (u� ⋅ u�)-structure is an u�-structure and a u�-structure.
The third operation we look at is composition. If u� and u�
are two species, then their composition species u� ∘ u� is defined by
− (u� ∘ u�)[u� ] ≔ ⋃u� a partition of u� u�[u�] × ∏u� ∈u� u�[u� ],
u�u� ∘u� (u�1 , u�2 , …) = u�u� (u�u� (u�1 , u�2 , u�3 , …), u�u� (u�2 , u�4 , u�6 , …),
u�u� (u�3 , u�6 , u�9 , …), u�u� (u�4 , u�8 , u�12 , …), …),
− (u�̃ ̃
∘ u�)(u�) = u�u� (u�(u�), ̃ 2 ), u�(u�
u�(u� ̃ 3 ), …).
To maintain the light stick-to-the-point exposition, we will skip the
proofs here. Intuitively one can think of an (u� ∘ u�)-structure as an
u�-structure of u�-structures.
There are two trivial species, 0 and 1. For 0 there are no structures
whatsoever. For 1 there is exactly one structure for the empty set of
labels and no structures for any other set of labels. The generating
functions of u� = 0 and u� = 1 are u�u� = u� (u�) = u�(u�) ̃ = 0 and
̃
u�u� = u�(u�) = u�(u�) = 1.
In fact we can take the sum species 1 + … + 1 where there are
u� terms to get a species u� which has exactly u� structures on the
empty set of labels and no structures on the other label sets. For
u� = u� we have u�u� = u� (u�) = u�(u�) ̃ = u�. Thus u� can be embedded
in the space of species. One can check that u� ⋅ u� = u�u� as species,
as well as u� ⋅ u� = u� + … + u� for any species u� (the sum has u�
terms).
The next interesting species is the singleton species u� which
has a single structure for any label set that has exactly one element
and no structures for other label sets. It follows that we have
̃
u�u� (u�1 , u�2 , …) = u�1 and u� (u�) = u�(u�) = u�.
Basic species 111
Let us now introduce a species that has structures for label sets
of any size. The species u� of sets is simply defined by u�[u� ] = {u� }.
Since there is exactly one structure, we have
∞ ∞
u�u� ̃ 1
u�(u�) = ∑ = u�u� and u�(u�) = ∑ u�u� = .
u�=0
u�! u�=0
1 − u�
∞ u� u�
u�1 1 u�2 2 …u�u�u�u�
= ∑ ∑
u�=0 u�1 +2u�2 +…+u�u�u�
u� !…u�u� !1u�1 2u�2 …u�u�u�
=u� 1
u�1 u�2 u�u�
∞ ( u�11 ) ( u�22 ) … ( u�u�u� )
= ∑
u�1 +2u�2 +…+u�u�u� =0
u�1 !…u�u� !
u�2 u�3 u�4
= exp (u�1 + + + + …) ,
2 3 4
since there are u� !…u� !1u�u�!1 2u�2 …u�u�u� different permutations of cycle
1 u�
type (u�1 , …, u�u� ).
The species of permutations u� can be defined by setting u�[u� ] =
{u� ∶ u� → u� : u� is a bijection} and u�[u�](u� ) = u� ∘ u� ∘ u�−1 . We have
∞
1 u�
u�u� = ∑ ∑ u�1 !…u�u� !1u�1 …u�u�u� u�1 1 …u�u�u�u�
u�=0
u�! u�∈u� u�
∞
u�
= ∑ ∑ u�1 1 …u�u�u�u�
u�=0 u�1 +2u�2 +…+u�u�u� =u�
∞
1
= ∏ ,
u�=1
1 − u�u�
since there are u�1 !…u�u� !1u�1 …u�u�u� permutations that are fixed under
u�!
conjugation by u� and in total there are u� !…u� !1 u�1 …u�u�u� permutations
1 u�
Derived species 112
1
of given cycle type. From this we easily see that u�(u�) = 1−u� and
̃ = ∏∞
u�(u�) 1
.
u�=1 1−u�u�
u� = 1 + u� ⋅ u�2 ,
to get
√
̃ 1− 1 − 4u�
u�(u�) = .
2u�
√
1− 1 − 4u�
u�(u�) =
2u�
and
1 − √1 − 4u�1
u�u� = .
2u�1
u� = u� ∘ u�u�u�.
In particular we have
∞
1 u�Cyc (u�2 , u�4 , u�6 , …)
∏ = exp (u�Cyc (u�1 , u�2 , …) +
u�=1
1 − u�u� 2
u�Cyc (u�3 , u�6 , u�9 , …)
+ + …),
3
∞ ∞ ∞
u�u�u� u�Cyc (u�u� , u�2u� , u�3u� , …)
∑∑ =∑ . (2.1)
u�=1 u�=1
u� u�=1
u�
for some series u�2 not depending on u�1 , since all the other terms
on the right hand side of (2.1) do not contain u�1 . Now assume by
induction that we have written u�Cyc (u�1 , u�2 , …) in the form
u�−1 ∞
u�u�u�
u�Cyc (u�1 , u�2 , …) = ∑ u�u� ∑ + u�u� (u�u� , u�u�+1 , …)
u�=1 u�=1
u�
for some numbers u�u� and consider u�u� . All the terms that contain
u�u� are contained in the sum
u�Cyc (u�u� , u�2u� , …, u� u� ⋅u� , …) 1
u�−1
u�u� ∞
= ∑ ( ∑ u�u� ∑ u�u� +
u�
∑
u� u� u�=1 u�=1 u�
u�|u� u�|u�
u�u�/u�
In particular we see that ∑u�|u� u� = 1, giving the recursion
u�u�/u�
u�u� = 1 − ∑
u�
u�|u�,u�≠1
∞
u�(u�) 1
u�Cyc (u�1 , u�2 , …) = ∑ log ( ).
u�=1
u� 1 − u�u�
3
Pólya theory is about counting objects that have symmetries. For
example we might want to count in how many ways u� people can be
seated around a round table when different rotations of the same
arrangement should be considered to be equal. In this case the
rotational symmetry is easy to take into account, and the answer
is of course (u� − 1)!.
The question becomes more difficult and interesting, however,
if we instead ask in how many ways we can form a necklace with u�
red beads, u� green beads and u� blue beads such that u� + u� + u� = u�.
Two necklaces are considered to be the same if one can be obtained
from the other by rotating the necklace.
In general the symmetry is described by some group. In the
case of necklaces this group is simply u�u� , but we could have some-
thing more complicated instead. We might for example want to
account also for reflections, in which case we would be counting
bracelets and the group in question would be u�u� . Yet another
problem would be to count in how many ways we can color the 6
faces of a cube in 3 colors when all the rotational symmetries should
be taken into account. The rotational symmetry group of the cube
turns out to be isomorphic to u�4 .
Definition 3.3 A colored species is a functor u�∶ u�u�u�u�u�u� → u�u�u�u�u�u�, where u�u�u�u�u�u�
is the category of finite colored sets and color-respecting bijections.
where the first sum ranges over all sequences u�1 , u�2 , … such that
u�u� = 0 for all but finitely many u�.
The generating function for a labeled species u� is given by
The definitions and rules for sums and products of colored species
parallel those of ordinary species and the induced operations on the
generating functions are the same.
For substitution we have the following: Let u� and u�1 , u�2 , …
be colored species and assume that u�u� [∅] = ∅. An u� ∘ (u�1 , u�2 , …)-
structure on a colored set u� is formed by taking a colored partition
of u�, creating an u�-structure on the partition, and then assigning
each part with color u� a u�u� -structure. The generating functions of
u� = u�(u�1 , u�2 , …) are
u�(u�1 , u�2 , …) = u� (u�1 (u�1 , u�2 , …), u�2 (u�1 , u�2 , …), …)
̃ (u� , u� , …), u�
̃ 1 , u�2 , …) = u�u� (u�
u�(u� ̃ (u�2 , u�2 , …), …;
1 1 2 1 1 2
̃ (u� , u� , …), u�
u� ̃ (u�2 , u�2 , …), …; …)
2 1 2 2 1 2
(1)
u�u� ((u�u� )u�≥1 ; …) = u�u� ((u�u�1 )1 , (u�u�1 )2 , …; (u�u�2 )1 , (u�u�2 )2 , …; …),
(1) (1) (2) (2)
where (u�u�u� )u� = u�u�u� (u�u� , u�2u� , …; u�u� , u�2u� , …; …).
It is typical to denote by u�u� (u� ≥ 1) a species that only has
a single structure for a single label of color u�. Then for example
u�1 + u�2 + u�3 would be a single label of one of the three colors 1,
2 or 3. This makes it easy to start constructing colored species out
of uncolored (or single-colored species) species by substitution.
Example 3.4 In the beginning we asked in how many ways we can form a necklace
of u� red, u� green and u� blue beads. This information can be distilled
from the unlabeled generating function of the species u�u�u�(u�1 +u�2 +
u�3 ), which is simply
∞ ∞
u�(u�) 1 u�! u�u� u�u� u�u�
∑ ∑ ∑ u�1 1 u�2 2 u�3 3 .
u�=1
u� u�=1 u� u� u� !u�
=u� 1 2 3
!u� !
1 +u�2 +u�3
We need u�u�1 = u�, u�u�2 = u� and u�u�3 = u�, so u� must divide gcd(u�, u�, u�).
Thus the coefficient of u�u�1 u�u�2 u�u�3 is
u�+u�+u�
u�(u�) ( u� − 1)!
∑ ⋅ u� .
u� ( )! ( u�
)! ( u�
)!
u�| gcd(u�,u�,u�) u� u� u�
Definition 3.5 Let Γ be a group and u� a (colored) species. We say that Γ acts
naturally on u�-structures if for every finite (colored) set u� there
exists an action Γ × u�[u� ] → u�[u� ] such that
Definition 3.6 The quotient of a Γ-species with underlying species u� is the species
u�/Γ defined by letting (u�/Γ)[u� ] ≔ u�[u� ]/Γ and (u�/Γ)[u�](Γu�) ≔
Γu�[u�](u�) for all finite (colored) sets u� and (color-respecting) bijec-
tions u�.
1
u�u�/Γ = ∑ u� Γ (u�).
|Γ| u�∈Γ u�
1
| Fix (u�/Γ)[u�]| = ∑ | Fix(u� ⋅ u�[u�])|.
|Γ| u�∈Γ
It follows that
1
| Fixu�/Γ (u�)| = ∑ u�(Γu� ∈ Fixu�/Γ (u�))
u�∈u�
|Γu�|
1 1
= ∑ ∑ u�(u� = u� ⋅ u� ⋅ u�)
u�∈u�
|Γu�| |Γu� | u�∈Γ
1
= ∑ | Fixu� (u� ⋅ u�)|.
|Γ| u�∈Γ
̃ , u� , …) =
− u�(u� 1
∑u�∈Γ u�u�̃ (u�1 , u�2 , …).
1 2 |Γ|
Quotient species 122
Example 3.9 We can construct the species u�u�u� of bracelets by letting the two-
element group u�2 act on u�u�u� by reflection and taking the quotient.
By Theorem 3.8 we have
1 Γ
u�Bra = (u� + u�Bra (u� )) ,
2 Cyc
u�
ways and thus there are ( u�2 )!2 2 −1 fixed cycles in total. There are
u�!
u� valid permutations.
( u�
2 )!2
2
Quotient species 123
u�!
fixed cycles. There are u�−2 valid permutations.
2( u�−2
2 )!2
2
P
A
R
T
IV
impartial games
1
In this chapter we will look at a class of combinatorial games which
are impartial in the sense that the available moves at a given game
position does not depend on which player is moving.
1.1 nimbers
Definition 1.1 An impartial game is a finite set u� such that one of the following
holds:
− u� = ∅
0, if u� = ∅
u� (u�) = {
mex({u� (u�1 ), …, u� (u�u� )}), if u� = (u�1 , …, u�u� )
Theorem 1.3 Under perfect play the first player wins game u� if and only if
u� (u�) ≠ 0. Equivalently u� is a losing position if and only if
u� (u�) = 0.
Proof. This is easy to prove by induction. The claim clearly holds for
u� = ∅. Assume that the claim holds for all u� ∈ u�. Now if
Sum games 126
̃ = {u� + u�
u� + u� ̃ : u� ∈ u�} ∪ {u� + u�
̃ : u� ̃
̃ ∈ u�}.
̃ = u� (u�) ⊕ u� (u�),
u� (u� + u�) ̃
Proof. By induction
a cyclic group 82
abelian group 78
additive group d
of a ring 26 Dirichlet ring 72
alternating group 89 dihedral group 79
analytic function 100 direct product
arithmetic function 72 of groups 83
b e
best rational approximation 55 Euclidean domain 14
Euler totient function 26
c embedding
centralizer 85 of species 107
characteristic polynomial 93 even permutation 89
chinese remainder theorem 24
class equation 86 f
colored species 117 Fibonacci sequence 102
combinatorial species 106 field 10
combinatorially equal 107 formal power series 99
commutative
group 78 g
completely multiplicative function Gaussian integers 44
72 generalized binomial coefficient
composite element 17 101
conjugacy class 85 generating function 104
conjugate elements 85 ordinary 99
continued fraction 49 greatest common divisor 14
periodic 62 group 78
convergent 50 homomorphism 81
convolution group of units 26
arithmetic functions 72
coprime 15 h
covering homomorphism
of species 107 group 81
cycle 87
cycle index 107 i
cycle type 88 ideal 11
128
o s
odd permutation 89 sgn
orbit 84 of a permutation 89
129
species 106 u
stabilizer 84 unique factorization domain 18
subgroup 79 unit 10
t w
totient function 26 witness
transposition 87 of compositeness 39