Franco Vivaldi (Author) - Experimental Mathematics With Maple-Chapman and Hall - CRC (2001)
Franco Vivaldi (Author) - Experimental Mathematics With Maple-Chapman and Hall - CRC (2001)
Franco Vivaldi (Author) - Experimental Mathematics With Maple-Chapman and Hall - CRC (2001)
Mathematics
with
MAPLE
CHAPMAN & HALL/CRC MATHEMATICS
Integration Theory
W. Filter and K. Weber
Full information on the complete range of Chapman & Hall/CRC Mathematics books is
available from the publishers.
Experimental
Mathematics
with
MAPLE
Franco Vivaldi
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
Vivaldi, Franco
Experimental mathematics with Maple / Franco Vivaldi.
p. cm. — (Chapman & Hall/CRC mathematics)
Includes bibliographical references and index.
ISBN 1-58488-233-6 (alk. paper)
1. Mathematics—Data processing. 2. Maple (Computer file) I. Title.
II. Series.
QA76.95 .V58 2001
510—dc21 00-052372
This book contains information obtained from authentic and highly regarded sources. Reprinted material
is quoted with permission, and sources are indicated. A wide variety of references are listed. Reasonable
efforts have been made to publish reliable data and information, but the author and the publisher cannot
assume responsibility for the validity of all materials or for the consequences of their use.
Neither this book nor any part may be reproduced or transmitted in any form or by any means, electronic
or mechanical, including photocopying, microfilming, and recording, or by any information storage or
retrieval system, without prior permission in writing from the publisher.
The consent of CRC Press LLC does not extend to copying for general distribution, for promotion, for
creating new works, or for resale. Specific permission must be obtained in writing from CRC Press LLC
for such copying.
Direct all inquiries to CRC Press LLC, 2000 N.W. Corporate Blvd., Boca Raton, Florida 33431.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are
used only for identification and explanation, without intent to infringe.
4 Sequences 59
4.1 Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.2 Sequences with Maple . . . . . . . . . . . . . . . . . . . . . 61
4.3 Plotting the elements of a sequence . . . . . . . . . . . . . . 64
4.4 Periodic and eventually periodic sequences . . . . . . . . . . 67
4.5 Some non-periodic sequences . . . . . . . . . . . . . . . . . 70
4.6 Basic counting sequences . . . . . . . . . . . . . . . . . . . . 77
4.7 Sequences de ned recursively . . . . . . . . . . . . . . . . . 82
vii
viii
ix
x
What is Maple?
Maple is a powerful system for symbolic computation. In this chapter, we
intend to give you a avour of Maple's capabilities by taking you through
a guided tour of some Maple commands. You need a computer with a
working copy of Maple software.
Starting and ending a Maple session
The procedure for starting a Maple session depends on the computer and
on the details of the software installation. For instance, if you are using a
window-based version of the software, you will click on Maple's icon with
the mouse. After starting a Maple session, the system will greet you with
a prompt, which may look like this
>
The cursor | the symbol that indicates the place where any typing will
end up (typically a bar `|' or an underscore ` ') | will be positioned just
after the cursor, waiting for input. Whenever Maple displays a prompt, it
means that it is ready to accept a new command. One of these commands
is quit (or done, or stop), which terminates a Maple session.
> quit
You rst type it, then press the RETURN key. In a window environment,
you may achieve the same result by clicking on the `quit' button with the
mouse. Before falling silent, Maple will display a brief summary of the
resources used during the session, which need not concern you now.
A guided tour of Maple
Here are a few things Maple can do.
Let n be equal to 40! (read `40 factorial'), which is the product of the
rst 40 positive integers: 40! = 1 2 3 39 40:
> n:=40!;
n := 815915283247897734345611269596115894272000000000
1
2 CHAPTER 1. WHAT IS MAPLE?
Decompose n into prime factors.
> ifactor(n);
(2) (3) (5) (7) (11) (13) (17) (19) (23) (29) (31) (37)
38 18 9 5 3 3 2 2 2
(Did you expect 40! to be so large? Does its prime factorization make sense
to you? Think about it.)
Display the rst 100 digits of . (Note that Pi has upper-case P and
lower-case i.)
> evalf(Pi,100);
3:141592653589793238462643383279502884197169399375105n
820974944592307816406286208998628034825342117068
Expand the binomial (x + py) : 10
> expand((x+sqrt(y))^10);
p
x10 + 10 x9 y + 45 x8 y + 120 x7 y 3=2 + 210 x6 y 2 + 252 x5 y 5=2
+210 x4y3 + 120 x3y7=2 + 45 x2y4 + 10 xy9=2 + y5
Compute the nite sum
n
X1
k 3 5k :
k=1
> sum(k^3*5^k,k=1..n-1);
45 5n n 115 5n 15 5nn + 1 n 5n + 115 2 3
32 128 16 4 128
Compute the limit
lim 3x 1 :
x!1 x arctan(x) + ln(x)
> limit((3*x-1)/(x*arctan(x)+ln(x)),x=infinity);
6 1
Compute the derivative of sin x ln(1 2
x3 ) .
> diff(sin(x^2*ln(1-x^3)),x);
31 xx
4
cos(x ln(1
2
x3 )) 2 x ln(1 x3 ) 3
> eqns:=f5*x-3*y=2*z+1,-x+4*y=7*z,3*x+5*y=zg;
eqns := f5x 3y = 2z + 1; x + 4y = 7z; 3x + 5y = z g
> solve(eqns,fx,y,zg);
x=
31 ; y = 22 ; z = 1
255 255 15
Plot the graph of the function
sin(x ) ln(1 + x );
3 2
x 2 [ 2; 2]:
> plot(sin(x^3)*log(1+x^2), x=-2..2);
1.5
0.5
-2 -1 0 1 2
x
-0.5
-1
-1.5
Chapter 2
The building blocks of arithmetic are the natural numbers, which form a
set denoted by N
N = f0; 1; 2; 3; : : : g:
The notion of a set | a collection of distinct objects | is fundamental in
mathematics. It will be dealt with in some detail in later chapters. Here
it suÆces to say that the objects constituting the set N are the integer
zero and the positive integers, which should be familiar to everyone. These
elements are listed within curly brackets, separated by commas. As we
cannot list them all, we resort to dots.
0 1 2 3
:::
N = f1; 2; 3; : : : g:
+
5
6 CHAPTER 2. INTEGERS AND RATIONALS
As an arithmetical environment, the set N is plainly inadequate. For
instance, 2 and 3 belong to N, and so do their sum 2 + 3 = 5 and product
2 3 = 6. But their di erence 2 3 = 1 does not. One says that N is
not closed under subtraction, that is, subtraction may take you out of the
set. The lack of closure under subtraction | a serious problem | can be
stated without ever mentioning the word `subtraction' or introducing the
minus sign. It suÆces to note that the equation
x+a = b a; b 2 N (2.1)
cannot always be solved for x in N. This formulation is useful, because it
only makes use of minimal ingredients: the set N and an operation, called
addition, that allows us to combine its elements together.
To solve this problem, we adjoin to N the negative integers, to obtain
the set Z of all integers
Z = f: : : ; 2; 1; 0; 1; 2; : : :g:
2 1 0 1 2
::: Æ Æ :::
Figure 2.2: The set Z, represented as points on a line. The white circles
denote elements of Z which are not in N.
Note that the elements of Z originate precisely from the distinct solu-
tions x of equation (2.1). For instance, by setting b = 0 in (2.1) and letting
a vary among all positive integers, we obtain all the negative integers. Like-
wise, by setting a = 0 and letting b vary in N, we recover all elements of N.
It follows that every element of N is also an element of Z, which we express
by saying that N is contained in Z (or that N is a subset of Z) and write
N Z: (2.2)
This is called an inclusion relation. By the same token, N N.
+
The sum, di erence, and product of any two elements of Z yield another
element of Z, and for this reason the arithmetic in Z is simpler than that
in N. In other words, each of the following equations
x =a+b x+a=b x = a b; a; b 2 Z (2.3)
can be solved by some x 2 Z. There is still a problem, though: the integers
are not closed under division | the equation x a = b cannot be added to
the list (2.3). Indeed, 2 and 3 are integers, but 2=3 is not. This problem
will lead to the introduction of the rational numbers.
Apart from the existence of arithmetical operations, two important
structures are de ned in Z which justify the representation of the integers
2.1. INTEGERS 7
as equally spaced points on a line, as in gure 2.2. They are an ordering
relation and a distance.
First, the integers are ordered, in the sense that, for any a; b 2 Z, at
least one of the following relations is satis ed:
ab b a: (2.4)
If both are satis ed, then a = b, while a 6 b is equivalent to a > b, etc.
The reader should be familiar with the following properties of the ordering
relation :
if a b and b c then a c
if a b then a + c b + c
if a b and 0 c then ac bc:
Second, we can measure the distance between two integers. To do so,
we rst de ne the size of an integer x as the absolute value of x, given by
jxj = x x xx < 00:
n
(2.5)
The quantity jxj is non-negative, and is equal to zero precisely when x = 0.
The numbers x and x have the same absolute value.
The distance between two integers x and y is de ned as the size of the
integer x y, which is jx yj. In particular, jxj = jx 0j is the distance
between x and 0. From the de nition, it follows that (i) the distance
between x and y is the same as that between y and x (since x y and y x
have the same size); (ii) the distance between x and y is zero precisely when
x = y (since x y is equal to zero only in this case); and (iii) the minimal
positive distance between two integers is 1. Moreover, any three integers x,
y , and z satisfy the inequality
jx z j jx yj + jy z j (2.6)
which is called the triangle inequality. The latter is justi ed by noting
that the direct journey from x to z cannot be longer than that with an
intermediate stop at y.
z y x x z y
(i) (ii)
Figure 2.3: The triangle inequality (2.6). (i) The equal sign holds when y
lies between x and z (including the endpoints). (ii) Strict inequality holds
when z is in the middle.
8 CHAPTER 2. INTEGERS AND RATIONALS
2.2 Arithmetical expressions
104079321946643990819252403273640855386152622472667048053191n
12350403608059673360298012239441732324184842421613954281n
00779138356624832346490813990660567732076292412950938922n
03457731833496615835504729594205476898112116936771475484n
78866962501384438260291732348885311160828538416585028255n
60466622483189091880184706822220314052102669843548873295n
8028878050869736186900714720710555703168729087
(2.7)
The integer 21279
1 has 386 decimal digits. (It turns out to be a prime. To
construct even larger primes, see the end of section 2.6). As the result does
not t on a single line, Maple has automatically appended the continuation
character `n ' which indicates that the output continues on the following
line. In principle, we can compute and display extremely large integers, with
up to half a million decimal digits, on most 32-bit computers. In practice,
however, to compute and display very large integers, the computer may
take longer than the maximum time we are prepared to wait (100 years, for
instance). (To nd out the maximum number of decimal digits supported
by your computer, type kernelopts(maxdigits).)
The expression 2^1279-1 contains two distinct arithmetical operators,
exponentiation and subtraction. Again, there are two possibilities, namely
(a) 2 1279
1 (b) 2 1279 1
=2 1278
> (3-2)^5;
1
If there is more than one operator, the order of evaluation of an ex-
pression is (i ) exponentiation, followed by (ii ) multiplication or division,
followed by (iii ) addition, subtraction, or negation (changing sign). If more
than one operator with the same priority appears, evaluation proceeds from
left to right for groups (ii ) and (iii ). Chains of exponentiation operators
are illegal, as we have seen. The parentheses `(' and `)' may be used to alter
the order of evaluation and to introduce signed operands. A sub-expression
2.2. ARITHMETICAL EXPRESSIONS 11
enclosed in parentheses is always evaluated rst, and if there is more than
one of them, with left to right priority.
Example 2.1. Let us construct the following expression
2 + 6 = (2 + 6) = ((2 ) + 6) :
34 5 (3 4 5) ((3 4) 5)
(2.8)
78 (7 8) (7 8)
This expression is normally written in the form appearing on the left, with-
out parentheses. Its architecture | a numerator with some material at an
exponent, and a denominator | implies three sets of parentheses (middle
expression), while a further layer of parentheses spells, in detail, the order
of evaluation of all arithmetical operators (rightmost expression).
In all, we have seven operands and six operators, and the order in which
the operations have to be performed is the following ( rst a, then b, etc.)
2 ^3 4 5 + 6 = 7 8
c a b d f e
abc d a b a
4) [2] 5) [2] 6) c [1]
e c d b+
d
Try to minimize the use of parentheses by taking advantage of the order of
evaluation of arithmetical operators. The minimal number of parentheses
needed in each case is indicated in square brackets.
12 CHAPTER 2. INTEGERS AND RATIONALS
Exercise 2.2. Evaluate the following arithmetical expressions, trying to
minimize the use of parentheses.
1 1766319049 161 226153980 3 5
3 3 181:
2 2
2 2 3
4 + 17 8
7
5
The symbol `' denotes multiplication. The expression on the left is an
arithmetical miracle. Change just a digit in one of the integers in the
denominator, and compare the result.
2.3 Some Maple
three steps
> 12^3+1^3;
1729
> 10^3+9^3;
1729
> %%-%;
0
When a computation involves a sequence of commands, one may wish to
suppress the display of intermediate results. This is done by appending at
the end of the command a colon `:' rather than a semicolon `;'
> 12^3+1^3:
> 10^3+9^3:
> %%-%;
0
Example 2.2. Evaluate the following arithmetical expression
((((1 + 1) 2 + 1) 3 + 1) 4 + 1) 5:
This expression is nested, and it should be built from the inside out
> 1:
> (%+1)*2:
> (%+1)*3:
> (%+1)*4:
> (%+1)*5;
325
14 CHAPTER 2. INTEGERS AND RATIONALS
In Maple, one can place more than one statement on the same line. All
syntactical rules described above apply.
> 1+1:%-2;%%;
0
2
This feature should be used sparingly, because it can lead to illegible codes.
It is also possible to display more than one result on the same line of output,
by separating expressions with commas
> 1+1:%-2,%;
0; 2
The composite expression %-2,% constitutes a single statement (this con-
struct will be considered in greater detail in a later chapter). Compare
carefully the use of ditto variables in the last two examples.
Comments can be inserted in a Maple command using the character #.
Everything after that character is ignored. Comments can also be inserted
in the output, by enclosing them between double quotes.
> "one plus one equals",1+1; # this should be about two
one plus one equals; 2
> # cool!
Variables
Mathematical variables represent `indeterminates' or `unknowns'. So by
(a + b) we denote the square of the sum of two unspeci ed quantities.
2
One sees that the value of an assignment statement, captured by the ditto
variables, is equal to the value of the expression being assigned, and not to
the assignment itself. This is because the assignment statement is not an
expression, and so it does not have a value.
Once a variable is assigned a value, the latter is substituted whenever
the variable's symbolic name appears in an expression
> a:=3:
> 2-a^a,c;
25; (3 + b)
2
To return the variable to the status of indeterminate, we must use the right
quotes as follows
> a:='a';
a := a
> 2-a^a,c;
2 aa; (a + b) 2
2 + 2y + y x z x xz z
2 2 2 2 4
(2.9)
Assigning values to x and y by means of the assignment operator would
cause the original value of s to be temporarily lost. Its recovery would
require clearing variables, and constructs of the type (2.9) could not be
performed.
> x:=100:y:=99:s;
29900
> x:='x':y:='y':s;
x + y + x2 + y 2 + xy
If more than one substitution is performed with a single subs command,
Maple will proceed left to right in the substitution list. This form of multiple
substitution is called sequential. In general, the order in which substitutions
are performed matters, e.g.,
> subs(a=b,b=c,a+b+c);
3c
> subs(b=c,a=b,a+b+c);
b + 2c
(Study the above example with care.) In order to carry out a simultaneous
substitution, it is necessary to group the substitution equations within curly
brackets
> subs(a=b,b=c,c=a,a^3+b^2+c);
a3 + a2 + a
> subs(fa=b,b=c,c=ag,a^3+b^2+c);
b3 + c2 + a
The e ect of the latter substitution is to permute the variables a, b, and c.
Example 2.3. Evaluate the following algebraic expression
1
4 a a b +b 1 9a :
1
2
b+1
2 4
(b 1) 2
18 CHAPTER 2. INTEGERS AND RATIONALS
for a = 2 and b = 1. We build this expression starting from the sub-
expression 1=a a, using ditto variables and substitutions. Then we sub-
2
1071508607186265447020721164266013135159412775893191176596n
8472890921952544339378013021020630393279855155792260144674n
6605212834809970127613668968844556474896643683504041891476n
0047746073703282588498083618354488963128411684797363293441n
7856017249719920313704260788202771289845415370457786694613n
325762937375
Because this number is positive, we conclude that 2 > 3 . However,
1000 600
> %%+%;
2+x < z 3y 2
> n:=100^100:n1:=101^99:n2:=99^101:
> evalb(abs(n-n1) < abs(n-n2));
true
So n is the closer of the two to n.
1
Exercises
Exercise 2.3. By means of some experiments, verify the rules concerning
the order of evaluation of arithmetical operators.
Exercise 2.4. In this exercise you should use the function subs.
(a) Construct the following algebraic expression
1+z+z +z +z ;
2 3 4
1 y y y 2 3
1 2 2 2 1 3+3 3
2 3 2 3
Exercise 2.5. In this exercise you should not display any digit. Display
only the minimal output required to establish your result.
(a) Place the following integers in ascending order
100 ; 80 ; 60 ; 40 ; 20 :
100 120 140 160 180
(b) Find the largest integer whose 17th power is smaller than 10 . 30
2.4 Divisibility
divides n and vice versa. The above symmetry has an arithmetical coun-
terpart: if d divides n, the role of d and q = n=d in the equation n = d q is
interchangeable.
Because every divisor d of n has the twin divisor n=d, can we conclude
that the number of divisors of an integer is always even? Not necessarily,
because d and n=d may coincide, giving n = d , a square. Thus, an integer
2
nd all divisors of n, we must test for divisibility only those integers d for
which 1 d n (that is, 1 d pn).
2
(3; 12), (4; 9), and 6. Because 36 is a square, one of its divisors has no twin,
and the number of divisors of 36 is odd.
Quotient and remainder
Given two natural numbers d and n, with d 6= 0, one can always nd two
uniquely determined natural numbers q and r such that
n = dq + r 0 r < d: (2.10)
22 CHAPTER 2. INTEGERS AND RATIONALS
The integers q and r are called the quotient and the remainder of the division
of n by d, respectively. The existence of such integers with the stated
properties can be established as follows. We arrange n points on the plane
to form a pattern which is as close as possible to a rectangular array with d
rows. In other words, we maximize the number of columns with d elements,
as illustrated below for the case n = 14 and d = 3.
q=4
z }| {
8
<
d=3 r=2
:
Figure 2.6: Dividing 14 by 3 gives quotient 4 and remainder 2.
Let q be the number of complete columns (d elements). Then there exists,
at most, one incomplete column, whose number r of elements is necessarily
smaller than d (otherwise, we could form more complete columns). This
establishes the equation and the inequality in (2.10). The condition that
r be smaller than d is what makes the de nition of remainder interesting.
In particular, it follows that d divides n precisely when the corresponding
remainder is zero, in which case the n = dq points form a rectangular array
with d rows.
Note that one speaks of the quotient of the division of n by d whether or
not d divides n. When d divides n, the de nition (2.10) with r = 0 reduces
to that given at the beginning of this section.
The greatest common divisor
A common divisor of two integers is an integer that divides both. Thus, if
n = dq and n0 = dq 0 , then d is a common divisor of n and n0 .
8 8
< <
(i) 3 : (ii) 3 :
Figure 2.7: The integers (i ) 12 = 3 4 and (ii ) 21 = 3 7 have the common
divisor 3.
If d divides n and n0, then d also divides n n0. Indeed, from n = dq
and n0 = d0 q, we have n n0 = d (q q0 ). (This is also clear from the above
gure.)
The greatest common divisor of two integers x and y, denoted by gcd(x; y),
is the largest among the common divisors of x and y.
gcd( 15; 21) = 3 gcd(1; 7) = 1 gcd(0; 7) = 7:
2.4. DIVISIBILITY 23
By de nition, gcd(x; y) = gcd(jxj; jyj). Also, gcd(x; 0) = jxj, and, for
convenience, gcd(0; 0) is de ned to be zero. Thus, gcd(x; y) 0. The
de nition of greatest common divisor may be extended to the case of more
than two integers in an obvious fashion. Thus, gcd(x ; : : : ; xn ) is the largest
1
among the common divisors of the n integers x ; : : : ; xn .
1
The integers x ; : : : ; xn are said to be relatively prime, or coprime, if
1
their greatest common divisor is equal to 1. For example, 14; 15 are rel-
atively prime, and so are 10; 15; 18. The latter example shows an inter-
esting phenomenon. We have gcd(10; 15; 18) = 1, in spite of the fact that
gcd(10; 15) > 1, gcd(10; 18) > 1 and gcd(15; 18) > 1. So when n is greater
than 2, n integers may be relatively prime without being so when consid-
ered in pairs. We say that n integers are pairwise relatively prime if any two
of them are. For instance, 26; 33; 35 are pairwise relatively prime. Pairwise
relative primality is stronger than relative primality, in the sense that the
former implies the latter (see exercises).
The least common multiple of two nonzero integers x and y, denoted
by lcm(x; y), is the smallest positive integer divisible by x and y. For any
integer x we de ne lcm(x; 0) = 0. For instance, lcm(12; 21) = 84. As for
the greatest common divisor, the de nition of least common multiple can
be extended to the case of more than two integers.
Maple functions for divisibility
Maple implements a large number of arithmetical functions, including all
elementary functions related to divisibility.
The functions iquo(n,d) and irem(n,d) return the quotient and the
remainder, respectively, upon dividing n by d. (The pre x i means `integer'.
Maple also has the functions quo, rem which carry out the corresponding
operations for polynomials, as we shall see.) These functions accept also
negative arguments, in which case the de nition of quotient and remainder
given in (2.10) is generalized as follows
n = dq + r 0 jrj < jdj; n r 0: (2.11)
The behaviour of iquo and irem when one or both arguments are negative
is best illustrated with examples.
> iquo(23,7),iquo(23,-7),iquo(-23,7),iquo(-23,-7);
3; 3; 3; 3
> irem(23,7),irem(23,-7),irem(-23,7),irem(-23,-7);
2; 2; 2; 2
We see that the remainder is negative whenever n is negative, while the
quotient is negative if n and d have opposite signs.
The function igcd(x1,...,xn) and ilcm(x1,...,xn) return the great-
est common divisor and the least common multiple, respectively, of the
24 CHAPTER 2. INTEGERS AND RATIONALS
integers x1 through xn. For a summary of the main Maple arithmetical
functions, see the last section of this chapter.
Example 2.7. Divisibility and greatest common divisor. There is a close
relationship between divisibility of b by a and the greatest common divisor
of a and b. Indeed, if a > 0, then a divides b precisely when gcd(a; b) = a.
To see this, observe that if gcd(a; b) = a, then ajb, by de nition of greatest
common divisor. Conversely, let ajb. Because aja, then a divides both a and
b, and since a is the largest integer dividing a, it follows that gcd(a; b) = a.
Example 2.8. Divisibility and remainder. We can use irem to test di-
visibility, since d divides n exactly when irem(n,d) is equal to zero, in
which case evalb(irem(n,d)=0) evaluates to true. For instance, 2 1 191
is divisible by 383
> evalb(irem(2^191-1,383)=0);
true (2.12)
Pythagorean triples
A Pythagorean triple is a set of three positive integers (x; y; z) such that
x +y =z :
2 2 2
(2.13)
For instance, (3; 4; 5) is such a triple. The elements of a triple are the
lengths of the sides of a right triangle, from Pythagoras' theorem, and so the
problem of nding Pythagorean triples amounts to nding right triangles
whose sides all have integer length.
If (x; y; z) is a triple, then by multiplying equation (2.13) by d we 2
nd that (dx; dy; dz) is another triple. So from any triple (x; y; z) one can
generate in nitely many of them, by letting d = 1; 2; 3; : : : . If d > 1 then
the elements of such a triple are not relatively prime, having the common
divisor d.
Conversely, if d is the greatest common divisor of x; y, and z, then d 2
can be cancelled out from equation (2.13) and one nds that (x=d; y=d; z=d)
is also a triple whose elements are relatively prime. A Pythagorean triple
with this property is said to be primitive.
It turns out that all triples can be derived from primitive ones, so the
primitive case is the most interesting one. The following algorithm for
nding all primitive Pythagorean triples is due to Diophantus of Alexandria
(about 250 a.d.).
Consider two positive integers p and q such that (i) p > q, (ii) p and
q are relatively prime, (iii) p and q are of opposite parity (i.e., one is
even and the other is odd). Then (2 p q; p q ; p + q ) is a primitive
2 2 2 2
Pythagorean triple. That this is actually a triple follows from the algebraic
identity
(2 p q) + (p q ) = (p + q )
2 2 2 2 2 2 2
(2.14)
2.5. RATIONALS 25
which is valid for all p and q. It is possible to prove that such a triple is
primitive, and that all primitive triples can be obtained in this way (see
exercises that follow).
The primitive triples for p 7 are tabulated below
PYTHAGOREAN TRIPLES
p q x y z
2 1 4 3 5
3 2 12 5 13
4 1 8 15 17
4 3 24 7 25
5 2 20 21 29
5 4 40 9 41
6 1 12 35 37
6 5 60 11 61
7 2 28 45 53
7 4 56 33 65
7 6 84 13 85
Exercises
Exercise 2.6. Show that every common divisor of two integers divides
their greatest common divisor.
Exercise 2.7. Show that if n integers are pairwise relatively prime, they
are relatively prime.
Exercise 2.8. Find all primitive Pythagorean triples corresponding to
p = 987 and q 970 ( ve in all).
Exercise 2.9. Find a primitive Pythagorean triple whose elements all
have at least 20 decimal digits (i.e., x; y; z 10 ). Verify directly that
19
your solution is indeed a primitive triple, that is, that x, y, and z satisfy
equation (2.13) and are relatively prime.
Exercise 2.10 .
Prove that if p > q > 0, gcd(p; q) = 1, and p and q have
opposite parity, then x = 2pq; y = p q , and z = p + q are pairwise
2 2 2 2
relatively prime. This, along with (2.14), establishes that (x; y; z) is a prim-
itive Pythagorean triple. (In this exercise, you will need the fundamental
theorem of arithmetic | see below.)
Exercise 2.11
Prove that any primitive triple can be obtained with
.
Diophantus' algorithm.
2.5 Rationals
all rationals with denominator n. These are the numbers of the form k=n,
for k 2 Z. The set n Z consists of equally spaced rationals with spacing
1
am 1
bm
lie at distance 1=bm from x. So, by choosing m to be suÆciently large, we
can make this distance be as small as we please.
30 CHAPTER 2. INTEGERS AND RATIONALS
While nding a rational close to a given rational is straightforward, the
problem becomes much more subtle if we try at the same time to minimize
the size of the denominator. For given n, the minimal denominators will
occur when ad bc = 1 and when b and d are of similar magnitude, each
approximately equal to the square root of n.
Example 2.12. Find a rational lying at distance smaller than 10 from
4
> (a*m+1)/(b*m);
1325
10005
But we can do better with a much smaller denominator, e.g.,
> c:=47:d:=71:
> abs(a/b-c/d);
1
10295
With reference to (2.19) note that
> abs(a*d-b*c);
1
(The rational 47/71 was found with a number-theoretical technique called
continued fractions. See (2.22) for an example.)
Example 2.13. Let a=b and c=d be two rational numbers, in reduced
form. Their mediant is de ned as
a+c
b+d
: (2.20)
We show that the mediant of two reduced rationals lies between them. Let
us assume that a=b < c=d. Then, since bd and d(b + d) are positive, we have
a c
<
b d
() ad < bc () ad + cd < bc + cd
() (a + c)d < (b + d)c () ab ++ dc < dc :
The other inequality is proven similarly. Another prominent rational lying
between a=b and c=d is their midpoint
1 a + c = ad + bc : (2.21)
2 b d 2bd
2.5. RATIONALS 31
The denominator of the midpoint is, in general, much larger than that of
the mediant. For this reason, the mediant may be used as a simple device
for identifying low-denominator rationals between two given rationals.
Exercises
Exercise 2.12. Evaluate the following rational expression, rst by hand,
then with Maple
1 + 13 1 + 181 122 41
4 5 2 3 + 10
9 14 3 56 28
Exercise 2.13. Consider the following expression, called a continued frac-
tion
a+
1 (2.22)
b+
1
a+
1
b+
1
a
(a) Construct it by means of a single Maple expression.
(b) By repeated use of the ditto variable %, construct it recursively, in four
or ve steps. [Hint: begin from the end.]
(c) Use the function subs to evaluate (2.22) for a = b = 1, and then for
a = 10 and b = 20.
Exercise 2.14. Let a = 5001999958 and let b = 5004400966.
(a) Compute the greatest common divisor d of a and b.
(b) Verify the following equation and inequalities
a = bq + r 0r<b
where q is the quotient and r is the remainder upon dividing a by b.
(c) Verify that Maple represents a=b in reduced form, that is, check that
a=b is displayed as a0 =b0 where
a b
a0 = b0 =
d d
and d is as above. The integers a and b are extracted from the value of
0 0
a/b by means of the Maple functions numer and denom, respectively.
(d) Find a proper divisor of a that does not divide b.
Exercise 2.15. Let a = 10 20 and let b = 2 1. Determine the
20 10 60
10 4
10 2
25
10 3
168
10 4
1; 229
10 5
9; 592
10 6
78; 489
10 7
664; 579
10 8
5; 761; 455
10 9
50; 847; 534
10 10
455; 052; 511 (2.24)
The density of primes decreases slowly. While a quarter of the integers
less than 100 are prime, the proportion of primes among the rst 10 billion
integers has dropped to less than 5%.
The arithmetic in Z is based on the following result.
Theorem 1 (the fundamental theorem of arithmetic). Every integer greater
than one can be expressed as a product of primes, and this factorization is
unique up to the order of the factors.
2.6. PRIMES 33
Thus, 12 = 2 3. You can see why 1 is not considered prime. If it
2
where we stipulate that the primes pi are all distinct and that the exponents
ei are positive integers. The primes p ; : : : ; pk are called the prime divisors
1
of n.
If n has the prime decomposition (2.25), and n = dq, then the prime
decomposition of dq is the same as that of n, from the fundamental theorem.
So if d divides n, the prime divisors of d must also be prime divisors of n,
and their exponent cannot exceed the corresponding exponent for n.
When constructing a divisor of n from (2.25), we nd that there are
ei + 1 possible choices for each exponent ei (from 0 to ei ). So the number
(n) of divisors of n is given by
(n) = (e + 1) (e + 1) (ek + 1):
1 2 (2.26)
Example 2.14. We have
84 = 2 3 7 : 2 1 1
where p and p are arbitrary distinct primes. From the in nitude of primes,
1 2
it follows that the number of integers with 10 divisors is in nite.
Maple has a function called ifactor which returns the unique prime
decomposition of an integer, as referred to in the fundamental theorem of
arithmetic
> ifactor(3^52-2^52);
(5) (13) (53) (79) (1093) (13761229) (29927) (4057)
2
34 CHAPTER 2. INTEGERS AND RATIONALS
Note that Maple is rather untidy | the prime factors are not necessarily
displayed in ascending order.
The existence of the fundamental theorem gives us a systematic way
of constructing all divisors of any given integer n, once we have its prime
decomposition. Obtaining the prime decomposition of an integer is concep-
tuallyp easy. All that is needed is testing the divisibility of n by all primes
p n. However, if n is large, this procedure is hopelessly slow. But even
with the most sophisticated factorization algorithms (such as the one im-
plemented by the function ifactor), factoring a large integer into primes
remains an extremely diÆcult computational problem.
Example 2.16. The function ifactor a ords a test for primality, al-
beit a rudimentary one. For instance, let us decide whether or not p =
31418506212244678577 is prime
> ifactor(31418506212244678577);
(7949) (7927) (7933) (7919) (7937)
> expand(%);
31418506212244678577
So p is not prime. The multipurpose function expand is doing just that, ex-
panding the product, thereby allowing us to recover the original entry. This
method of testing primes is ineÆcient, and it turns out that it is possible
to decide whether or not an integer is prime without actually computing
its prime decomposition.
Example 2.17. The function isprime(x) returns the value true if x is
prime and the value false otherwise
> isprime(31418506212244678577);
false
> isprime(7949);
true
Strictly speaking, the above description of isprime is incorrect. This func-
tion runs a probabilistic primality test, meaning that when it returns the
value true, the argument is `very likely' to be a prime. In practice, not a
single counterexample for which such test has failed is known, and if such
counterexample exists, it has been conjectured to be hundreds of digits
long. So you may trust isprime after all!
Example 2.18. A trapdoor function. If you run the commands ifactor
and expand of the previous exercise, you will not fail to notice that factor-
ization is a lot more time-consuming than multiplication. Multiplication is
a trapdoor function, easy to do but very diÆcult to undo. This very inter-
esting phenomenon is of great theoretical interest, and has applications in
cryptography. To get a feeling for the behaviour of this trapdoor function,
2.6. PRIMES 35
identify some large primes, multiply them together, and then attempt to
undo the multiplication by means of ifactor. Monitor the computation
time with the function time (see online documentation).
The Mersenne numbers
The number 2 1279
1 computed in (2.7) is very interesting: it is prime, and
so is 1279, the exponent of 2. Its primality was discovered in 1952 using a
computer, and at that time it was the largest known prime. The numbers
of the form
Mp = 2p 1 p prime
are called Mersenne numbers, from the name of the 17th-century priest,
Father Marin Mersenne, who rst studied their properties. He originally
thought that they were all primes, but this turned out not to be the case.
For instance, we have already veri ed that M is not prime (see (2.12)),
191
and indeed (as of December 2000) Mp has been shown to be prime only for
the following values of p
2; 3; 5; 7; 13; 17; 19; 31; 61; 89; 107; 127; 521; 607; 1279; 2203; 2281;
3217; 4253; 4423; 9689; 9941; 11213; 19937; 21701; 23209; 44497;
86243; 110503; 132049; 216091; 756839; 859433; 1257787; 1398269;
2976221; 3021377; 6972593
(2.27)
It is not known whether there are in nitely many Mersenne primes. The
importance of Mersenne numbers derives from the discovery, made by Lucas
in 1878, of a very eÆcient algorithm for testing their primality. It was
Lucas himself who found in this way the largest prime to be known before
the computer age, namely
> 2^127-1;
170141183460469231731687303715884105727
The current world record corresponds to the largest prime in (2.27), that
is, 2
6972593
1 with 2098960 digits.
By the time you read this page, this record may already have been bro-
ken: check the latest on Mersenne primes and other prime number records
on the web sites
http://www.mersenne.org
http://www.utm.edu/research/primes/
The primality of small Mersenne numbers can be tested with the func-
tion isprime. Thus, with reference to (2.23) and (2.27)
> isprime(2^89-1);
true
> isprime(2^97-1);
false
36 CHAPTER 2. INTEGERS AND RATIONALS
Even though the algorithm used by isprime is quite sophisticated, this
function does not implement Lucas' test, which works only for Mersenne
primes. Maple has specialized software for Mersenne numbers | type
?mersenne for details.
An advanced account of prime number records can be found in [4]. For
the necessary background in arithmetic, see [5] or [2].
Exercises
Exercise 2.17.
Exercise 2.18.
must use the isprime function, but you should avoid testing those numbers
that are divisible by 2, 3, or 5 (for these you do not need Maple!).
Exercise 2.19. There are 25 primes less than or equal to 100. Indeed,
the 25th prime is 97, and the 26th prime is 101 | cf. (2.23).
(a) Verify the above statement with the function ithprime.
(b) Let (x) be the number of primes not greater than x (cf. 2.24). Use the
function ithprime to determine (x) for x = 1000; 2000; 3000; 4000; 5000.
(The function nextprime may also be useful.) This calculation is likely to
require a certain amount of trial and error, but you must show only the
minimal output that suÆces to prove your result.
(c) Let (x; n) = (x) (x n) be the number of primes that lie between
x n and x (x n excluded). Tabulate the values of (x) and of (x; 1000).
Exercise 2.20. Find out which is the largest Mersenne prime you can
test for primality using isprime.
Exercise 2.21. Using the fundamental theorem of arithmetic, show that
for any x; y; z 2 Z, we have
gcd(x; y; z) = gcd(gcd(x; y); z) = gcd(gcd(x; z); y) = gcd(gcd(y; z); x):
This result says that the gcd of three integers can be computed by rst
computing the gcd of any two of them, and then the gcd of the latter and
the third integer.
Exercise 2.22. Show that for any m; n 2 Z, not both zero, the following
holds
gcd(m; n) lcm(m; n) = m n: (2.28)
2.7. STANDARD LIBRARY FUNCTIONS 37
Exercise 2.23 .
Find an integer greater than 10 which has exactly 21
40
3.1 Sets
A set is a collection (family, aggregate) of objects. We shall regard it as
a primitive concept, without attempting to characterize it formally. The
simplest way of de ning a set is to list all its elements (whenever this is
possible!), by enclosing them within curly brackets, separated by commas.
For instance,
A = f 1; 3; 5g
is the set constituted by the three integers 1, 3, and 5. The order with
which the elements of a set are listed is irrelevant, and therefore two sets
are the same if they contain the same elements
A = f 1; 5; 3g = f5; 1; 3g = f5; 3; 1g; etc:
39
40 CHAPTER 3. SETS AND FUNCTIONS
contains two elements, namely a and the set fa; bg. In particular,
a2B b 62 B fb; ag 2 B:
The empty set is the set that does not contain any element. This set is
important in the formal development of the theory and is denoted by the
symbol ;. Formally
; = fg:
The number of elements of a set A | called the cardinality of A | is
denoted by jAj.
jfa; b; abgj = 3: jffa; bggj = 1 jfgj = 0:
Example 3.1. Some nite sets.
1. The set D(n) of divisors of a positive integer n
D(120) = f1; 2; 3; 4; 5; 6; 8; 10; 12; 15; 20; 24; 30; 40; 60; 120g
D(127) = f1; 127g: (3.1)
2. The set P (n) of all partitions of a positive integer n | the possible
ways of representing n as a sum of positive integers, ignoring the order
of the summands
P (5) = f5; 4+1; 3+2; 3+1+1; 2+2+1; 2+1+1+1; 1+1+1+1+1g:
T = f(3; 5); (5; 7); (11; 13); (17; 19); (29; 31); (41; 43); : : : g: (3.6)
The largest known twin primes (as of December 2000) are
665551035 280025 1
with 24099 digits.
2. The set of Mersenne numbers Mp = 2p 1, p prime, which are prime
fM2; M3 ; M5; M7 ; M13 ; : : : g:
3. The set of Mersenne numbers which are not prime
fM11 ; M23 ; M29; M37 ; : : : g:
42 CHAPTER 3. SETS AND FUNCTIONS
In the above examples, the sets were de ned by listing the distinctive
properties of their elements. We develop a notation for this type of de ni-
tion. For instance, let A be the set of of non-negative powers of 2
A = f1; 2; 4; 8; 16; 32; 64; 128; 256; : : : g:
We write
A = fm m = 2k and k 2 N g:
The symbol before the vertical bar represents a generic element of the set.
After the vertical bar, we list the properties that every element of the set
must satisfy. The symbol before the bar may be replaced by an expression,
resulting in greater conciseness
A = f2k k 2 N g:
where (= now reads `if'. Note that reversing the implication requires
negating the operator 2, that is, changing the logical value of both expres-
sions (think about it).
Similarly, if B A we write
x 2 A (= x 2 B: (3.8)
It should be clear that the relations A B and B A are not mutually
exclusive. If (3.7) and (3.8) hold simultaneously, we have
x 2 A () x 2 B
3.1. SETS 43
which says that x is in A `if and only if' x is in B . The two statements
are equivalent: they are either both true or both false. This is the formal
de nition of equality between sets (A = B ).
We now introduce operations between sets, de ned in terms of the no-
tion of membership to a set. These operations are binary , in that they
involve two sets at a time, in much the same way as the arithmetical oper-
ations between numbers.
Union
The union of two sets A and B , denoted by A [ B , is the set constituted
by those elements that belong to at least one of the two sets A and B . In
particular, the union includes the elements that belong to both.
x 2 A[B () x 2 A or x 2 B: (3.9)
The conditions on the right-hand side, when taken individually, give two
one-way implications
x 2 A[B (= x 2 A
x 2 A[B (= x 2 B:
Intersection
The intersection of two sets A and B , denoted by A \ B , is the set consti-
tuted by those elements that belong to both A and B .
x 2 A\B () x 2 A and x 2 B: (3.10)
We have two one-way implications
x2 A\B =) x 2 A
x2 A\B =) x 2 B:
Di erence
The di erence of B from A, denoted by A n B , is the set of the elements of
A that do not belong to B .
Exercises
Exercise 3.1. Prove that for any two sets A and B , the following holds
A \B A A \B B A \ B A [ B:
Exercise 3.2. Let S and C be the sets of squares and cubes of natural
numbers, respectively.
S = f0; 1; 4; 9; 16; : : :g C = f0; 1; 8; 27; 64; : : :g
Characterize S \ C in terms of the prime factorization of its elements.
Exercise 3.3. Show that the set of primes p such that p +2 and p 2 are
also primes has only one element. (Hint: if p is prime, then one of p + 2 or
p 2 is divisible by 3.)
Exercise 3.4 .
Let A, B , and C be arbitrary sets. Prove that (i) A [ (B [
C ) = (A [ B ) [ C (associative law for unions); (ii) A \ (B \ C ) = (A \ B ) \ C
(associative law for intersections); (iii) A \ (B [ C ) = (A \ B ) [ (A \ C )
(distributive law).
3.2. SETS WITH MAPLE 45
3.2 Sets with Maple
Maple supports the set data type, with straightforward syntax. The fol-
lowing assignment statement de nes a set of 3 integers
> T:=f1,3,-4g;
T := f1; 3; 4g
> whattype(%);
set
f7; 0; 3g
and may rearrange the order of the elements
> f-4,3 1g;
f1; 3; 4g
To check whether or not a given element belongs to a set, we use the
standard library function member, which returns a value of the logical type
> U:=fa,fa,bgg:
> member(a,U),member(b,U),member(fb,ag,U);
empty := fg
To count the number of elements of a set, we use the function nops (number
of operands)
> nops(U),nops(empty);
2; 0
46 CHAPTER 3. SETS AND FUNCTIONS
Set operators
Maple supplies three set operators, namely union, intersect, and minus,
for union, intersection, and di erence between sets, respectively (see (3.9),
(3.10), and (3.11)). Their straightforward syntax is illustrated in the fol-
lowing examples.
> A:=f1,f1,2gg;
A := f1; f1; 2gg
> B:=f1,f1,3gg;
B := f1; f1; 3gg
> A union B;
f1; f1; 2g; f1; 3gg
> A intersect B;
f1g
> A minus B;
ff1; 2gg
> B minus A;
ff1; 3gg
Logical operators
Sets are often de ned through set operations. For instance, let a and b be
rationals such that a < b. The rational interval [a; b) containing the left
endpoint a but not the right endpoint b is the intersection of two sets A
and B , given by
A = fx 2 Q x ag B = fx 2 Q x < bg:
Now, from the de nition (3.10) of the intersection of two sets, we may say
that x belongs to [a; b) = A \ B , when the proposition x 2 A is true and the
proposition x 2 B is true. Accordingly, we shall test the condition x 2 [a; b)
with Maple through the truth or falsehood of the logical expression
> x >= a and x < b;
The union of two sets would require a similar construct, with the term `and'
replaced by `or', whereas the di erence between A and B would read x 2 A
and (not x 2 B ).
The binary logical operators and and or, and the unary logical operator
not, relate expressions whose value is of the logical (Boolean) type. These
operators act on the set B = ftrue; f alseg of the two Boolean constants.
(Maple actually has a three-way logic, involving the additional Boolean
constant FAIL, but we shall ignore it for the time being.) They should be
3.2. SETS WITH MAPLE 47
thought as an analogue of the binary arithmetical operators + and , and
the unary operator (sign change, carrying x to x), respectively, which
combine, for instance, elements of Z and Q .
The action of the binary operators and and or and of the unary operator
not is de ned by listing all possibilities:
LOGICAL OPERATORS
x y x and y x or y not x
Thus, x and y is equal to true only if both operands are equal to true,
while for x or y to be equal to true, only one of the operands needs to have
the value true. The following properties of logical operators can be veri ed
directly from the entries of the above table
(i) x and y = y and x
(ii) x or y = y or x
(iii) not (x and y) = (not x) or (not y)
(iv) not (x or y ) = (not x) and (not y )
The rst two properties state the commutativity of the operators and and
or, which in this respect behave just like + and .
Maple implements logical operators and expressions in a straightforward
manner. If there are several operators in a logical expression, they are
evaluated in the following order, given by: rst not, then and, then or.
Parentheses may be used to alter the order of evaluation. Expressions
containing logical operators are identi ed by Maple as being of the logical
type, and evaluated automatically. When this is the case, the use of evalb
is no longer required.
In the following examples we combine logical operators with relational
expressions:
Example 3.10. We verify property (iii) above, for the values x = true; y =
f alse
> not (true and false) = (not true) or (not false);
true
Example 3.12. Verify that 83 divides 341 1, but it does not divide
241 1.
> not irem(2^41-1,83)=0 and irem(3^41-1,83)=0;
true
Even if the relational operator = is involved, the presence of the logical
operators makes the Boolean status of the above expression unequivocal,
so that Maple evaluates it automatically without the need of calling evalb.
Example 3.13. Verify that the integer n = 10042 + 1 is a prime of the
form 7k + 3 or 7k + 4, for some integer k. We have to test n for primality
and check that n gives remainder 3 or 4 when divided by 7.
> 1004^2 + 1:
> isprime(%) and (irem(%,7)=3 or irem(%,7)=4);
true
The last expression requires two evaluations of irem(%,7). One of the two
can be avoided as follows
> isprime(%) and member(irem(%,7),f3,4g);
Exercises
Exercise 3.5. Without displaying any digit, verify that 11396333 is di-
visible by 43 or 47, but not by both.
3.3 Functions
Let A and B be two sets. By a function f de ned on A with values in B
we mean a rule which associates to every element x of A one | and only
one | element of B , denoted by f (x). We write
f :A!B f := x 7! f (x): (3.12)
The set A is called the domain of f , and the set B the co-domain (or range).
The clause that f (x) be unique is crucial: this is what makes functions
interesting. Also, a function must be de ned for all elements of the domain
A. If this is not the case, then A cannot be called the domain of the
function. In other words, the domain is always chosen as economically as
possible: it has no redundant elements.
As x varies in the domain A, f (x) varies in the co-domain B , and the
set of elements of B that can be obtained in this way is called the image
3.3. FUNCTIONS 49
of A, denoted by f (A). Because every element of f (A) is in B , we have
f (A) B , but the de nition of a function does not require that f (A) be
equal to B , that is, that the image must coincide with the co-domain. So,
unlike the domain, the co-domain of a function can be | and often is |
chosen with redundant elements.
If the image coincides with the co-domain, we say that f is surjective
(or onto ). Then, for every element y of B there exists at least one element
x of A such that f (x) = y . It immediately follows that any function f can
be turned into a surjective function if we identify co-domain and image:
f : A ! f (A).
If distinct elements of A always have distinct images in B , then f is said
to be injective (or one-to-one). Thus, a function is injective if f (x) = f (y)
implies x = y, for all x and y in the domain. If the domain of f is nite, then
f is injective precisely when domain and image have the same cardinality
(think about it).
A function which is both injective and surjective is said to be bijective.
A bijective function establishes a strong correspondence between domain
and co-domain.
Example 3.14. On any set A, we de ne the identity function, given by
IA : A ! A IA := x 7! x:
The identity function is bijective, and its domain, co-domain, and image
all coincide with A.
Example 3.15. Let
A = f 2; 1; 0; 1; 2g B = f 4; 3; 2; 1; 0; 1; 2; 3; 4g
and
f :A!B f := x 7! 2x:
Then f (A) = f 4; 2; 0; 2; 4g, so that the image is distinct from the co-
domain and f is not surjective. For instance, 3 2 B , but there is no
element x of A such that f (x) = 3. Because the domain of f is nite,
and the image has the same cardinality as the domain, we conclude that f
is injective without further check.
Example 3.16. The function g := x 7! x , with domain and co-domain
2
as in the previous example, is neither injective nor surjective. Indeed,
jAj = 5 and jg(A)j = 3, so g is not injective (for instance, 1 and 1 are
distinct elements of A but g( 1) = g(1) = 1 is the same element of B ).
Likewise, 1 62 g(A), so g is not surjective.
Example 3.17. Let
A = f0; 1; 2; 3; 4g B = f0; 2; 4; 6; 8g
and
f :A!B x 7! rem(6x; 10):
50 CHAPTER 3. SETS AND FUNCTIONS
x+1
f : Z n f0g ! Q f := x 7! : (3.13)
x
Division by zero requires that the integer x = 0 be excluded from the
domain of f , for otherwise we would have to set f0 = 1, but this value is
not a rational number. Alternatively, one could adjoin the point x = 1 to
the co-domain of f , and extend the de nition of f to x = 0 as follows:
x + 1 if x 6= 0,
f : Z! Q f := x 7! x
1 if x = 0
where Q = Q [f1g. With either de nition, the function f is injective. We
prove it for the case (3.13). If f (x) = f (y), then (x +1)=x = (y +1)=y, and
because x and y are nonzero, we have xy + y = xy + x, whence x = y. On
the other hand, f is not surjective. For instance, the rational 3 does not
belong to the image f (Z), because the equation f (x) = 3 yields x = 1=2,
which is not in the domain Z.
Example 3.19. Let S be the set of squares of elements of N
S = f02 ; 12 ; 22 ; 32 ; 42 ; 52 ; : : : g:
p if p + 2 2
n
: ! [ f0g p 7!
0 otherwise.
The function is neither injective nor surjective, which can be seen, for
instance, from the fact that (2) = (7) = 0: two elements of the domain
have the same image, and neither of them belongs to (). The set ()
consists of 0 and a representative for each pair of twin primes; see (3.6).
Once a new function is de ned, it can be used like any other library function,
by substituting any valid expression for its argument. In our example, valid
expressions are arithmetical and algebraic expressions
> f(2/3),f(joe),f(a^2-1);
5 ; joe + 1 ; a2
2 joe a2 1
We make some syntactical remarks.
The name of the function's argument (the variable appearing immedi-
ately on the left of the arrow operator) is invisible from the outside, and in
particular, it is unrelated to any variable with the same name that could
have been previously de ned. Study the following example carefully:
> g:=n->2*n:
> h:=whatever->2*whatever:
> whatever:=3:
> g(2),h(2),g(whatever),h(whatever),g(n),h(n);
4; 4; 6; 6; 2n; 2n
Common mistakes originate from assignments such as
> wrong(x):=2*x+1:
This statement does not de ne a Maple procedure, in spite of the fact that
it is syntactically legal. Thus,
> wrong(x),wrong(2),wrong(1/y);
2x + 1; wrong(2); wrong y1
One sees that Maple knows what is wrong(x), but nothing else (this feature,
which allows a function to be de ned at individual values of the argument,
is useful in the so-called recursive de nitions).
In the above construct, the value assigned to the variable f is a pro-
cedure de nition: x ! (x + 1)=x. However, for reasons of eÆciency of
computation, the function's symbolic name is not evaluated automatically
(the function's name is a pointer to the place in memory where its value is
stored)
52 CHAPTER 3. SETS AND FUNCTIONS
> f,whattype(f);
f; symbol
C : A ! f0; 1g x 7! (3.14)
0 if x 62 C
is called the characteristic function of C in A. A variant of the above
construction is the Boolean characteristic function, given by
if x 2 C
C : A ! ftrue; f alseg x 7!
true
f alse if x 62 C .
(3.15)
Example 3.21. For a non-empty set A the following holds
; (A) = f0g A (A) = f1g:
: n 7!
1 if n = 0 (3.16)
0 if n 6= 0.
The Maple implementation is straightforward:
> chi:=n->if n=0 then 1 else 0 fi:
The Boolean version of the same function is even simpler, as it does not
require the if-structure
> chi:=n->evalb(n=0);
54 CHAPTER 3. SETS AND FUNCTIONS
: Q ! f0; 1g : x 7!
0 if x 62 Z.
We note that a rational x is an integer precisely when its denominator is
equal to 1. Thus,
> chi:=x->if denom(x)=1 then 1 else 0 fi:
Example 3.31. Let A = f0; 1; 2; 3; 4; 5g. We show with Maple that the
mapping f : x 7! rem(x + 5; 6) of A to itself is bijective. Because domain
and co-domain are the same, it suÆces to check that A = f (A).
> f0,1,2,3,4,5g;
> evalb(map(x->irem(x+5,6),%)=%);
true
( ) = 10 ifotherwise
x is a multiple of 7
n
(b) f x
( ) = 10 ifotherwise
x and x + 2 are prime
n
(c) f x
(f ) The set of positive integers i for which the ith prime is twice a
prime plus one; hence compute the intersection between such set and the
set f101; 102; 103; 104; 105g.
Exercise 3.10. Let n be a positive integer. Construct a function nearp(n)
whose value is the prime number nearest n.
Exercise 3.11. Let r and s be positive rational numbers. Construct a
function K(r,s) whose value is true if there is an integer between r and s,
and f alse otherwise. (You may assume that r and s are not integers.)
Chapter 4
Sequences
Sequences are functions de ned over the integers. The ordering property
of the integers is what makes sequences special.
4.1 Basics
When the domain of a function is the set of positive integers N + , one speaks
of a sequence. Thus, a sequence f is a rule which associates to every positive
integer n a unique element f (n) of a given set B . For instance, let B = Z,
and let us consider the function f which associates to every natural number
n the value of the expression 2n n2 . We write
f : N+ ! Z f := n 7! 2n n2
or, more compactly
f (n) = 2n n2 n 1: (4.1)
The elements of this sequence are the integers f (1); f (2); f (3); : : : = 1; 0;
3; : : : . When a function is a sequence, it is customary to write fn in place
of f (n). So we write out the elements of the sequence as
f1 ; f2 ; f3 ; : : : ; fk ; : : :
and the entire sequence as ffk g, or, more pedantically, ffk g1 k=1 . In the
above notation, f is a function, k is an element of the domain of f , fk is
an element of the co-domain of f , and ffk g is the image (think about it).
The condition that the domain of a sequence be N + may be relaxed.
For instance, it may be convenient to restrict the domain to a proper subset
of the positive integers, or to extend it to the natural numbers N , or even
to the whole of Z. In the latter case, we speak of a doubly-in nite sequence
: : : ; a 2 ; a 1 ; a0 ; a1 ; a2 ; : : :
59
60 CHAPTER 4. SEQUENCES
The elements of such a sequence belong to the set ftrue; falseg: the rst
few of them are
false; true; true; false; true; false; true; false; false; false; : : :
Even though the function p is represented explicitly by the function isprime,
the process that allows Maple to determine whether p(n) is true or false
is complicated. In spite of some 2000 years of e ort, nobody has yet suc-
ceeded in reducing this algorithm to the evaluation of a simple `formula'.
If you test isprime on large values of n, you will notice how much Maple
struggles to give you the answer.
4.2. SEQUENCES WITH MAPLE 61
> seq(isprime(k),k=100..110);
false; true; false; true; false; false; false; true; false; true; false
Thus, 101, 103, 107, and 109 are prime.
Example 4.4. Generate the rst 10 elements of the sequence
k+1
f : k 7! 2 k 1:
k
We construct a user-de ned function for f .
> f:=k->(k+1)/k^2:
> seq(f(n), n=1..10);
3 4 5 6 7 8 9 10 11
2; ; ; ; ; ; ; ; ;
4 9 16 25 36 49 64 81 100
Example 4.5. Generate the set S of the rst 30 non-negative powers of
2.
> S:=fseq(2^i, i=0..29)g;
S := f1; 2; 4; 8; 16; 32; 64; 128; 134217728; 268435456; 536870912;
256; 512; 1024; 2048; 4096; 8192; 16384; 32768; 65536;
131072; 262144; 524288; 1048576; 2097152; 4194304;
8388608; 16777216; 33554432; 67108864g
While the expression sequence produced by seq was in the appropriate
order, the enclosure between curly brackets has resulted in a seemingly
arbitrary arrangement of the elements of the set.
62 CHAPTER 4. SEQUENCES
The seq command has the syntax seq(e,i=a..b), where e is any ex-
pression, i is a name, and a and b are expressions which must evaluate to
an integer. The expression a..b is of the range type
> whattype(a..b);
::
Before executing seq, Maple expands the range expression, which evaluates
to a sequence of integers. This can be done explicitly with the sequence
operator $
> $1..10;
1; 2; 3; 4; 5; 6; 7; 8; 9; 10
(The range a..b may also be replaced by more general expressions | see
chapter 6.)
The value of seq is a list of expressions separated by commas, called an
expression sequence, or, more brie y, a sequence. The following command
generates a sequence of sets
> seq(f$1..ng,n=0..4);
fg; f1g; f1; 2g; f1; 2; 3g; f1; 2; 3; 4g
> whattype(%);
exprseq
Unlike the elements of a set, the elements of an expression sequence are
arranged in a prescribed order. If in the range a..b the value of a is
greater than that of b, the result is the NULL sequence. NULL sequences
may be de ned explicitly, which is useful in initializations
> s:=NULL;
s :=
> s:=s,1,2,3;
s := 1; 2; 3
Example 4.6. We construct a function divs(n) whose value is the set of
divisors of a positive integer n (see also ?divisors). A straightforward (if
ineÆcient) approach consists in applying the function x 7! gcd(x; n) to the
set of the rst n positive integers. The latter is constructed by expanding
the range 1..n with the operator $. The mapping is performed with the
function map, whose rst argument is igcd. Because igcd is a function of
two variables, the second variable must be supplied as the third argument
of map.
> divs:=n->map(igcd,f$1..ng,n):
> divs(84);
f1; 2; 3; 4; 6; 7; 12; 14; 21; 28; 42; 84g
4.2. SEQUENCES WITH MAPLE 63
1 32 52 72 3 8 24 35
(iii) ; ; ; ; ::: (iv) 0; ; ; ; ; : : :
22 42 62 82 4 9 25 36
Exercise 4.2.
(a) Let n be a positive integer. Construct a function S(n) whose value is
the set of all rationals between 0 and 1 having the nth prime as denominator
(0 and 1 do not belong to S(n)).
(b) Let n be a positive integer, and let S (n) be the set of rationals between
0 and 1 having denominator n. Construct a user-de ned function for S .
(c) Verify that S (12) \ S (21) = S (3).
(d) From numerical experiments, conjecture the value of S (m) \ S (n), for
m; n 1.
Exercise 4.3. Construct the set of squares less than 5000 which are not
cubes, whence determine its cardinality.
Exercise 4.4 .
Let m and n be integers with m n, and let [m; n] be
an interval which includes the endpoints.
64 CHAPTER 4. SEQUENCES
1.5
0.5
-0.5
-1
4.3. PLOTTING THE ELEMENTS OF A SEQUENCE 65
> whattype(sq);
list
Maple automatically connects points with a segment, so the vertex ( 1; 1)
has to be entered twice, or one of the sides would be missing.
Plotting a sequence amounts to plotting points on the plane. Indeed, if
a1 ; a2 ; : : : ; an are elements of a sequence, they are represented by the points
(1; a1 ); (2; a2); : : : ; (n; an )
expressed in Cartesian coordinates. For illustration, let us consider the
graphical representation of the sequence of gaps between consecutive primes.
If pn is the nth prime we let
n 7! gn gn = pn+1 pn ; n = 1; 2; : : : :
We rst de ne a function for the sequence g
> g:=n->ithprime(n+1)-ithprime(n):
Then we construct the data set corresponding to the rst 300 elements of
this sequence, assign its value to a variable, and plot it
> gap:=[seq([n,g(n)], n=1..300)]:
> plot(gap,style=POINT,title="Gaps between primes");
30
25
20
15
10
been inserted using the title option, which is set equal to an expression
enclosed within double quotes (a string data type). The gaps are even in-
tegers, except for the rst one: g1 = 3 2 = 1. An n increases, larger
gaps appear. Overall, the average gap size seems to increase, but in a very
irregular fashion (the study of this phenomenon was a major concern for
19th-century number theorists). The largest gap among the plotted data is
located approximately between the 200th and the 250th prime. To locate
it precisely, we magnify a portion of the graph, reverting to line style for
better visualization.
> plot(gap,n=200..250,"g(n)"=15..35);
35
30
g(n) 25
20
It is clear that the maximum of g occurs for n = 217, where the gap size is
34
> ithprime(217),ithprime(218);
1327; 1361
The second and third arguments of the call to plot de ne the ranges in the
horizontal and vertical axes, respectively, labelling the corresponding axes.
The labels must be strings, whence the expression g(n) has to be enclosed
within double quotes, for it contains parentheses. Any other option (such
as a title) must be placed after the range speci cations (see ?plot and
?plot[options] for more information).
4.4. PERIODIC AND EVENTUALLY PERIODIC SEQUENCES 67
The number of elements in the repeating block (T in this case) is called the
period of the sequence. Periodic sequences are of great interest.
A periodic sequence is speci ed by specifying its repeating block. Let the
period be equal to T , and the repeating block a0 ; a1 ; : : : ; aT 1 . Periodicity
is expressed concisely by the recursive notation
ak+T = ak k0 (4.5)
which says that an arbitrary element of the sequence is equal to the cor-
responding element in the previous block. Let this element be an . By
moving backwards from block to block, we eventually reach the rst block
a0 ; : : : ; aT 1 , so we can identify an with one of its elements an , where n0 0
a:N ! f 1; 1g a := n 7! ( 1)n :
This sequence is periodic with period 2:
1| ;{z 1}; 1| ;{z 1}; 1| ;{z 1}; : : :
: n 7! gcd(n; a) n 0:
68 CHAPTER 4. SEQUENCES
80
60
40
20
> plot([seq([d,irem(200,d)],d=1..300)]);
200
150
100
50
n 0 1 2 3 4 5 6 7 8 9 10 11 12
quo(n; 5) 0 0 0 0 0 1 1 1 1 1 2 2 2
rem(n; 5) 0 1 2 3 4 0 1 2 3 4 0 1 2
The rst sequence displays a step-like behaviour, with steps of length 5 and
height 1. The second is periodic with period 5; i.e., it consists of inde nite
repetition of the same 5-pattern 0; 1; 2; 3; 4. This behaviour persists when
one replaces 5 by an arbitrary value of d. To prove it, we consider the
quotient-remainder equation (2.10). Replacing n by n + d we obtain
n + d = d (q + 1) + r 0 r < d; (4.9)
which shows that the quotient increases by 1 every d steps. Because the
quotient cannot decrease when n increases, then it must increase by 1 pre-
cisely once every d steps. Thus, the sequence n 7! quo(n; d) has a step-like
behaviour, increasing by 1 when n is a multiple of d.
Equations (2.10) and (4.9) show that dividing n and n + d by d gives the
same remainder. So the sequence n 7! rem(n; d) is periodic, with period at
most d. To show that the period is exactly d, we note that as n increases
by 1, then r either increases by 1 (if r 6= d 1), or it becomes zero (if
r = d 1).
The step-like behaviour of n 7! quo(n; d) can be thought of as the result
of superimposing a linear and a periodic sequence. From equation (2.10)
we obtain
1 r(n)
q(n) = n
d d
where the n-dependence of q and r has been made explicit. This sequence
has the form (4.8), with a = 1=d and P (n) = r(n)=d. The sequence P is
periodic with period d, because r is periodic with the same period.
Next we consider the modulation of a linear sequence by a periodic one.
With reference to (4.7) we have n 7! a(n) n, where the function a = a(n)
is periodic. As an example, consider the sequence
: n 7! lcm(n; d) (d 6= 0 xed):
Because d lcm(n; d) nd, we obtain
n n nd: (4.10)
From (2.28) and the fact that gcd(n; d) 6= 0, we have
d
lcm(n; d) = n :
gcd(n; d)
Because of the periodicity of the greatest common divisor, we see that
n 7! lcm(n; d) is the product of a linear and a periodic function.
72 CHAPTER 4. SEQUENCES
1000
800
600
400
200
0 20 40 60 80 100
1; 0; 1; 0; 1; 0; 1; 1; 0; 0; 1; 0; 1; 1; 1; 0; 0; 1; 0; 0; 1; 1; 0; 1; 0; 1; 0; 1
Farey sequences
For every integer n 1, let Fn be the set of rational numbers in the unit
interval, whose denominator does not exceed n
p
Fn =
q
2 Q; 1 q n; 0 p q : (4.11)
Thus,
0 1
F1 = ;
1 1
0 1 1
F2 = ; ;
1 2 1
0 1 1 2 1
F3 = ; ; ; ;
1 3 2 3 1
0 1 1 1 2 3 1
F4 = ; ; ; ; ; ;
1 4 3 2 3 4 1
0 1 1 1 2 1 3 2 3 4 1
F5 = ; ; ; ; ; ;
1 5 4 3 5 2
; ; ; ;
5 3 4 5 1
:
We have a nested sequence of nite sets, each properly contained in the
following one:
F1 F2 F3 : : : :
74 CHAPTER 4. SEQUENCES
The set Fn is called the nth Farey sequence (the appellative `sequence'
derives from the fact that the elements of the set Fn are usually considered
in ascending order). For each n and each denominator q in the range
1 q n, the corresponding numerator ranges from 0 to q, inclusive. In
this way, some fractions appear several times, but multiple occurrences are
eliminated in accordance with the de nition of a set. For instance, 0 = 0=q
and 1 = q=q appear for every value of q, while 1=2 appears only when q is
even.
We wish to construct a user-de ned function F(n) whose value is the
set Fn . We shall not be concerned with the ordering of the elements of Fn
(from the smallest to the largest), and accordingly we shall be using the set
data type. This will enable us to eliminate repeated entries automatically.
The starting point is to construct an expression sequence containing all
fractions with a given denominator q. (We exclude the elements 0 and 1,
to limit the number of repeated entries.)
> seq(p/q,p=1..q-1):
Next we embed the above sequence within an expression sequence which
determines the range of values assumed by the denominator q.
> seq(seq(p/q,p=1..q-1),q=2..n):
Then we add the missing elements 0 and 1, and construct the corresponding
set data type.
> f0,1, seq(seq(p/q,p=1..q-1),q=2..n)g:
Finally, we construct a user-de ned function whose value is the above ex-
pression.
> F:=n->f0,1, seq(seq(k/d,k=1..d-1),d=2..n)g:
> F(5);
1 1 2 1 3 1 2 3 4
0; 1; ; ; ; ; ; ; ; ;
2 3 3 4 4 5 5 5 5
Exercises
Exercise 4.5. Consider the following sequences n 7! an (pn denotes the
nth prime number)
(Note: Maple does not always display the prime divisors of an integer in
ascending order.)
Exercise 4.13. Let rn be the remainder of the division of the nth prime
number by 12 (thus, r6 = 1). It can be shown that for n 3, rn assumes
only the four values 1; 5; 7; 11 (can you prove it?). Write a user-de ned
function K(n) for the following characteristic function
K (n) = 1 if rn = 1 or 11
1 if rn = 5 or 7.
Exercise 4.14. Let Pk be the set of primes not greater than k, and let
Fk : Pk ! Z be the function sending x to x (x k). Construct Pk and
hence compute jFk (Pk )j, for k = 2309 and 2310. For which of these values
of k is Fk injective?
This result could not have been extracted from the de nition (4.13), because
for n = 0 the product is empty. With this device, we have extended the
de nition of the factorial function to all non-negative integers. Note that
this procedure cannot be used to extend the de nition of the factorial to
negative integers | try!
Factorials grow very rapidly. Below we compare the factorial function
with the exponential functions 2n ; 3n, and 4n , for n < 10.
n 0 1 2 3 4 5 6 7 8 9
n! 1 1 2 6 24 120 720 5040 40320 362880
2n 1 2 4 8 16 32 64 128 256 512
3n 1 3 9 27 81 243 729 2187 6561 19683
4n 1 4 16 64 256 1024 4096 16384 65536 242144
For any b > 1 the function bn is initially greater than n!, but the factorial
function eventually takes over. For instance, when b = 100 one nds that
n! overtakes 100n at n = 269.
What is the combinatorial meaning of the factorial function? We de ne
the concept of a permutation. A permutation of n objects in a row is a
rearrangement of them. The following is a permutation of n = 6 objects,
called a; b; c; d; e; f
a b c d e f 7! d a b f e c
(Formally, this is just a bijection of the set fa; b; c; d; e; f g.) How many
possible permutations of n objects are there? Let us call this number n .
Clearly 1 = 1 and 2 = 2. For n = 3 we have
abc acb bac bca cab cba:
Thus, 3 = 6. With a little patience, one nds that 4 = 24 and the
temptation to conjecture n = n! is irresistible.
This is indeed the case and the proof is simple. There are n ways of
choosing the position of the rst object. For every choice of the rst object
there are n 1 ways to choose the position of the second, so we have n(n 1)
choices of the destination of two objects, and so forth, until we reach the
last object for which there is only one choice, because the remaining n 1
objects are already in place.
It is possible to extend the de nition of the factorial function to real
numbers. The resulting function | called the gamma function | will be
dealt with in section 5.5.
The binomial coeÆcient and combinations n
Let n and k be positive integers. The binomial coeÆcient k is de ned as
follows
n n (n 1) (n 2) (n k + 1)
= : (4.15)
k k!
4.6. BASIC COUNTING SEQUENCES 79
Thus,
6 654
= = 20:
3 123
One nds immediately from the de nition
n n n(n 1)
=n = :
1 2 2
If k = 0 in (4.15), the product at the numerator is empty. In this case, we
de ne
n
= 1:
0
We look at small values of n and k, by displaying Pascal's triangle
1
1; 1
1; 2; 1
1; 3; 3; 1
1; 4; 6; 4; 1 (4.16)
1; 5; 10; 10; 5; 1
1; 6; 15; 20; 15; 6; 1
1; 7; 21; 35; 35; 21; 7; 1
1; 8; 28; 56; 70; 56; 28; 8; 1
The rows are labelled by the value of n, starting from 0 and proceeding from
top to bottom. The elements of the nth row are labelled by k, starting
from
k = 0 and proceeding from left to right up to k = n. For example, 75 = 21.
We note that the binomial coeÆcient is an integer, which is not at all
obvious from de nition (4.15). To prove this, we turn to the combinatorial
interpretation of the binomial coeÆcient. A combination of n objects taken
k at a time is any subset of k objects from a set of n distinct objects
(this is where the word combinatorics comes from!). The order in which
these objects are arranged makes no di erence. For instance, there are
10 combinations of ve objects taken two at a time, that is, 10 ways of
selecting two objects out of a set of ve. If the ve objects are taken from
the set fa; b; c; d; eg, then the 10 subsets of 2 elements are
fa; bg; fa; cg; fa; dg; fa; eg; fb; cg; fb; dg; fb; eg; fc; dg; fc; eg; fd; eg:
The number of combinations of n objects taken k at a time is given
by the binomial coeÆcient nk . To see this, we rst select k elements and
arrange them in a sequence (that is, in a speci ed order). There are n
choices for the rst element of the sequence. For each such choice there are
n 1 choices for the second element, and so forth, until we choose the kth
element, which can be done in n k + 1 di erent ways. In all, we obtain
n(n 1) (n k +1) sequences with k elements. But since in a combination
80 CHAPTER 4. SEQUENCES
the order in which these elements are arranged is irrelevant, we must divide
this number by all possible permutations of k objects, which we found to
be equal to k!. So the number of combinations is n(n 1) (n k + 1)=k!,
which is the binomial coeÆcient (see (4.15)).
The binomial coeÆcient can be represented in terms of factorials as
follows
n n (n 1) (n 2) (n k + 1)
=
k k!
n (n 1) (n 2) (n k + 1) (n k)!
=
k! (n k)!
n!
= :
k! (n k)!
From this we obtain
n n
= : (4.17)
k n k
If n < k, then n k + 1 is either 0 or negative. Then, from (4.15)
n n (n 1) 1 0 1 (n k + 1)
= = 0:
k k!
Taking into account (4.17), we see that for xed n, the binomial coeÆcient
can be de ned for any integer k, positive or negative, but its value will be
nonzero only for 0 k n.
The binomial coeÆcient nk is implemented in Maple by the function
binomial(n,k)
> evalb(binomial(10,4)=10!/(4!*(10-4)!));
true
To save typing, we can shorten the symbolic name binomial to bc via the
alias statement as follows
> alias(bc=binomial):
This will cause Maple to translate bc(n,k) into binomial(n,k).
> seq(bc(8,k),k=-2..10);
0; 0; 1; 8; 28; 56; 70; 56; 28; 8; 1; 0; 0
The name binomial coeÆcient derives from the following result, called
the binomial theorem
Theorem 2
n
X n k n
(x + y) =
n
xy k
n 0:
k=0
k
4.6. BASIC COUNTING SEQUENCES 81
x8 + 8 x7 y + 28 x6 y2 + 56 x5 y3 + 70 x4 y4 + 56 x3 y5 + 28 x2 y6 + 8 xy7 + y8
Setting x = y = 1 and x = 1; y = 1 in Theorem (2), respectively, we
obtain the well-known identities
n n
X n X n
= 2n ( 1)k
= 0:
k=0
k k=0
k
Exercises
Exercise 4.15.
(a) Determine how many di erent football teams (11 players) can be made
out of 30 players.
(b) De ne a function row(n) whose value is the nth row of Pascal's triangle,
in the form of an expression sequence. Thus,
> row(3);
1; 3; 3; 1
(c) Using row generate the rows from 0 to 7 of Pascal's triangle.
(d) In Pascal's triangle, observe the symmetry induced by the identity
n n
=
k n k
which causes every entry to appear exactly twice, except when n is even,
in which case there is a unique central element. Construct the sequence of
the central elements of the even rows of Pascal's triangle
2m
cm = m 0:
m
Compute c15 ; c50 .
(e) Find an explicit formula for cm , involving factorials. Check your for-
mula with Maple, in the case m = 200.
(f ) Plot the coeÆcients of (1 + x)30 . Convince yourself that the value of
the central coeÆcient as inferred from the plot is consistent with what you
have computed in part (d).
(g) From the plot alone, determine how many coeÆcients are larger than
108 , identifying explicitly the indices of the corresponding binomial coef-
cients. Then compute all the coeÆcients that are greater than 108 with
Maple.
82 CHAPTER 4. SEQUENCES
> f([a,b,b]);
[ b; a; b; a; b ]
The construction of the recursive sequence (4.20) now proceeds as usual,
e.g.,
> [a]:f(%):f(%):f(%):f(%):f(%);
[ b; a; b; a; b; b; a; b ]
In the general case, a sequence de ned recursively has the following
structure
xt = f (xt 1 ; xt 2 ; : : : ; xt s ; t) t s: (4.21)
Here f is an arbitrary function of xt 1 ; xt 2 ; : : : ; xt s as well as of t. Such a
sequence is speci ed by assigning the s initial values x0 ; x1 ; : : : ; xs 1 . The
integer s is called the order of the recursion. If the function f in (4.21)
depends on t, the recursive sequence is said to be non-autonomous.
Example 4.14. The factorial sequence (4.14) is a rst-order non-autonom-
ous recursive sequence. With the notation of (4.21), we have f (xt 1 ; t) =
t xt 1 .
Example 4.15. The Fibonacci numbers. We consider the recursive se-
quence
F0 = 1 F1 = 1 Ft+1 = Ft + Ft 1 ; t 1:
With reference to (4.21), we have f (xt 1 ; xt 2 ) = xt 1 + xt 2 , and there-
fore this is a second-order autonomous recursive sequence (s = 2 and no
dependence of f on t). The rst few elements of this sequence are displayed
below
t 0 1 2 3 4 5 6 7 8 9 10 11 12
Ft 0 1 1 2 3 5 8 13 21 34 55 89 144
The numbers Ft are known as Fibonacci numbers (named after the 13th-
century mathematician Leonardo da Pisa, nicknamed Fibonacci). They
have several important combinatorial applications. We compute F10 with
Maple
> 0:1:%+%%:%+%%:%+%%:%+%%:%+%%:%+%%:%+%%:%+%%:%+%%;
55
Maple has a library function generating the Fibonacci numbers. For details,
see ?fibonacci.
Example 4.16. We prove by induction the following identity (discovered
by Cassini in 1680)
Fn+1 Fn 1 F 2 = ( 1)n
n n 1: (4.22)
4.7. SEQUENCES DEFINED RECURSIVELY 85
3x + 1
f :Q !Q f := x 7!
x+3
:
86 CHAPTER 4. SEQUENCES
Prove that g is indeed a function of Q I into Q I [ f0g; that is, prove that if
r 2 Q I , then g(r) 2 Q I [ f0g.
(f ) Using the function fp developed above, construct the function g(r)
given by (4.24).
(g) Consider the following recursive sequence
g(rt ) rt 6= 0
r0 = r rt+1 = t 0: (4.25)
0 otherwise
Let r = 11=19. Determine the smallest value of t for which rt is equal to
zero.
(h) Choose an arbitrary rational number r 2 Q I . Verify that the corre-
sponding sequence rt eventually reaches zero.
(i) Prove that for any choice of r 2 Q I , the sequence rt with initial condi-
tion r0 = r reaches 0 in a nite number of steps.
Exercise 4.21 .
Let the sequence rt be as in (4.25). From the previous
problem it follows that to every rational r in QI we can associate the natural
number (r), which is the number of iterations needed to reach zero starting
from r
: QI !N := r 7! smallest t for which rt = 0, and r0 = r:
For instance, (4=7) = 3, because
4
r0 =
7
! r1 = g(r0 ) = 34 ! r2 = g(r1 ) = 13 ! r3 = g(r2 ) = 0:
(4.26)
Now let Fn be the nth Farey sequence, de ned in (4.11). Then Fn is a
nite subset of Q I . Let n be the maximum value attained by (r) as r
scans all the elements of the set Fn . For example, since 3=4 2 F5 , from the
data (4.26) we obtain the estimate 5 2 (in fact, 5 = 3).
4.7. SEQUENCES DEFINED RECURSIVELY 87
Find a good estimate for n , in the case n = 10000. Speci cally, obtain
an estimate 10000 by producing a rational r in F10000 for which =
(r) is as large as possible. You must display the calculations showing that
the sequence with initial condition r reaches zero in steps. [Hint: The set
F10000 has 30397486 elements. Careful experimentation and thinking will
get you much further than blind trial and error.]
Exercise 4.22. In this problem, we compare the growth rate of the expo-
nential sequence 2n with that of the power sequence nb , where b is a given
integer greater than 1, and n = 1; 2; : : : . Both sequences diverge to in nity,
but the exponential `gets there rst'; that is, it grows faster than any power.
However, the sequence of powers starts o faster than the exponential (for
n > 1) but is eventually overtaken by the latter. This phenomenon |
illustrated below for the cases b = 2 and 3 | becomes more pronounced as
b gets large.
n 1 2 3 4 5 6 7 8 9 10
2n 2 4 8 16 32 64 128 256 512 1024
n2 1 4 9 16 25 36 49 64 81 100
n3 1 8 27 64 125 216 343 512 729 1000
Ignoring the value n = 1, we see that 2n overtakes n2 at n = 5, and n3 at
n = 10.
For xed b > 1, let Ob be the smallest n > 1 for which 2n > nb
Ob : n 7! smallest n for which 2n > nb n > 1; b 1:
Thus, Ob is the integer at which 2n overtakes nb , whence O2 = 5 and
O3 = 10, from the above data. Compute and tabulate Ob for b = 2; : : : ; 10.
Display only the calculations that suÆce to establish your result.
Exercise 4.23. In this problem, we compare the behaviour of the factorial
function n! with that of the exponential function bn , where b is a given
positive integer. Convince yourself that for any xed b 1, n! is smaller
than (or equal to) bn for suÆciently small n. However, the factorial function
is known to grow faster than any exponential, so at some point n! will
overtake bn .
Let Ob be the point at which the factorial overtakes the powers of b. So
we have an integer sequence O1 ; O2 ; O3 ; : : : . Compute and tabulate Ob for
b = 2; : : : ; 10. Display only the calculations that suÆce to establish your
result.
Exercise 4.24. Consider the following recursive sequence
0=1 k+1 = f ( k ) =
2 + 1; k 0:
k
(b) Show that 9 has 91 decimal digits, without using length. [Hint: how
do you show that 88 has two decimal digits?]
(c) This is the fastest-growing sequence you have come across so far. It
grows faster than the factorial sequence. Indeed, k eventually overtakes
(k!)b for any positive integer b. Let Ob be the point at which k overtakes
(k!)b . Compute O1 and O2 .
Exercise 4.25. Construct the fastest-growing sequence you can think
of. Speci cally, replace the 10 question marks in the following function
de nition
> fast:=n->??????????;
with at most 10 characters so as to obtain a Maple function fast(n),
representing a rapidly growing sequence de ned for all positive n. (Any
character that is on the keyboard is allowed, but you cannot make use of
functions you have de ned elsewhere.)
What counts is the asymptotic behaviour of your sequence (how fast it
grows when n becomes very large). For instance, n ! n2 starts slower than
n ! n + 1010 , but it gets to in nity rst.
Chapter 5
= 3:141592 : : :
89
90 CHAPTER 5. REAL AND COMPLEX NUMBERS
> Digits;
10
> Pi,evalf(Pi);
; 3:141592654
> Digits:=20:
> evalf(Pi),evalf(Pi,3);
3:1415926535897932385; 3:14
5.1. DIGITS OF RATIONALS 91
> p:=1:q=7:
> seq(iquo(p*10,q,'p'),k=1..7);
1; 4; 2; 8; 5; 7
Exercises
Exercise 5.1. Consider the following rational numbers r
10001 13 1
(i) (ii) (iii) :
11110 23 585728
Let m be the number of the pre-periodic decimals, and n the period of the
repeating decimals of r.
(a) For each value of r, evaluate a suÆcient number of decimal digits, and
hence conjecture the values of m and n. Identify the pre-periodic head of
m digits, as well as the n repeating digits of the tail.
(b) Express the denominator of r in the form 2a 5bR, where R is relatively
prime to 2 and 5. Compute m from a and b, and check that for R > 1 you
have n < R.
(c) Summarize your results in a table.
(d) For the case (ii) determine the third digit of r using only the functions
irem and iquo.
Exercise 5.2. Let x = a=b and y = c=d be rational numbers, in reduced
form. Their midpoint and mediant, given by (x + y)=2 and (a + c)=(b + d),
respectively, lie in [x; y] | the interval with endpoints x and y (cf. equations
(2.20) and (2.21)).
(a) Construct a function mid(x,y) whose value is the midpoint of x and
y.
(b) Construct a function med(x,y) whose value is the mediant of (the
reduced formpof) x and y.
(c) Let = 2 1. (You may assume that is not rational.) Show that
lies in [0; 1].
(d) Compute the midpoint r of 0 and 1, using mid, hence decide which
of the two sub-intervals [0; r] or [r; 1] contains . (Convince yourself that
6= r, because r is rational and is not.) Then compute the midpoint of
that interval, and decide which of the two resulting sub-intervals contains
, and so on. Repeat this process until you have determined a rational
number whose distance from is less than 10 2.
(e) Do the same with the mediant in place of the midpoint, using med.
Compare the performance of the two methods with a short comment.
Exercise 5.3 .
Prove that if the digits of a number are eventually
periodic, then one can nd integers k and m such that 10k + m has
periodic digits.
96 CHAPTER 5. REAL AND COMPLEX NUMBERS
Exercise 5.4 .
Prove that a number whose digits are eventually periodic
is rational. [Hint: rst prove it in the periodic case, then use the result of
the previous exercise.]
In the previous section we have identi ed the distinctive feature of the digits
of a rational number, their eventual periodicity. This gives us a constructive
method for building irrational numbers. All we have to do is to produce
strings of digits that do not repeat, some examples of which are given below
(these examples, however, turn out to be atypical, as we shall see)
1 = 0:1234567891011121314151617181920212324252627282930 : : :
2 = 0:4040040004000040000040000004000000040000000040000 : : :
3 = 0:1223334444555556666667777777888888889999999991010 : : :
(5.7)
Putting together the rational numbers (eventually periodic digits) and
the irrational numbers (non-eventually periodic digits), we obtain the set R
of real numbers. Such set is the collection of all decimal expansions having
a nite number of digits to the left of the radix point. Any such expansion
de nes an in nite sum whose value is the real number in question (see
exercises). It should be remembered that the correspondence between digit
strings and real numbers fails to be bi-unique in the case of rationals whose
denominator is divisible only by 2 or 5, which have two distinct expansions.
This ambiguity gives rise to quite a lot of problems in numerical analysis,
the branch of computing concerned with manipulating real numbers.
Because R contains Q , we have the chain of inclusions
N Z Q R:
The arithmetical operations in R can be de ned, for instance, as the limit
of operations on rationals, but we shall not be concerned with this problem
here. All we need to note is that, like in Q , in R we can perform all four
arithmetical operations unrestrictedly, excluding division by zero. So R,
like Q , is a eld.
A distance in R
We de ne a distance in R in the same way as we did in Q . The distance
between two real numbers x and y is de ned as the size of the real number
x y, which is jx yj. If two real numbers share the beginning of their
sequence of digits, then they are close to each other: the larger the number
of common digits, the closer they are. For instance, let us consider the
three numbers
= 3:14159265358979 : : :
r1 = 3:14158430114199 : : :
r2 = 3:14159265359001 : : :
5.2. REAL NUMBERS 97
We see that r1 has 4 digits after the decimal point in common with , while
r2 has 9 of them. We nd
j r j1 = 0:000008 : : : ' 10 5
j r j2 = 0:00000000008 : : : ' 10 10
The converse is not true: two real numbers may be very close to each
other without sharing any digit at all. This phenomenon originates from
the fact that certain rationals have two distinct decimal representations
(cf. Theorem 3.2). If x is one such rational, then closeness to x means
identi cation of the digits of one of the two representations, that is
x = 12 = 0:500000000000000 : : : = 0:499999999999999 : : :
r1 = 0:50008430114199 : : :
r2 = 0:49999999999001 : : :
Thus, r2 is closer to 1=2 than r1 .
Real numbers in Maple
A real number in Maple is a decimal number with radix point and sign.
The positive sign may be omitted.
> 1.5, -33.0, 0.0001, +41.41;
1:5; 33:0; :0001; 41:41
> seq(whattype(x),x=%);
float; float; float; float
The above syntactical construction with seq is novel, since the running
index x is extracted from the elements of an arbitrary expression sequence,
rather than from a range. The expression sequence can also appear directly
as the second argument of seq, but in this case it must be enclosed in
parentheses, because a call to seq requires two arguments.
> seq(whattype(x),x=(1.5,-33.0,0.0001,+41.41));
We shall return to this point in section 6.3.
The word oat denotes the oating-point data type, which refers to the
way these data are represented in Maple (see below).
Very large or very small real numbers are more conveniently written in
exponential notation, which mimics more closely the way these numbers are
represented. The syntax is the following
xey ! x10y
where x is a real or an integer, y is an integer, and they are separated by
the character e. This corresponds to multiplying x by 10y . Thus, 1.5e-4
represents the number 1:5 10 4 = 0:00015.
98 CHAPTER 5. REAL AND COMPLEX NUMBERS
> 1.5e-4,1.5e4,1.5e-20,1.5e20;
:00015; 15000:; :15 10 19; :15 1021
The function evalf can be used to transform integer or rational data types
(among others) into the oating-point type
> 3,1/3,evalf(3),evalf(1/3);
1
3; ; 3:; :3333333333
3
Expressions such as 3. and .3 are valid real numbers, but their use is
discouraged because the radix point is easily overlooked, as illustrated in
the following example
> (1+2)/(3+4)*5+6-7/8;
407
56
> (1+2)/(3+4)*5+6-7/8.;
7:267857143
Note that the oating-point data type is `contagious'. The presence of the
single real number 8. forces the whole expression to become real.
The oating-point representation is non-exact, since a computer cannot
store in nitely many digits. It should be noted that while integers and
rationals can also be represented as oating-point numbers, the two repre-
sentations are vastly di erent. Every oating-point number is represented
as a pair of integers, called the mantissa and the exponent.
3:141592654 = mantissa 10 exponent = 3141592654 10 9
Mantissa and exponent are operands (that is, components) of the expression
representing the real number
> op(evalf(Pi));
3141592654; 9
The function op returns the operands of an expression (see chapter 6).
We compare the representations of 1=3 as a rational and as a real, with
10 digits accuracy
> op(1/3);op(evalf(1/3));
1; 3
3333333333; 10
This result says that
1
3
' 3333333333 10 10
We stress again that the oating-point representation is only approximate
> 3*(1/3),3*evalf(1/3);
1; :9999999999
5.3. RANDOM AND PSEUDO-RANDOM DIGITS
99
Exercises
Exercise 5.5. The following numbers are rational approximations to =
3:1415 : : :
22 314 333 355 355 103993 31415926
; ; ; ; ; ; : (5.8)
7 100 106 113 114 33102 10000000
(a) Create a Maple list whose elements are these numbers.
(b) De ne a Maple function d(x), which returns the distance between x
and : d(x) = jx j.
(c) Compute the distance between and every element of the sequence
(5.8), whence order the elements of the sequence (5.8) according to how
well they approximate (begin with the worst approximation).
Exercise 5.6. Let be a real number, and let
1
g(x) = e (x 2) :
6
f (x) = x
2
For certain values of , the equation f (x) = g(x) admits three solutions,
which correspond to three intersections of the graphs of f and g. These
values of form an interval [a; b]. Plot f and g in the same plot, for various
values of of your choice, and by examining the graphic data determine
approximate values for a and b (10% accuracy will suÆce).
Exercise 5.7. Let D be a positive integer. We consider the following
recursive sequence of rational numbers
x2 + D
x0 = 1 xt+1 = t ; t 0:
2 xt
p
It can be shown that the elements of this sequence approach D very
quickly. This method for computing the square root is called Newton's
method.
Usingp the pabove sequence twice, determine a rational number approxi-
mating 2 + 3 with an error not greater than 10 5.
Exercise 5.8 .
Prove that any decimal expansion with nitely many dig-
its to the left of the radix point de nes a unique number (you must prove
the convergence of a series).
The examples of irrational numbers that were given in (5.7) are, in a sense,
misleading. There are obvious patterns in their digit sequences. The knowl-
edge of such patterns can be exploited to specify these numbers concisely,
using a very small amount of information. For instance, the number 1
is the real number whose digits are given by the sequence of all positive
100 CHAPTER 5. REAL AND COMPLEX NUMBERS
integers. Likewise, the number 2 is de ned by the rule that its nth digit
is equal to 4 if n is a number of the form n = k(k + 1)=2 (a triangular
number), and to 0 if it is not, etc. These de nitions could be translated
into computer programs that generate these numbers to any desired degree
of accuracy.
However, the digits of a real number are not subject to any constraint,
in the sense that any sequence of digits de nes a unique number. So, if we
choose a number blindly, we should expect its digit sequence to be not only
aperiodic, but also patternless. The validity of this observation is con rmed
by a celebrated theorem of complexity theory, which states that almost all
numbers have random digits.
The expression `almost all' is to be intended in a probabilistic sense: a
blind choice will give you a random number unless you are extremely lucky
(or extremely unlucky, depending upon which way you look at it). Said
di erently, if you take all possible strings of n digits, where n is a large
integer, those that are not random form a vanishing fraction of the total.
Randomness is the property we usually associate to the outcome of
a gambling device. Loosely speaking, a sequence of digits is said to be
`random' if the most economical way of specifying its elements is to list
them all explicitly, one by one. So a sequence is random if the information
it contains cannot be compressed in any way. Any computer program that
generates the rst n bits of a random sequence cannot itself be shorter than
n bits, lest we would have achieved a compression of information stored in
the sequence.
To illustrate this concept, suppose we have to send to a distant galaxy
the rather dull sequence consisting of the rst billion odd integers, and
suppose that the cost of the transmission is very high. It is quite possible
to minimize the amount of transmitted data: instead of sending the entire
sequence, we send the Maple statement seq(2*i-1,i=1..10^9);. This
is a compressed version of the same information (21 characters in all!),
which the receiver can decode, recovering the original information. This
substantial data compression was made possible by the very non-random
nature of our sequence. But if we had to transmit, say, the outcome of all
football games since the beginning of the century, one can see that it is
going to cost a lot more.
We seem to have reached an impasse. If we are to build a very long string
of digits, then we need some kind of algorithm | a sequence of computer
instructions to be repeated automatically. However, the very existence of
such algorithm makes the outcome non-random, according to our de nition
of randomness. It then appears that a truly random sequence cannot be
meaningfully de ned, represented, computed, or predicted: it is terminally
beyond our grasp. But then, how close can one get to observing randomness
inside a computer? What a machine can generate are the so-called pseudo-
random numbers: they `look like' random numbers, but they are actually
5.3. RANDOM AND PSEUDO-RANDOM DIGITS
101
p
Proof.pAssume 2 is rational. Then there exist two integers a and b such
that 2 = a=b, and we shall agree that a and b are relatively prime (if
they are not, we rst divide by their greatest common divisor). Squaring,
we obtain 2b2 = a2 , whence a2 is even. From the fundamental theorem of
arithmetic, we can then conclude that a is even (why?), and we let a = 2k.
But then 2b2 = (2k)2 = 4k2 and so b2 = 2k2 , and with the same reasoning
we conclude that also b is even. But this is impossible,
p because a and b
were assumed to have no common divisor. So 2 is irrational.
There are still arithmetical problems in R, and they originate from the fact
that the square of a real number cannot be negative. Suppose that this was
p py and a positive real number
not true, and that we could nd a real number
a such that y2 = a. Then by writing a = a a, and letting x = y= a, we
p
would have a real number x (why real?) satisfying the equation x2 = 1, a
nice quadratic equation with integer coeÆcients. So all problems in R can
be traced to the impossibility of solving the equation x2 = 1, for a real
number x. We have experienced similar problems in Q , when we attempted
to solve equations like x2 = 2. That led to the introduction of R.
A solution of the equation x2 = 1 is `a thing whose square is equal to
minus one', which is often called the imaginary unit and denoted by the
letter i. We shall not attempt to ascertain what such object actually is;
all we need to know is that if we square it, we get 1. If we assume that
i obeys ordinary arithmetical rules, we must then conclude that i also
satis es the same equation, since ( i)2 = ( 1)2 i2 = 1 i2 = 1. We have
i0 = 1; i1 = i; i2 = 1; i3 = i; i4 = 1; etc: (5.9)
p i,1namely
That is, the sequence of powers of ik ; k = 0; 1; 2; : : : is periodic
with period 4. Maple represents by the symbol I and we can easily
generate a few terms of this sequence, to emphasize its periodicity
> seq(I^k,k=0..12);
1; I; 1; I; 1; I; 1; I; 1; I; 1 I; 1
5.4. COMPLEX NUMBERS 103
This is not the rst time we nd a number whose powers form a periodic
sequence. The powers of the integers 1 and 1 also generate periodic
sequences, with period 1 and 2, respectively
> seq((-1)^k,k=0..12);
1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1 1; 1
So far we have considered i in isolation. Now we combine it with ele-
ments of R, beginning with expressions of the type
z = r0 + r1 i + r2 i2 + r3 i3 + r4 i4 + + rn in rk 2 R:
Because of (5.9), this expression becomes
z = r0 +r1 i r2 r3 i+r4 +r5 i+ = (r0 r2 +r4 )+i (r r +r ):
1 3 5
The term in the rst set of parentheses is a nite sum of real numbers, and
it is therefore a real number. (In nite sums may also be meaningful, but
their study transcends the scope of this course.) The same holds for the
sum in the second set of parentheses, and so z is of the form
z = r + is r; s 2 R : (5.10)
The real numbers r and s are called the real part and imaginary part of z ,
respectively.
Next we consider the four arithmetical operations with expressions of
the type (5.10). Let z = r + is and w = t + iu. We have
z w = r + is t iu = (r t) + i (s u)
zw = (r + is)(t + iu) = (rt su) + i (ru + ts)
z r + is (r + is)(t iu) (rt + su) + i (st ru) (5.11)
w = t + iu = (t + iu)(t iu) = t2 + u2
rt + su
= 2 2 + i 2 2: st ru
t +u t +u
For division to make sense, we must assume that all denominators are
nonzero. For this it is suÆcient to require that w 6= 0, that is, that t and u
are not both zero. This in turn guarantees that the quantity t iu is also
nonzero, and so is the product (t + iu)(t iu) = t2 + u2 .
Equations (5.11) show that all numbers of the form (5.10) form a set
closed under the four arithmetical operations, excluding division by zero.
This is the set C of complex numbers. So C is a eld. We see that C is
obtained from R by adjoining to it the quantity i, and then allowing unre-
stricted arithmetical operations between i and the elements of R, assuming
that i obeys ordinary arithmetical rules.
Letting s = 0 in (5.11), we obtain z = r +0 i = r, a real number. Thus,
C contains R, the set of complex numbers with zero imaginary part, and
we have the chain of inclusions
N ZQ R C : (5.12)
104 CHAPTER 5. REAL AND COMPLEX NUMBERS
As far as `ordinary' numbers are concerned, this is the end of the road.
There is no arithmetical need of going beyond C . The distinguished 19th-
century mathematician L. Kronecker once said:
`God gave us the integers, the rest is the work of man.'
You can begin to see the giant edi ce mathematicians have constructed
from the natural numbers alone. The diÆcult step is the one between Q
and R. In fact, only two integers are needed to construct a rational, and
only two reals are needed to construct a complex number. But in nitely
many rationals are needed to construct a real number. In fact, the domain
lying between Q and R (or C ) is by far the most interesting one, but its
study lies beyond the scope of this course.
The following table summarizes the most signi cant steps in the journey
from N to C . The need of enlarging a set of numbers originates from the
impossibility of solving certain equations.
FROM N TO C
solve solve solve solve
set x+a=b ax = b x2 = a x2 = a
a; b 2 N a; b 2 Z; a 6= 0 a2N a2Z
N no no no no
Z yes no no no
Q yes yes no no
R yes yes yes no
C yes yes yes yes
A distance in C
How do we measure the size of a complex number? The real numbers were
represented geometrically as points on a line, and we de ned their size to
be the distance from the origin. Likewise, complex numbers are points in
the plane, so it is natural to represent the size of a number as its distance
from the origin in C .
If z = x + iy, we let z = x iy. The number z is called the complex
conjugate of z , and from (5.11) we nd
z z = (x + iy)(x iy) = x2 + y2 :
But then, from Pythagoras' theorem, the distance of the point z from the
origin is given by the modulus of z
p p
jz j = z z = x 2 + y2 : (5.13)
Since
(z) = x iy = x + iy = z;
5.4. COMPLEX NUMBERS 105
we have that p p
jzj = zz = zz = jz j:
As we did for Q and then for R, we de ne the distance between two
complex numbers z1 and z2 as the size of their di erence, which is jz1 z2j.
On the plane, the triangle inequality (2.6) justi es its name: it says that
if x, y, and z are complex numbers representing the vertices of a triangle,
then the length of the side connecting x to z cannot be greater than the
sum of the length of the other two sides.
So in C , like in R, there is an arithmetical structure and a distance (but
not an ordering relation ).
Complex numbers in Maple p
We have seen that in Maple the number 1 is represented by the symbolic
name I. The basic operations of complex arithmetic are straightforward,
with several intrinsic functions available for this purpose.
> z:=2-I:
> Re(z),Im(z),conjugate(z),abs(z);
p
2; 1; 2 + I; 5
An expression involving complex numbers with rational real and imaginary
parts is automatically simpli ed to the form x + iy with x; y 2 Q
> z^2+1/z;
17 19
I
5 5
> evalf(%);
3:400000000 3:800000000 I
Note that evalf acts separately on the real and imaginary parts of a com-
plex expression (see also section 6.3).
The simpli cation to the form r + is is not carried out in general. To
achieve it, one must use the standard library function evalc (evaluate to
complex)
> 1/(x+I*y);
1
x+Iy
> evalc(%);
x Iy
x + y2
2
x + y2
2
as points in the complex plane. The data are arranged as a list consisting
of two-element lists [xn ; yn ], where xn and yn are the real and imaginary
part of zn, respectively.
> z:=n->(1-n*I)/(n+4*I^n):
> data:=[seq([Re(z(n)),Im(z(n))],n=1..30)]:
> plot(data);
-1
-2
-3
Exercises
Exercise 5.9. Let z = 11 19i. Compute, with Maple
1=z; z; jz j; arg(z); Re(z ); Im(z ); 1 z2:
where z is the complex conjugate of z .
Exercise 5.10. Which of the following complex numbers
109
z1 = z2 = 7 + 7i z3 = 5 9i z4 = 1 10i
11
is nearest to the origin? Which is nearest to i?
Exercise 5.11. Consider the following recursive sequence of complex
numbers
4 z i
z0 = (9 + i) zt+1 = 8 t ; t 0:
41 i zt + 10
5.4. COMPLEX NUMBERS 107
Maple has several built-in functions for performing the basic operations of
real and complex calculus. The argument of the trigonometric functions is
in radians.
REAL AND COMPLEX FUNCTIONS
Trigonometric, hyperbolic, and logarithmic functions
sin(x),cos(x),tan(x) Sine, cosine, tangent
of the angle in radians.
sec(x),csc(x),cot(x) Secant, cosecant, cotangent.
arcsin(x),arccos(x), Arc sine, arc cosine, arc tangent,
arctan(x) with range in [ =2; =2].
sinh(x),cosh(x),tanh(x) Hyperbolic sine, cosine, tangent.
sech(x),csch(x),coth(x) Hyperbolic secant, cosecant,
cotangent.
sqrt(x) Square root.
exp(x) Exponential function ex.
log(x) Natural logarithm (base e).
log10(x) Logarithm to the base 10.
log[b](x) Logarithm to the base b.
GAMMA(x) Gamma function.
Other functions
abs(x) Absolute value.
sign(x) Sign of x (1 or 1).
max(x,y,: : :) The largest value among arguments.
min(x,y,: : :) The smallest value among arguments.
round(x) Round x to the nearest integer.
trunc(x) Truncate x to its integer part.
floor(x) Floor function bxc.
ceil(x) Ceiling function dxe.
evalf(z,n) Evaluate z to n oating-point digits.
Complex arithmetic
Re(z) Real part of z .
Im(z) Imaginary part of z .
conjugate(z) Complex conjugate of z .
argument(z) Argument of z (principal value).
evalc(z) Split z into real and imaginary part.
5.5. STANDARD LIBRARY FUNCTIONS 109
This shows that (x + 1) = x!. Note, however, that the integral (5.15) is
de ned also when x is not an integer.
In Maple, the symbolic name for the gamma function is GAMMA. Maple
knows that the value of at positive integers is an integer. It also knows
the values of at half-integers.
> seq(GAMMA(x),x=1..5);
1; 1; 2; 6; 24
> seq(GAMMA(evalf(x)),x=1..5);
1:; 1:; 2:; 6:; 24:
> seq(GAMMA(x-1/2),x=1..5);
p; 1 p; 3 p; 15 p; 105 p
2 4 8 16
> seq(GAMMA(evalf(x-1/2)),x=1..5);
> simplify(%);
(n + 1) (n + 4) 1
(n + 4)
5.5. STANDARD LIBRARY FUNCTIONS 111
> plot(sin(x)*sin(10*x)*sin(100*x),x=0..Pi);
0.5
-0.5
-1
0.4
0.2
-0.2
-0.4
Chapter 6
Structure of expressions
In previous chapters, we have encountered a variety of data types, e.g.,
> 3,2/3,2.95,joe,[a,b],fa,bg:
> seq(whattype(x),x=%),whattype(%);
integer; fraction; float; symbol; list; set; exprseq
In this chapter, we consider the way Maple represents composite data, of
which sets and lists are simple examples. In particular, we analyze in detail
the sum, product, and exponent data types, which are used to represent al-
gebraic expressions. This chapter is more technical than mathematical, but
it is necessary for the arithmetic of polynomials and rational functions, and
for understanding the behaviour of certain functions acting on expressions,
such as map, seq, select, etc.
x := a + 2bc b3 (6.1)
> whattype(x);
+
In the rst instance, Maple regards x as a datum of the sum type. To list
the operands of this expression, we use the function op
> op(x);
a; 2bc; b3
> whattype(%);
exprseq
113
114 CHAPTER 6. STRUCTURE OF EXPRESSIONS
a + 2bc b3
(+)
. # &
a 2bc b3
> op(middle);
2; b; c
These operands are primitive
> seq(whattype(z),z=%);
integer; symbol; symbol
The rightmost operand is also a product
> whattype(right);
> op(right);
1; b3
> seq(whattype(z),z=%);
integer; ^
The expression b3 is an exponent, with operands b and 3
> op(op(2,[%%]));
b; 3
(The use of square brackets is necessary here, lest the innermost op would
have been called with the wrong number of arguments.) The above sub-
expressions are represented as
2bc b3 b3
a + 2bc b3
(+)
. # &
a 2bc b3
() ()
.#& .&
2 b c 1 b3
(^)
.&
b 3
Figure 6.3: The tree associated with the complete representation of the
expression a + 2bc b3 .
The standard library function subs substitutes expressions with other ex-
pressions. The expression to be changed must appear as an operand of the
original expression or of one of its sub-expressions. The following exam-
ples illustrate how subs operates, depending on the structure of the target
expression.
> expr1:=x*y+z:
> expr2:=x*y*z:
> whattype(expr1),whattype(expr2);
+;
> op(expr1);op(expr2);
xy; z
x; y; z
Note that xy is a sub-expression of expr1 but not of expr2.
xy + z xyz
(+) ()
. & . # &
xy z x y z
Hence
> subs(x*y=joe, expr1);
joe + z
> subs(x*y=joe,expr2);
xyz
To replace xy in expr2, we must resort to a trick
> subs(x=joe/y, expr2);
joe z
We consider another example
> beta:=a*b*c+b*c+(b*c)^3;
:= abc + bc + b3 c3
118 CHAPTER 6. STRUCTURE OF EXPRESSIONS
> op(beta);
abc; bc; b3 c3
Note that the expression bc does not appear as a sub-expression of the third
operand, due to simpli cation.
> subs(b=y,beta);
ayc + yc + y 3 c3
> subs(b*c=y,beta);
abc + y + b3 c3
In order to target the substitution to a particular operand of an ex-
pression, we use the standard library function subsop, which substitutes
speci ed operands with new values
> subsop(3=three, beta);
abc + bc + three
The data type fraction is reserved for rationals. An algebraic fraction
is instead regarded as a product
> 2/3:whattype(%),op(%);
fraction; 2; 3
> a/b:whattype(%),op(%);
; a; 1b
> op(3,[%]):whattype(%),op(%);
^; b; 1
Thus, a=b is represented as a b 1 .
Exercises
Exercise 6.1. Construct the tree representation of the following algebraic
expressions
2 z
(1) 2 (2) xy (3) xyz + w
y w
(4)
pxy xz + yzw (5) q
x y
(6)
x
p z :
z+ w y+
w
6.3. FUNCTIONS ACTING ON OPERANDS OF EXPRESSIONS 119
expressions
Several standard library functions make implicit use of the operand function
op, by acting in various ways on the operands of an expression. Here
we consider seq, map, select, remove, evalf. Other functions acting on
operands (add, mul, sum, prod) will be dealt with in chapter 8.
In chapter 4, we learned that the second argument of the function seq
is either a range, which is expanded to an expression sequence, or an ex-
pression sequence itself. Such argument, however, can be virtually any
expression, which Maple turns into the expression sequence consisting of
its operands at the top level. This feature makes seq into one of the most
useful utility functions in Maple. For instance, the following function re-
turns an expression sequence consisting of the square of all operands of an
arbitrary expression
> sqall:=x->seq(z^2,z=x):
> sqall([a,b,c,d,e]);
a2 ; b2 ; c2 ; d2 ; e2
> sqall(a^2+b*c);
a4 ; b2 c2
An expression sequence can be used as the second argument of seq, pro-
vided it is enclosed in parentheses
> seq(z-1,z=(1,2*4,joe));
0; 7; joe 1
The parentheses are not needed if the expression sequence is represented
symbolically by a variable (including a ditto variable)
> es:=1,8,joe:
> seq(z-1,z=es);
120 CHAPTER 6. STRUCTURE OF EXPRESSIONS
0; 7; joe 1
In chapter 3, we have introduced the construct map(f,A) to obtain the
image of a set A under a function f. The standard library function map
allows for more general usage. Its second argument can be any expression,
not just a set: map will then act on its top-level operands, while preserving
the data type. It is instructive to compare the behaviour of the functions
map and seq when called with similar arguments
> f:=x->x^2:
> map(f,a+b+c);
a2 + b2 + c2
> seq(f(s),s=a+b+c);
a2 ; b2 ; c2
While map preserves the structure of the expression, seq transforms it into
an expression sequence.
If the rst operand f of map requires more than one argument, then
the additional arguments x2 ; x3 ; : : : must be supplied after the target ex-
pression: map(f,expr,x2,x3,..). For illustration, we consider a list L
containing numerical data. We wish to replace any element of L which is
larger than the last element by the latter. This is accomplished by the
following function
> cap:=L->map(min,L,op(nops(L),L));
> cap([1,2,3,4,5,6,7,5]);
[1; 2; 3; 4; 5; 5; 5; 5]
The last element of the list L is given by op(nops(L),L). The rst argument
of the function min runs through all the elements of L, while the second
argument is supplied as the rst optional argument of map.
Another useful utility function is select, which selects the operands
of an expression, according to some criteria. The latter is speci ed by a
Boolean function, which is applied to all operands of the target expression.
The operands giving the value true are those to be selected. The target
expression is usually a list or a set.
To compute the primes between a and b, we rst construct the list of
integers between a and b, which is [$a..b], and then select the primes in
the list with the Boolean function isprime.
> select(isprime,[$100..110]);
[101; 103; 107; 109]
The output is a list, the same data type of the second argument of select.
The syntax of select resembles that of map. Its basic form is select(f,
expr), where f is the symbolic name of a Boolean function (or an explicit
6.3. FUNCTIONS ACTING ON OPERANDS OF EXPRESSIONS 121
Example 6.2. We generate the set of the irreducible factors of the poly-
nomial x12 1
> fseq(f,f=factor(x^12-1))g;
x 1; x2 + 1; x + 1; x4 x2 + 1; x2 x + 1; x2 + x + 1
The above method would not work for the polynomial x4 + 1. Why?
Example 6.3. Let x be a real number, and let A be a set (list) of real
numbers. We construct a function of x and A whose value is the subset
(sublist) of A constituted by all the elements of A that are smaller than x.
122 CHAPTER 6. STRUCTURE OF EXPRESSIONS
d t d t d t
d t d d t d
(2,3)
(4,2)
d t d td t
d d d
d d
(2,1) d
d d t t t
x
(0,0)
d d d d d d
Figure 6.5: Visible and invisible points from the origin (0; 0) of Z2, repre-
sented by circles and solid circles, respectively. The point (2; 3) is visible,
while the point (4; 2) is not, since its view is obstructed by (2; 1).
obstructs the view of (x; y ) from the origin (think about it). This proves
the `if' bit of the theorem.
To prove the `only if' implication, we assume that x and y are coprime.
If jxj or jy j is equal to 1, then (x; y ) is clearly visible (draw a picture!),
so we shall assume that jxj; jy j > 1. We proceed by contradiction, and
assume that (x; y ) is not visible. Then there exists a point (x0 ; y 0 ) lying
between (x; y ) and the origin, which implies that xy 0 = yx0 . If x and x0
were coprime, then from the fundamental theorem of arithmetic, we would
conclude that x divides y (why?), contrary to the assumption that x and y
are coprime. So the greatest common divisor d of x and x0 is greater than
1, and we write x = ad, x0 = bd. Now, a and b are coprime, and moreover
a > 1, since 1 jx0 j < jxj. So a is also a proper divisor of y , and therefore
x and y are not coprime, a contradiction.
target expression. The invisible points are the di erence between the above
two sets
> InvisPts:=IntPts minus VisPts:
To plot (in)visible points, we must rst convert the corresponding data set
into a list.
> plot([op(VisPts)],style=POINT,scaling=`constrained`);
50
40
30
20
10
0 10 20 30 40 50
Considering horizontal and vertical lines, we see that the density of visible
points is maximal whenever the corresponding coordinate is a prime num-
ber, and minimal when it is an integer with a large number of divisors, e.g.,
30. (A more accurate formulation of this phenomenon requires knowledge
of Euler's -function.)
To count visible points, we use nops
> nops(VisPts);
1549
Since our region contains 512 = 2601 integer points, we see that approx-
imately 60% of them are visible from the origin. We shall return to the
question of the density of visible points in section 8.4, after having devel-
oped a more e ective method for counting such points.
6.3. FUNCTIONS ACTING ON OPERANDS OF EXPRESSIONS 125
Exercises
Exercise 6.4. Each expression E on the left is transformed into the ex-
pression on the right by map(f,E), for a suitable function f. Construct
such user-de ned function f in each case.
E map(f; E)
Exercise 6.5. The following function generates the list of the prime di-
visors of an integer n > 1.
> PrimeDiv:=n->[seq(expand(op(1,x)),x=ifactor(n))]:
> PrimeDiv(18800);
[2; 5; 47]
However, this function does not produce the desired answer for all values
of n. Characterize the set of integers n for which it does.
Exercise 6.6. A Maple list L contains numerical data, representing the
elements of a sequence.
(a) Plot the elements of L.
(b) Determine the number of negative elements of L.
(c) Let a1 ; : : : ; ak be the distinct elements of L, ordered arbitrarily. For
every i = 1; 2; : : : ; k , let ni be the frequency of occurrence of ai , i.e., the
number of times ai appears in L. Construct a function freq(L) which re-
turns the frequency of each distinct element of L, with the following format
[ [a1 ; n1 ]; [a2 ; n2 ]; : : : ; [ak ; nk ] ]:
Chapter 7
7.1 Polynomials
p deg(p) a0 a1 a2 a3 a4 K [x]
x4 4 0 0 0 0 1 Z[x]
3 x2 + 2x3 3 3 0 1 2 Z[x]
21=5 0 21=5 Q [x]
(x2 + 6)=3 2 2 0 1=3 Q [x]
p 2 p
5x x 2 0 1 5 R[x]
x3 i x=2 3 0 i=2 0 1 C [x]
The degree of the product of two polynomials is equal to the sum of the
degrees of the operands
deg p q = deg(p) + deg(q ):
Division
Like for integers, the ratio of two polynomials may or may not be a poly-
nomial. In K [x] | which is structurally identical to Z | division cannot
always be carried out, and one needs to introduce the corresponding notion
of divisibility.
Given two polynomials p and q in K [x], we say that p divides q if there
exists a polynomial h in K [x] such that p h = q . As for integers, we write
pjq to denote divisibility.
For instance, let p = 1 x and q = 1 + x2 . Then p divides q in Z[x],
because there exists the polynomial h = 1 x such that ph = q .
> p:=1-x:
> q:=-1+x^2:
> q/p;
1 + x2
1 x
> simplify(%);
x 1
Again, Maple refuses to carry out the simpli cation automatically, and we
have to use the function simplify for this purpose.
A greatest common divisor of two polynomials is a polynomial of highest
degree dividing both. A least common multiple of two polynomials is a
polynomial of smallest degree divisible by both. They are unique up to a
constant factor.
Example 7.3. The divisors of a polynomial come in pairs. Indeed, if p
divides q , then also q=p divides q , and vice versa. The argument is identical
to that given in chapter 2 for the integers. Thus, in the above example,
both p = 1 x and q=p = 1 x divide q . As for the integers, the number
of divisors of a polynomial p is even, except when p is a square of another
polynomial.
The analogy between polynomials and integers can be carried out fur-
ther.
Theorem 7 If f and g are polynomials over Q , R, or C , and g is nonzero,
then one can nd unique polynomials q and r such that
f = gq +r (7.3)
where r has smaller degree than g .
132 CHAPTER 7. POLYNOMIALS AND RATIONAL FUNCTIONS
The polynomials q and r are the quotient and the remainder, respec-
tively, on dividing f by g . (Compare with the analogous construction
given in equation (2.10).) Note that the above theorem does not apply to
polynomials in Z[x]. To see why, let us consider the case f (x) = x2 1 and
g(x) = 2x 2. Then g divides f in Q [x], but not in Z[x]
1 1
x 2
1 = (2x 2) x+ :
2 2
In Z[x], the equation (7.3) gives q (x) = 0 and r(x) = g (x), with the de-
gree of r(x) equal to that of g (x). It turns out that Theorem 7 can be
extended to Z[x], provided that we consider only monic polynomials, that
is, polynomials with leading coeÆcient equal to 1.
Quotient and remainder may be computed using the long division algo-
rithm from elementary algebra. The working for the case f = x5 1 and
g = x2 x 1 is displayed below
x5 1 x2 x 1
x5 x4 x3
x4 + x3 x3 + x2 + 2 x + 3
x4 x3 x2
2 x3 + x2
2 x3 2 x2 2 x
3 x2 2 x 1
3 x2 3 x 3
5x+2
DIVISIBILITY FUNCTIONS
FOR INTEGERS AND POLYNOMIALS OVER Q
integers polynomials
iquo(a,b) quo(p,q,x)
irem(a,b) rem(p,q,x)
igcd(a,b) gcd(p,q)
ilcm(a,b) lcm(p,q)
ifactor(a) factor(p)
Sequences of polynomials
A sequence g0 ; g1 ; g2 ; : : : of polynomials in K [x] is a function that associates
to every natural number t an element of K [x]
g:N ! K [x] g := t 7! gt :
Note that in this context g is a function of t, not a function of x. For
instance, if we let
g:N ! Z[x] g := t 7! x2 tx + t 1
we obtain a sequence of polynomials over Z, all of degree 2
x2 1; x2 x; x2 2x + 1; : : :
> g:=t->x^2-t*x+t-1:
> seq(g(t),t=0..5);
x2 1; x2 x; x2 2 x + 1; x2 3 x + 2; x2 4 x + 3; x2 5x +4
In the above example the variable x is not passed to the function g as an
argument. We say that x is a global variable, meaning that its value can be
modi ed from outside the function.
> x:=-1:
> seq(g(t),t=0..5);
0; 2; 4; 6; 8; 10
Global variables can lead to unpredictable results and should be used spar-
ingly.
Sequences of polynomials can be de ned recursively, in a manner anal-
ogous to sequences of numbers. In the simplest instances, a recursive se-
quence is de ned by the initial condition g0 , which is a given polynomial
f 2 K [x], and by a rule that de nes the element gt+1 of the sequence as
7.2. POLYNOMIAL ARITHMETIC 135
Exercises
Exercise 7.1. Prove the formula (7.2) for the coeÆcients of the product
of two polynomials.
Exercise 7.2. Prove that the degree of the product of two polynomials
is equal to the sum of the degrees of the operands.
Exercise 7.3. Show that x + x + x + x + 1 and x + x + 1 are relatively
4 3 2 2
prime.
Exercise 7.4. List all common divisors of x6 x5 2x4 + x3 + x2 +2x 2
and x x x + 1 in Z[x].
5 3 2
The lack of closure of the integers under division led to the introduction of
the rational numbers. With polynomials there is a similar need of enlarging
the set K [x] in order to achieve closure with respect to division. Thus, the
ratio (x2 1)=(x 1) = x + 1 is a polynomial, because x 1 divides x2 1
> (x^2-1)/(x-1):%=simplify(%);
x2 1
= x+1
x 1
However, the ratio (x2 1)=(x 2) is not a polynomial, since x 2 does
not divide x2 1.
> simplify((x^2-1)/(x-2));
x2 1
x 2
In this case we speak of a rational function.
The rational functions over K in the indeterminate x | denoted by
K (x) | is the set of fractions whose numerator and denominator are ele-
ments of K [x], and the denominator is nonzero. This construction is analo-
gous to the construction of Q from Z. In particular, we have K [x] K (x),
since every polynomial over K is also a rational function with denominator
1 = 1 x0 , and the latter is itself a polynomial over K . (This is due to the
fact that every eld K contains 1; see section 12.3.) So we have the chain
of inclusions
Q (x) R(x) C (x):
Note that we have not mentioned the set Z(x), since the latter is equal to
Q (x). (Why?)
Recall that Maple simpli es rational numbers in such a way that the
denominator is positive, and the numerator and denominator are relatively
prime. Maple does not simplify rational functions, unless the simpli cation
is obvious, that is, unless the quantity to be simpli ed is already factored.
The intrinsic function simplify represents a rational function over Q so
that numerator and denominator are relatively prime and their coeÆcients
are integer (so does the function normal).
> p:=(x-1)*(x-2)*(x-3):
> q:=(x-1)*(x+1):
> p/q;
(x 2)(x 3)
x+1
> p/expand(q);
(x 1)(x 2)(x 3)
x2 1
> simplify(%);
7.4. BASIC MANIPULATIONS 139
(x 2)(x 3)
x+1
Simpli cation is not always desirable. For instance, the rational function
x1000000 1
x 1
= x999999 + x999998 + +x +1
which is de ned by two polynomials with four nonzero coeÆcients in all,
`simpli es' to a polynomial with one million nonzero coeÆcients!
1
x+
1
x2 +
1
x3 +
x4
> simplify(%),factor(%);
x10 + x3 + x5 + x7 + 1 x10 + x3 + x5 + x7 + 1
; 2 2
x (x + 1 + x )
2 7 2 x (x + x + 1) (x5 x4 + x2 x + 1)
Here simplify has produced a partially factored denominator. By contrast,
the function factor, when applied to a rational expression, provides a
simpli ed and fully factored form, and may be used as an alternative to
simplify, keeping in mind that it is more expensive to evaluate. For
simpli cation of rational expressions, see also ?normal.
A trick of the trade
We consider the problem of proving the following identity with Maple
1 (1 + x)3 + 1
(1 + x)2 + = :
1+x 1+x
> rf:=(1+x)^2+1/(1+x):
The procedure simplify does not do what we want
> simplify(rf);
2 + 3x + 3x2 + x3
1+x
and factorization does not help, either
> factor(%);
(x + 2)(x2 + x + 1)
1+x
The idea is to perform a substitution in order to `freeze' the subexpression
1 + x.
> subs(1+x=y,rf);
1
y2 +
y
> simplify(%);
y3 + 1
y
> subs(y=1+x,%);
(1 + x)3 + 1
1+x
142 CHAPTER 7. POLYNOMIALS AND RATIONAL FUNCTIONS
7.5 Partial fractions decomposition
The decomposition into partial fractions is an important representation of
a rational function. We consider an example.
> (x+1)/(x^4-2*x^3+x^2-2*x);
x+1
(7.4)
x4
2x3 + x2 2x
The conversion to partial fractions is done by the many-purpose intrinsic
function convert (check the online documentation)
> convert(%,'parfrac',x);
11 3 1 1 3+x
+ +
2 x 10 x 2 5 x2 + 1
How does this decomposition work? The denominator of the rational func-
tion (7.4) factors into the product of three irreducible polynomials
x+1 x+1
= :
x4 2x3 + x2 2x x(x 2)(x2 + 1)
The right-hand side is then converted to the form
q1 q q
+ 2 + 3
x x 2 x2 + 1
where
1 3 3 x
q1 = q2 = q3 = + :
2 10 5 5
The three polynomials qk are polynomials over Q with degree lower than
that of the corresponding denominator. The following theorem describes
the general situation.
Theorem 9 Let f=g be a rational function over Q , whose numerator is of
lower degree than the denominator. Let
g = pe11 pe22 perr
be the factorization of the denominator into irreducibles. Then there exist
polynomials q1 ; : : : qr , with deg(qk ) < deg(pk ), for k = 1; : : : ; r, such that
f q q
g
= t11 + t22 +
p1 p2
+ pqtrr
r
and tk ek ; k = 1; : : : ; r.
A common use of the decomposition into partial fractions is to integrate
rational functions.
7.5. PARTIAL FRACTIONS DECOMPOSITION 143
Exercises
Exercise 7.10. Consider the polynomial (2x3 x)(x2 x 1). Using
Maple, transform this polynomial into:
(a) 2 x5 2 x4 3 x3 + x2 + x
(b) x (2x2 1)(x2 x 1)
(c) x3 (2 x2 1) x2 (2 x2 1) x (2 x2 1)
(d) 2 x3 x2 x 1 x x2 x 1
(e) x 2 x4 2 x3 3 x2 + x + 1 :
(a) (x 1)2 (x + 2)
x3
2 x2 x + 2
(b) x3 3x+2
(x + 1) (x 1) (x 2)
(d) x2 + x 2
x2 x 2
(e) (x + 2) (x 1)
(x + 1) (x 2)
(f ) x2 x 2
(x + 1) (x 2) + (x + 1) (x 2) (x + 1) (x 2) :
Exercise 7.12.
Exercise 7.13.
Exercise 7.15. Let h(x) = f (x)=g (x) be a rational function and let r(x)
be the remainder of division of f (x) by g (x). We de ne
fh(x)g = gr((xx)) :
Consider the function
8n o
< 1
h(x) h(x) 6= 0
(h(x)) =
:
0 otherwise.
7.5. PARTIAL FRACTIONS DECOMPOSITION 145
Show that the elements of the following recursive sequence of rational func-
tions over Q
x3 + x + 1
h0 (x) = ht+1 (x) = (ht (x)); t0
x5 + x + 1
becomes eventually zero.
Chapter 8
8.1 Basics
Sn = a0 + a1 + a2 + +a n = ak n 0; (8.2)
k=0
Y
n
Pn = a0 a1 a2 a n = ak n 0: (8.3)
k=0
It should be clear that, for any given sequence ak , the quantities Sn and
Pn are functions of n, and hence they are new sequences
n 0 1 2 3
an a0 a1 a2 a3
Sn a0 a0 + a1 a0 + a1 + a2 a0 + a1 + a2 + a3
Pn a0 a0 a1 a0 a1 a2 a0 a1 a2 a3
We begin with some examples.
147
148 CHAPTER 8. FINITE SUMS AND PRODUCTS
Example 8.1. A = Z, an = 1, n 0.
n 0 1 2 3 4 5
an 1 1 1 1 1 1
Sn 1 2 3 4 5 6
Pn 1 1 1 1 1 1
We have Sn = n + 1, and Pn = 1.
Example 8.2. A = Z, an = n, n 1.
n 1 2 3 4 5
an 1 2 3 4 5
Sn 1 3 6 10 15
Pn 1 2 6 24 120
We have
n(n + 1)
Sn = Pn = n! : (8.4)
2
The leftmost formula is the sum of the arithmetic progression.
n 0 1 2 3 4 5
an 1 1 1 1 1 1
Sn 1 0 1 0 1 0
Pn 1 1 1 1 1 1
We have
1 + ( 1)n
Sn = Pn = ( 1)b(n+1)=2c
2
where b c is the oor function.
n 0 1 2 3 :::
an 1 x x2 x3
Sn 1 1+x 1 + x + x2 1 + x + x2 + x3
Pn 1 x x3 x6
8.2. SUMS AND PRODUCTS WITH MAPLE 149
We have
xn+1 1
Sn = Pn = x n(n+1)=2 : (8.5)
x 1
In the leftmost expression we can substitute for x any numerical value
x 6= 1, to obtain the formula for the sum of the geometric progression.
Example 8.5. Summing characteristic functions. Let A be a subset
of N , and let A be the characteristic function of A in N (cf. (3.14)). We
wish to count the elements of A that are not greater than a given natural
number n. Let this number be A(n). Because A (k ) is 0 if k 62 A, and 1 if
k 2 A, we have
X
n
A(n) = A (k ):
k=0
For instance, if A is the set of primes, then A(n) is the number of primes
not exceeding n.
25502500
Alternatively, the expression for the summands could have been stored in
a variable, or used directly as the rst argument of the function add
> a:=k^3:
> add(a,k=1..100),add(k^3, k=1..100);
25502500; 25502500
We shall, however, favour the explicit representation of the summands as
values of a function, because this construct helps clarify the structure of
the summation process.
Example 8.7. Compute the product of the rst 20 prime numbers. The
elements of the sequence of primes are available through the standard li-
brary function ithprime
> mul(ithprime(s),s=1..20);
557940830126698960967415390
Example 8.8. Verify that 33 factorial is actually the product of the rst
33 positive integers
> evalb(mul(things,things=1..33)=33!);
true
In this example, the relevant sequence is just the identity sequence ak = k ,
for which we have not constructed a user-de ned function. The following
(rather pedantic) alternative solution underlines this point
> id:=x->x:
> mul(id(i),i=1..33)-33!;
0
Example 8.9. We construct the function 1 (n), which gives the number
of primes of the form 4k + 1 which are not greater than n. (This is a
specialization of the prime counting function introduced in section 2.6.)
We rst construct the characteristic function of the set of such primes, and
then sum.
> chi:=k->if irem(k,4)=1 and isprime(k) then 1 else 0 fi:
> pi1:=n->add(chi(k),k=1..n):
> pi1(100);
11
Example 8.10. Consider the following sequence of elements of Z[x]
X
n
Sn = xk kx + 1 n 1:
k=1
8.2. SUMS AND PRODUCTS WITH MAPLE 151
5 + x2 14 x + x3 + x4 + x5
; 5
To achieve a partial factorization, we must assemble and multiply together
the relevant operands
> op(3,p)*expand(mul(op(k,p),k=[1,2,4,5]));
(x2 + x + 1) (x3 + x9 + x6 + 1)
The same construct is also possible with integers
> ifactor(30!);
(2)26 (3)14 (5)7 (7)4 (11)2 (13)2 (17) (19) (23) (29)
> op(1,")*op(nops("),")*expand(mul(op(k,"),k=2..nops(")-1));
products
Such formulae allow us to compute the above sums for any given value of
n, with much less computational work than carrying out the sum explicitly
10 4
X 104 (104 + 1) 103 (103 + 1)
k = = 49505500: (8.7)
k=103
2 2
Note the structural analogy with the problem of constructing inde nite
integrals Z Z x
1
x dx = y dy = x2 :
0 2
Maple provides the standard library functions sum and product which
are designed to perform symbolic (i.e., inde nite) summations and prod-
ucts, rather than to explicitly add or multiply quantities. Their syntax
is the same as that of add and mul, except that the range of summation
(product) may now be symbolic, not just numerical. The function sum
will attempt to `discover' a formula for inde nite summation every time it
is called with a non-numerical range (the reader should be aware that this
process is time-consuming). If the range is numerical, sum and product will
give the same result as add and mul, respectively, except a lot more slowly.
So the use of these functions for de nite summations is to be avoided.
For instance, sum can nd the formula for the inde nite sum of the
arithmetic and geometric progressions (8.6)
> AS:=sum(k,k=1..n);
1 1 1
AS := (n + 1)2 n
2 2 2
> factor(%);
1
n (n + 1)
2
The function factor is legitimate here, because the value of the sum is a
polynomial in Q [n]. Now that we have a formula, we can compute the sum
(8.7) with substitutions
8.3. SYMBOLIC EVALUATION OF SUMS AND PRODUCTS 153
> subs(n=10^4,AS)-subs(n=10^3,AS);
49505500
The geometric sum is treated similarly
> GS:=sum(x^k,k=1..n);
x(n+1) 1
GS :=
x 1 x 1
> simplify(%);
x(n+1) 1
x 1
The reader is invited to compare the amount of computation involved
in evaluating the arithmetic sum for a large value of n, using add, sum with
a numeric argument, and substitution into the symbolic expression AS.
Let us consider another example. The terms of the sum
X
n
k2
Sn =
k=1
k
binomial(k 2 ; k )
k=1
Maple cannot nd such a formula. This, of course, does not mean that
there isn't one, but only that the function sum cannot nd it. Substituting
an integer value for n in the expression S will cause the de nite summation
to be performed explicitly
> subs(n=4,S);
1911
However, since the evaluation of the inde nite sum failed in this case, the
use of add would be preferable.
The functions sum and product have the inert form Sum and Product,
respectively, which leaves the operation indicated. The function value will
turn the value of the inert form into the actual value.
> Sum(cube(k),k=1..100);
100
X
k3
k=1
154 CHAPTER 8. FINITE SUMS AND PRODUCTS
(For further applications of these inert functions, see the next section.)
The dependence of sj;n on j not only derives from aj;k , but it may also
result from the j -dependence of the range n0 : : : n. In other words, we may
have n0 = n0 (j ) and n = n(j )
2
X
m
X
j
aj;k :
j =m0 k=j
The simplest case is when the range n0 : : : n of the inner sum is inde-
pendent from the summation index of the outer sum. This is illustrated in
the following example
P3 P2 P P
3 2
S = j =1 k=1
aj;k = j =1 k=1
aj;k
P3
= j =1
(aj;1 + aj;2 )
= a1;1 + a1;2 + a2;1 + a2;2 + a3;1 + a3;2 :
The sum S can be written as
3
X 2
X
S = Sj Sj = aj;k = aj;1 + aj;2 ;
j =1 k=1
8.4. DOUBLE SUMS AND PRODUCTS 155
showing that S is the sum of the rst three elements of the sequence Sj ,
which are themselves sums. Because the upper index of the inner sum
does not depend on the summation index of the outer sum, the order of
summation can be reversed
P3 P2 P
2 3P
j =1 k=1
aj;k = aj;k
Pk2 =1 j =1
= k=1
( a 1;k + a2;k + a3;k )
= a1;1 + a2;1 + a3;1 + a1;2 + a2;2 + a3;2 :
(j + k ) 2
= 1339800
j =1 k=1
Combining the active function product (or, better still, mul) with the inert
function Sum, the product is evaluated but the sum is not, giving
> product(s(j),j=1..4);
1
! 2
! 3
! 4
!
X 2
X 2
X 2
X 2
(1 + k ) (2 + k ) (3 + k ) (4 + k )
k=1 k=1 k=1 k=1
The following statements extract and evaluate the third operand in the
above expression
> op(3,%):%=value(%);
3
X
(3 + k )2 = 77
k=1
Example 8.12. We wish to count the visible points (see gure 6.5, section
6.3), that lie within a square region on the plane, centered at the origin.
This will involve performing a double summation of their characteristic
function. Because visible points are characterized by the fact that their
coordinates are coprime (theorem 6 of section 6.3), we deduce that the
pattern of visible points in the plane must posses an 8-fold symmetry, since
for any integers x and y we have gcd(x; y ) = gcd(y; x). We denote
by V p(n) the total number of visible points lying within the square n
x; y; n, where n is a positive integer. One sees that V p(1) = 8. For
n 2, we must add to this gure eight times the number of the visible
points contained within the triangular region de ned by the inequalities
2xn yx 1: (8.9)
Such inequalities determine the range of double summation of the charac-
teristic function chi(x,y).
> chi:=(x,y)->if igcd(x,y)=1 then 1 else 0 fi:
> Vp:=n->8*(1+add(add(chi(x,y),y=1..n-1),x=2..n)):
> Vp(1),Vp(10),Vp(100),Vp(1000);
8; 256; 24352; 2433536
8.4. DOUBLE SUMS AND PRODUCTS 157
For n = 1, the function Vp gives the correct answer 8, since in this case,
the outer function add has an empty range of summation: (x=2..1).
What is the probability that an integer point chosen at random is vis-
ible? From Theorem 6, this is the same as the chance that two randomly
chosen integers are coprime, and a theorem in number theory states that
the latter probability is equal to 6= 2 . We estimate it by dividing V p(n)
by the total number of integer points in the corresponding box, which is
equal to (2n)2 . For this purpose, we use the largest value computed above
(n = 1000), corresponding to a sample of 4 million points
> evalf(2433536/(2*1000)^2),evalf(6/Pi^2);
:6083840000; :6079271016
Our result is accurate to within three decimal gures.
Exercises
Exercise 8.1. Compute the value of the following expressions (pk denotes
the k th prime number).
100
X 4
X k 2 10
(a) k (k + 1) (b)
k=0 k=0
k +k+1
2
10
Y
1 50
X
1 50
(c) 1 (d) ( 1)k
k=1
pk k=0
k
5
X 5
X
xn x2n 1
(e) (f ) ( 1)n+1
n=0
n! n=1
(2n 1)!
0 1
10 5 10
X k
20 k Y X
(g ) 2
(h) @ (k + j )A
k=0
2 10 k=1 j =1
5 k +1 5 X
X X k2 j X n
2k+1 n
(i) (j ) :
k=0 j =1
k+j n=1 k=0
k+1 k
Exercise 8.2. Using Maple, prove that
0 10 12
4
X 5
X
0 1k 2
@ (9 + j )A @ (16 + j )A
4
Y k+1
X j =1 j =1
@ (k 2 + j )A = 2
:
j =1
X
k=1
(1 + j )
j =1
158 CHAPTER 8. FINITE SUMS AND PRODUCTS
X
n
Y
m
1 1 m+1
(c) k 2 = (n
k
1) 2 n+1
+2 (d) 1 =
k=1 n=2
n2 2 m
!2
X
n
X
n
X
n
n
(e) k =
3
k (f ) k2 = 2n 2 n (n + 1):
k=1 k=1 k=1
k
Exercise 8.4. Express each of the following sums (products) by means
of the summation (product) symbol. Then, using Maple, nd and prove
formulae for their values. Express the answer in the simplest possible form.
1 1 1 1
(b) + +
1 4 4 7 7 10
+ +
(3n 2) (3n + 1)
4 4 4 4
(c)
1
1
1
9
1
25
1
(2n 1)2
:
ak = F (n) n n0 (8.10)
k=n0
where
Pn n0 is an integer and F is a given function of n. We de ne S (n) =
k=n0
ak and rewrite equation (8.10) as
S (n) = F (n) n n0 : (8.11)
To prove (8.11) you must proceed as follows:
Construct Maple functions for the inde nite summation S (n) and its
conjectured value F (n).
Using these functions, verify that S (n0 ) = F (n0 ), thereby establishing
(8.11) for n = n0 .
8.4. DOUBLE SUMS AND PRODUCTS 159
Verify with Maple that F (n)+ an+1 = F (n +1), proving that equation
(8.11) holds for n + 1.
n
Y1
n
1 x2
n0
k
(b) (1 + x2 ) =
k=0
1 x
X
n
k2 + k 1 1 n+1
(c) = n 1:
k=1
(k + 2)! 2 (n + 2)!
Exercise 8.8 .
Let n be an integer. Construct a function SumDigits(n)
whose value is the sum of the decimal digits of n.
Sn = ak = ak + an = Sn 1 + an (8.12)
k=n0 k=n0
Y
n n
Y1
Pn = ak = ak an = Pn 1 a n : (8.13)
k=n0 k=n0
These are recursive formulae, valid for n > n0 . The leftmost summation
and product are valid also for n = n0 , and substituting this value we obtain
the initial conditions
P
Sn0 = Q nk=0 n0 ak = an0
Pn0 = nk=0 n0 ak = an0 :
The initial conditions can be made independent from the sequence fak g as
follows
Sn0 = Sn0 1 + an0 =) Sn0 1 = 0
Pn0 = Pn0 1 a n0 =) Pn0 1 = 1;
extending the range of validity of the recursion formulae (8.12) and (8.13)
to the case n = n0
Sn0 1 = 0; Sn = Sn 1 + an ; n n0
Pn0 1 = 1; Pn = Pn 1 a n ; n n0 :
The last formulae yield the following recursive algorithm for computing
sums and products
S 0 P 1 (8.14)
S S + an0 P P an0
S S + an0 +1 P P an0 +1
S S + an0 +2 P P an0 +2
.. ..
. .
8.5. SUMS AND PRODUCTS AS RECURSIVE SEQUENCES 161
Exercises
Exercise 8.9. Consider the following recursive sequence in Q (x)
x
g0 = x gt+1 = ; t 0:
gt + 1
Show that
3
Y x4
gt = :
t=0
x2 x 1
162 CHAPTER 8. FINITE SUMS AND PRODUCTS
Elements of programming
In this chapter, we introduce three universal programming tools: iteration
(the do-structure), conditional execution (the if-structure), and procedures
(user-de ned functions). Only the rst one is new to us. We have encoun-
tered simple instances of the if structure in the context of characteristic
functions, and we have used extensively the arrow operator -> to construct
user-de ned functions consisting of a single statement.
9.1 Iteration
> a:=1:
> to 3 do
> a:=f(a)
> od:
> a;
39135394
The same result may be achieved with the following simpler construction,
which makes use of ditto variables
> 1:
> to 3 do
> f(%);
> od:
> %;
39135394
Next we consider the problem of constructing Pascal's triangle. Recall
that the nth row of the triangle is given by the nite sequence
n n n
0 ; 1 ; ::: ; n
which is best dealt with by a user-de ned function
> row:=n->seq(binomial(n,k),k=0..n):
The following command generates the rst eight rows of Pascal's triangle
(n ranging from 0 to 7)
> row(0);row(1);row(2);row(3);row(4);row(5);row(6);row(7);
This time the statements to be repeated are not identical, but they depend
on the index n increasing in unit steps from 0 to the prescribed maximal
value. The do-structure can be made to assume the control of the integer
variable n, as follows
> for n from 0 to 7 do
> row(n)
> od;
1
1; 1
1; 2; 1
1; 3; 3; 1
1; 4; 6; 4; 1
1; 5; 10; 10; 5; 1
1; 6; 15; 20; 15; 6; 1
1; 7; 21; 35; 35; 21; 7; 1
9.1. ITERATION 165
The value of n is initialized to 0, and then increased by 1 each time. For
every value of n the statement row(n) is executed, until n exceeds the
target value of 7.
Syntax
The syntax of the do-structure is the following:
> for n from start by step to nish do
> statement;
>
> statement
> od;
The loop control variable n is initialized at start and then increased by
step until its value exceeds that of nish (or until it becomes smaller than
nish, if step is negative). The expressions start, step, and finish must
have integer, rational, or oating-point value.
The body of the do-structure consists of an arbitrary number of state-
ments, each statement in the block being executed in correspondence of
each value assumed by n. The loop control variable can be used within
the structure as any other variable, but its value cannot be changed inside
the loop. The last statement in the body of the structure does not require
a terminator. To make the do-structure visible, it is good programming
practice to indent the statements in the body of the loop, inserting a few
blank characters.
The output of the statements is displayed if od is followed by a semicolon,
and suppressed if od is followed by a colon, regardless of which terminator
follows the individual statements.
The from and by options may be omitted, in which case the value of both
start and step defaults to 1. If the loop control variable is not needed, the
for option may be omitted, in which case Maple de nes its own loop control
variable. If the to option is omitted, the loop is in nite, and presumably
it will be terminated by other means.
In the following examples, observe carefully the values assumed by the
loop control variable on exit. Also note that the whole loop is logically
equivalent to a single command.
> for i from 2 by 2 to 7 do i od:
2
4
6
> i;
8
> for i from 2 by -3 to -1 do i od;
2
166 CHAPTER 9. ELEMENTS OF PROGRAMMING
1
> i;
4
If the range is empty, the loop is not executed, and the value of the loop
control variable is not modi ed
> for i from 2 by -1 to 3 do i od;
> i;
2
To display the output of selected statements in a loop (rather than of all
or no statements, as determined by the terminator of od), Maple provides
the standard library function print, which always displays its value within
the body of a do-structure, in the form of an expression sequence.
> x:=1:
> for n to 3 do
> x:=x+igcd(n,x):
> print(n,%)
> od:
1; 2
2; 4
3; 5
The colon after od suppresses all output, except that of the print state-
ment, which is the only output to be shown.
Example 9.1. An instructive comparison between two similar construc-
tions using do and seq is as follows
> 2:
> for i to 3 do
> %*i
> od;
2
4
12
> 2:
> seq(%*i,i=1..3);
2; 4; 6
Make sure you understand how and why these two results di er.
9.1. ITERATION 167
Summing the elements of a recursive sequence. Compute
the sum P5t=0 xt , with xt given by (9.0), using the recursive summation
Example 9.2.
algorithm (8.14).
> x:=0:
> s:=x:
> to 5 do
> x:=f(x):
> s:=s+x:
> od:
> s;
711
Example 9.3. Constructing a nested expression. Compute the value of
the following expression
12 + 1
32 + 1
52 + 1
72 + 912
> 9^2:
> for i from 7 by -2 to 1 do
> i^2+1/%
> od:
> %;
997280
897949
Example 9.4. Plotting a recursive sequence. We wish to plot the ele-
ments of the rational sequence
x0 = 1=4 xt+1 = 1 2x2t ; t 0: (9.1)
As usual, we rewrite the recursion formula as xt+1 = f (xt ), with f (x) =
1 2x2 . In the recursive computation of xt , the index t plays no role, and
the successive values of xt are to be stored in the single variable x. The
plotting routine requires a list L = [[1; x1 ]; [2; x2 ]; : : :], containing the points
to be plotted, vs. the corresponding index. Such list L will be represented
as an expression sequence of two-element lists, which we initialize to the
NULL value. At the end of the computation, L is then turned into a list
for plotting. The sequence (9.1) is rational, since x0 is rational, and the
recursion involves only rational operations. However, an inspection of the
168 CHAPTER 9. ELEMENTS OF PROGRAMMING
0.5
0 20 40 60 80 100
-0.5
-1
Note the very irregular behaviour of this sequence, which is chaotic, using
the jargon of the theory of dynamical systems [6].
9.1. ITERATION 169
The in option
Recall that functions such as seq, add, mul, etc., accept any expression as
range, not just a sequence of integers.
> ifactor(3960);
(2)3 (3)2 (5) (11)
> [seq(expand(k),k=%)];
[8; 9; 5; 11]
A similar construction is available in a loop, using the in option
> for k in ifactor(3960) do
> expand(k)
> od;
8
9
5
11
> L:=NULL:
> for k in ifactor(3960) do L:=[L,expand(k)] od:
> L;
[[[[8]; 9]; 5]; 11]
The while option
The while option a ords an alternative way to terminate an iteration. Its
syntax is the following
> while logical expression do
> statements
> od;
The logical expression in the heading is evaluated. If its value is false, the
loop is exited. If it is true, the body of the loop is executed, after which
the logical expression is evaluated again, etc. Evaluation does not require
the use of evalb.
> x:=2:
> while x < 100 do
> x:=x^2;
> od;
x := 4
x := 16
x := 256
The while option is the most general one, in the sense that the e ect of
any other combination of options can be reproduced using the while option
170 CHAPTER 9. ELEMENTS OF PROGRAMMING
alone. The while option can be used in conjunction with any other option.
For instance, let start, step, and finish have integer values, and let step
be positive. The structure
> for k from start by step to finish do
> statements
> od:
can be reproduced as follows
> k:=start:
> while k <= finish do
> statements;
> k:=k+step
> od:
If step is negative, then the inequality tested by the while option should
be reversed.
Example 9.5. Compute the number of primes less than 100.
> for n while ithprime(n) < 100 do od:
> n-1;
25
(Why is the required output n 1 and not n?)
Example 9.6. Use the while option to compute Ob , the smallest positive
integer n for which n! is greater than bn . (This function was considered in
an exercise of chapter 4.) Here we compute the value of O5 .
> H:=(n,b)->n!<=b^n:
> for n while H(n,5) do od;
> n;
12
Note that the body of the loop is empty. All the work is done by the
loop control functions. By nesting the above loop inside another loop that
controls the value of b, we can compute several elements of the sequence
Ob . These are the rst 10 of them
> for b to 10 do
> for n while H(n,b) do od:
> print(b,n)
> od:
1; 2
2; 4
3; 7
4; 9
5; 12
6; 14
9.1. ITERATION 171
7; 17
8; 20
9; 22
10; 25
Exercises
Exercise 9.1. In the following exercises you must use the do-structure.
(a) Display the square of the rst 10 prime numbers. Hence do the same
using the function seq.
(b) Compute the sum of the rst 10 prime numbers. (When you are sure
that your code is working, suppress all intermediate output within the loop.)
Hence check your result with the function add.
(c) Compute the product of the rst 10 prime numbers. Hence check your
result with the function mul.
(d) Let
p(x) = x (3 + x (6 + x (9 + x (12 + x (15 + x))))):
Show that p(x)=x is irreducible.
(e) Show that the element x20 of the following sequence of integers
x0 = 3 x1 = 5 xt+1 = f (xt ; xt 1 ) = 3 xt 2 xt 1 ; t1
is equal to 221 + 1.
Exercise 9.2. Consider the following recursive sequence of rationals
x 2
x0 = 0 xt+1 = f (xt ) = t ; t 0: (9.2)
xt + 2
(a) Compute the element x10 , without displaying any intermediate output.
(b) Compute the rst 10 elements of the sequence (9.2), displaying the
value of t and xt at each step.
(c) Compute the sum of the rst 10 elements of the sequence (9.2).
(d) Plot the elements of the sequence (9.2) for t in the range 30; : : : ; 100,
connecting points with segments.
Exercise 9.3. In the following exercises you must use the while option
of the do-structure.
(a) Display the positive cubes smaller than 500.
(b) Compute the smallest integer greater than 1 which is relatively prime
to 9699690.
(c) Compute the smallest integer n > 1 for which n2 1 is divisible by 87.
(d) Compute the smallest positive integer n for which n2 + n + 41 is not
prime.
172 CHAPTER 9. ELEMENTS OF PROGRAMMING
(e) Compute the smallest positive n which is a multiple of 3, and for which
2n > n4 .
(f ) Consider the following recursive sequence of complex numbers
z0 = 2 i
4
zt+1 = zt2 + i; t 0:
3
Compute the smallest value of t for which jzt j > 105.
Exercise 9.4. We consider the number qn of composite integers following
the nth prime pn . Thus, q4 = 3, because the fourth prime p4 = 7 is followed
by the 3 composite integers 8; 9; 10. For n > 1, qn is always odd (make sure
you believe this).
(a) Compute the smallest prime pn which is followed by at least 9 composite
integers. Display also the corresponding value of n.
(b) Let at be the smallest value of n for which qn t. Thus, pa is the
smallest prime which is followed by at least t composite integers. (Think
t
about it. Read the de nition again. In the previous problem you have
computed a9 and pa9 .) Compute and display t; at , and pat , for all odd t
smaller than 20 (excluding t = 1).
Exercise 9.5. Exponentials grow faster than any power. In this prob-
lem, we compute the integer n > 1 at which the exponential sequence 2n
overtakes the power ne , where e is a given positive integer. We call this
number e . Compute the rst 10 elements of the sequence
1; 2 ; 3 ; : : :
Exercise 9.6 .
Compute the largest integer less than 1000 that can be
expressed as a sum of two squares (e.g., 5 = 12 + 22 ). No intermediate
output should be displayed.
Exercise 9.7 .
Consider the set of natural numbers, the sum of whose
decimal digits is divisible by 7
f0; 7; 16; 25; 34; 43; 52; 61; 70; 106 : : :g:
Compute the number of elements of this set which are smaller than 1000.
Exercise 9.8 .
We de ne an in nite recursive sequence S = s1 ; s2 ; : : : of
elements of the set f1; 2g. We rst let S = 1; 2; 1, and then we apply
repeatedly the substitution: S 7! S; 1; S; 2; S; 1; S . After t substitutions,
S will have 4t+1 1 elements. We then de ne the sequence
X
n
0 = 0 n = sk ; n 1:
k=1
9.2. STUDY OF AN EVENTUALLY PERIODIC SEQUENCE 173
Plot the rst 44 elements of the sequence 0; z0 ; z1; z2 ; : : :, where zn are
complex numbers de ned as
X
n
zn = e i k =3 :
k=0
We now apply various techniques developed so far to the study of the struc-
ture of a family of eventually periodic sequences of integers, de ned recur-
sively.
Take a four-digit integer x0 , whose digits are not all the same: (x0 =
1066). Rearrange the digits to make the largest possible number (6610) and
the smallest (0166). Subtract the smallest from the largest (6610 0166 =
6444 = x1 ). It can be shown (see exercises) that the result is a 4-digit
integer whose digits are not all the same. By repeating the above procedure,
we obtain a recursive sequence of four-digit integers: xt+1 = F (xt ), t 0.
The same construction can be carried out in the case of n-digit integers,
for arbitrary n > 1
xt+1 = Fn (xt ) t 0; n > 1: (9.3)
First some examples:
n=2: 82; 54; 9; 81; 63; 27 45; 9; 81; : : :
n=4: 1066; 6444; 1998; 8082; 8532; 6174; 6174; : : :
These sequences are eventually periodic, with period 5 and 1, respectively,
and transient length 2 and 7, respectively. Eventual periodicity is easy
to explain: the domain of Fn is nite, and therefore we must eventually
174 CHAPTER 9. ELEMENTS OF PROGRAMMING
encounter an integer that has appeared before. From that point on, the
sequence will repeat.
We wish to explore the case n = 4 in full, producing a computer-assisted
proof that all 4-digit sequences eventually reach 6174.
We need a user-de ned function for Fn . Decimal digits are to be
stored backwards in a 4-element list: 3861 <-> [1,6,8,3], and we must
be able to switch easily from an integer to the corresponding list, and vice
versa. For this purpose we develop two conversion functions. The function
int2dig(x,n) produces the list of the rst n digits of the integer x, begin-
ning with the least signi cant one. If n is greater than the number of digits
of x, the function will ll the missing spaces with zeros. This function is a
variant of the function dgt developed in section 5.1 to compute the digits
of a rational number.
> int2dig:=(x,n)->[seq(irem(iquo(x,10^(i-1)),10),i=1..n)]:
Its inverse of int2dig is dig2int(L), where L is a list of an arbitrary
number of non-negative integers
> dig2int:=L->add(op(i,L)*10^(i-1),i=1..nops(L)):
> od:
> #------------------------- store first element of cycle
> cycle:=cycle union fxg
> od:
> cycle;
f6174g
We have shown that all orbits eventually reach the cycle beginning with
6174, which we know to have period 1. In the parlance of the theory of
dynamical systems, we shall say that f6174g is a global attractor for the
system (9.3).
Exercises
Exercise 9.9. Let S be the set of all nite non-empty subsets of Z. Let
s 2 S. Because s is nite and non-empty, it has a largest element M and
a smallest element m. We consider the set s0 obtained from s by replacing
m by m + 1 and M by M m. This is also nite and non-empty, and it is
uniquely determined by s. So we have a function f : s 7! s0 of S into itself,
given by
f : s 7! f (s) = ((s n fmg) n fM g) [ fm + 1g [ fM mg: (9.4)
We construct recursive sequences in S , via f
s0 = s st+1 = f (st ); t 0: (9.5)
For example, the initial condition s0 = f 1; 2; 4g leads to an eventually
periodic sequence, with transient length 5 and period 3
f 1; 2; 4g 7! f0; 2; 5g 7! f1; 2; 5g 7! f2; 4g 7! f2; 3g
7! f1; 3g 7! f2g 7! f0; 3g 7! f1; 3g 7!
(a) Implement the function f in (9.5) with Maple.
(b) Explore the behaviour of the sequence (9.5).
(c) Prove that the sequence (9.5) is eventually periodic, for any choice of
s0 .
Exercise 9.10. Let x be a positive integer. We construct a new integer
f (x) according to the following rule
f (x) = 3x=x2+ 1 ifif xx is odd
is even. (9.6)
Explore the behaviour of the associated recursive sequence. This is the cel-
ebrated `3x +1' problem. The main conjecture | that all orbits eventually
reach the cycle (4; 2; 1) | is still unproved.
9.3. CONDITIONAL EXECUTION 177
Exercise 9.11 .
Prove that the map Fn of equation (9.3) is well-de ned,
in the sense that if x is an n-digit integer whose digits are not all the same,
so is Fn (x).
We shall agree to return the NULL value when the argument x is out of
range.
> f:=x->if x < 0 or x > 100 then
> NULL
> elif x < 40 then
> F
> elif x < 50 then
> E
> elif x < 60 then
> D
> elif x < 70 then
> C
> elif x < 80 then
> B
> else
> A
> fi:
The rst logical expression is true only if x is out of range. If this is not the
case, then x 0, necessarily, so that the single inequality x < 40 suÆces
to check the two inequalities in (9.7), and so on. If the control reaches
the keyword else, then all previous logical expressions in the structure are
false, whence 80 x 100, and no further check is required for the grade
A.
(An alternative way of constructing piecewise-de ned functions is af-
forded by the standard library function piecewise.)
Example 9.8. We develop an algorithm for nding a rational approxima-
tion to an irrational number lying in the unit interval [0; 1]. The algorithm
is based on repeated construction of mediants p (the mediant of two ration-
als was de ned in (2.20)). We choose = ( 5 1)=2. (This number has
a special status: it is the irrational between 0 and 1, which is the most
diÆcult to approximate by rationals.) We begin by de ning the function
med for the mediant, and the small parameter small, which represents the
desired accuracy of the result.
> med:=(l,r)->(numer(l)+numer(r))/(denom(l)+denom(r)):
> small:=1/1000:
> alpha:=evalf((sqrt(5)-1)/2):
The variable alpha has been assigned a float value, in order to allow the
evaluation of the expression abs(m-alpha) >= small to Boolean (see be-
low). The starting interval is the unit interval, with endpoints left and
right. The iteration is carried out within a do loop controlled via the
while option.
> left:=0:
9.4. PROCEDURES 179
> right:=1:
> m:=med(left,right):
> while abs(m-alpha) >= small do
> if m < alpha then
> left:= m
> else
> right:=m
> fi:
> m:=med(left,right)
> od:
> m;
21
34
We have implicitly assumed that the expression abs(m-alpha) >= small
will eventually evaluate to false, thereby ensuring that the loop is not exe-
cuted inde nitely (see exercises).
Exercises
Exercise 9.12. Consider the following irrational numbers in the unit in-
terval (e = 2:718 : : : is Napier's constant)
p
5 1 ; p2 1; 3; e 2:
2
By repeated iterations of the mediant method, determine rationals approx-
imating these numbers within 10 6. Compare the size of denominators.
Exercise 9.13 .
Prove that for any choice of in the unit interval, the
do-loop of the mediant algorithm does eventually terminate.
9.4 Procedures
A function de ned by means of the arrow operator -> is a simple instance of
a more general construct called a procedure , which we shall brie y describe
in this section. We recall the syntax of the arrow operator
> name:=(arguments sequence)->expression;
The following function adds 1 to each operand of an arbitrary object
> AddOneTo:=anything->map(x->x+1,anything):
The same function can be de ned as a Maple procedure as follows
> AddOneTo:=proc(anything)
> map(x->x+1,anything)
> end:
and then called as before
180 CHAPTER 9. ELEMENTS OF PROGRAMMING
[1009; 1013; 1019; 1021; 1031; 1033; 1039; 1049; 1051; 1061; 1063;
1069; 1087; 1091; 1093; 1097 ]
The argument a must be an integer, being used by the function nextprime.
This requirement is extended to the argument b for consistency. The two
variables p and L are local. The initialization p:=nextprime(a-1) ensures
that if a is prime, then it is included in the list. The output is stored in
the variable L, which is initialized to the empty list. The reader is invited
to compare the running time of this function with that of the simpler but
slower construct developed in chapter 6, using select. (To compute run
time, see ?time.)
Example 9.11. We construct a function freq that computes the fre-
quency of occurrence of the elements of an arbitrary list. For instance, in
the list [a,b,ba,b,ba,b,ab,b], the elements a and ab appear once, ba
twice, and b four times.
> freq([a,b,ba,b,ba,b,ab,b]);
[a; 1]; [b; 4]; [ba; 2]; [ab; 1]
The output is to be organized in the form of a sequence of two-elements
lists, containing each value with its frequency.
> freq:=proc(L::list)
> local l, x;
> seq([x, nops(select((l,x)->evalb(l=x), L, x))], x=fop(L)g)
> end:
The expression x=fop(L)g causes the control variable x in seq to run
through all distinct elements of the list L, which is turned into a set to
eliminate repetitions. The Boolean function (l,x)->evalb(l=x) is ap-
plied by the function select to all elements of L. Its second argument x
is supplied as the third optional argument of select. The result of the
selection is the list of those elements of L which are equal to x. Finally, the
latter are counted by nops.
As an application, we consider the sequence of remainders of division of
the rst 1000 odd primes by 4. For instance, if we divide by 4 the primes
182 CHAPTER 9. ELEMENTS OF PROGRAMMING
Vector spaces
This chapter contains some elements of linear algebra: the construction
of a vector space (in the limited context of direct product of elds), the
arithmetic of its elements (the vectors ), and the study of a most important
class of functions de ned over vector spaces (represented by matrices ).
We describe a general procedure for constructing new sets from old ones,
which is analogous to the construction of a two-dimensional plane or a
three-dimensional space, from a one-dimensional line.
Let T be a set. We denote by T 2 = T T the set of ordered pairs of
elements of T , called the Cartesian product of T with itself. Thus, T 2 is
the collection of elements v = (v1 ; v2 ) with v1 ; v2 2 T ; the requirement that
the pair be ordered means that (v1 ; v2 ) 6= (v2 ; v1 ). Similarly, one de nes
T 3 = T T T , the set of ordered triples (v1 ; v2 ; v3 ) of elements of T , and
in general the set T n of ordered n-tuples
T = T| T {z T} = f(v1 ; v2 ; : : : ; v )g
n
n v
k 2 T; k = 1; : : : ; n:
n times
The elements of T are called vectors.
n
If T U , then T Un
, for all positive n (why?). In particular, for
n
185
186 CHAPTER 10. VECTOR SPACES
Example 10.2. Let T = f0; 1g. Then T 3 has 23 = 8 elements
T 3 = f(0; 0; 0); (0; 0; 1); (0; 1; 0); (0; 1; 1); (1; 0; 0); (1; 0; 1); (1; 1; 0); (1; 1; 1)g:
Example 10.3. Let I = [0; 1] be the unit interval
I = fx 2 R j 0 x 1g: (10.1)
Then I 2 R2 is a square of unit area, while I 3 R3 is a cube of unit
volume.
In the de nition of Cartesian product of sets, there is no requirement
that the sets involved be identical copies of the same set. Thus, T S is
de ned as the set of ordered pairs v = (t; s) with t 2 T and s 2 S .
Example 10.4. Let T = f1; 2; 3g, and U = f 1; 2g. Then
T U = f(1; 1); (1; 2); (2; 1); (2; 2); (3; 1); (3; 2)g:
Example 10.5. Let T be a line and S a circle. Then T S is an in nite
cylinder. If I is the unit interval (10.1), then I S is an annulus.
Example 10.6. Let T be any set, and S = f1; 2; : : : ; ng. Then T S
can be thought of as the collection of n copies of T , indexed by the integers
1; 2; : : : ; n.
u v = u1 v1 + u2 v2 + +u v
n n = u v:
k k
k =1
The scalar product satis es the following properties, whose veri cation
is left as an exercise.
1. u v 2 F .
2. u v = v u.
3. u (v + w) = u v + u w.
4. ( u) v = (u v ); u ( v ) = (u v ):
5. If v = O = (0; 0; : : : ; 0), then v v = 0. Otherwise, v v > 0.
A distance in a vector space
The size of a vector v = (v1 ; : : : ; vn ) in V is called the norm, which is
de ned as
jjvjj = v12 + v22 + + vn2 = pv v:
q
(10.2)
As usual, we de ne the distance between two vectors as the norm of their
di erence: jjv ujj. For F = R, this distance becomes the ordinary Eu-
clidean distance. Furthermore, for n = 1, the norm becomes the absolute
value.
There are other ways of de ning the norm of a vector, whence a distance
in F n . Speci cally, for any positive real number p we de ne the p-norm of
v as
jjvjjp = (jv1 jp + jv2 jp + + jvn jp )1=p : (10.3)
Thus, the norm de ned in (10.2) is the 2-norm (p = 2). The case p = 1 is
also interesting. The 1-norm of a vector is the sum of the absolute value of
the coordinates.
jjvjj1 = jv1 j + jv2 j + + jvn j:
Exercises
Exercise 10.1. Complete the proofs of the properties 1{6 of a vector
space, rst for n = 2, and then for arbitrary n.
(With the semicolon, all functions being loaded are listed.) We begin con-
structing an unspeci ed 2-dimensional vector v = (v1 ; v2 ).
> v:=array(1..2);
v := array(1::2; [ ])
> v,whattype(v);
v; symbol
Unlike for other data types, the symbolic name of an array evaluates to
itself (a symbol). Full evaluation requires a call to the intrinsic function
evalm (evaluate to matrix)
> evalm(v);
[v1 v2 ]
> whattype(%);
array
In computational parlance, we say that the array's name represents a
pointer to the location in memory where the array information is stored.
The function evalm is used to fetch that information. (A procedure's name
has a similar status; see section 3.4.)
With the function op we can get some insight into the internal repre-
sentation of an array in Maple
> op(evalm(v));
1::2; 1 = v[1] ; 2 = v[2]
Thus, an array consists of a range, which speci es the dimension, and a
list, where each entry is assigned a value.
Next we construct a speci c vector in Z2
> v:=array([1,-4]);
v := [1 4]
The internal representation of v has now changed
> op(evalm(v));
1::2; [1 = 1; 2 = 4]
All components of an array may be accessed and assigned new values
> v[2];
4
> v[2]:=-3:evalm(v);
[1 3]
A vector can be multiplied by a scalar in a straightforward manner
> -2*v:%=evalm(%);
190 CHAPTER 10. VECTOR SPACES
2v = [ 2 6]
Likewise, vectors can be easily summed and subtracted
> u:=array([0,1]):
> z:=evalm(u+v);
z := [1 2]
> whattype(z),whattype(evalm(z));
symbol; array
In the last example, the new array z was generated without resorting to the
array command, by simply assigning to the name z a datum of the array
type. By contrast, the assignment
> w:=u+v:
assigns to w the algebraic expression u+v. Evaluation of z and w gives the
same result
> evalm(z),evalm(w);
[1 2]; [1 2]
but whereas z points to an array with a speci ed value, w points to the sum
of two variables which in turn point to arrays. Thus, if we change the value
of v
> v[1]:=99:
the value of w changes, but that of z does not
> evalm(z),evalm(w);
[1 2]; [99 2]
The scalar product | also called dot product | is supported by the
linalg function dotprod
> dotprod(u,v);
3
The p-norm of a vector (cf. equation (10.3)) is given by the function norm,
with p as a second argument
> norm(v,2);
p
10
> w:=array(1..3):norm(w,4);
1=4
jw1 j4 + jw2 j4 + jw3 j4
Example 10.7. Let u = ( 2; 3; 1) 2 Z3. Consider the following recur-
sive sequence v0 ; v1 ; : : : of elements of Z3
v0 = (1; 0; 0); v +1 = f (v ) = 2 v + u; t 0:
t t t
10.4. MATRICES 191
We show that the rst 10 elements of this sequence are orthogonal to the
vector w = (0; 1; 3).
> u:=array([-2,-3,1]):
> w:=array([0,1,3]):
> v:=array([1,0,0]):
> f:=v->evalm(2*v+u):
> for t to 10 while dotprod(v,w) <> 0 do
> v:=f(v)
> od:
> t;
11
As usual, all the information is buried in the function f that performs the
recursion. There is no need of displaying any intermediate computation.
Failure of orthogonality would have resulted in a smaller value of t (make
sure you understand this).
10.4 Matrices
c = a 1 b1 + a 2 b2 +
ij i j i j +a b in nj = a b :
ik kj
k =1
If A and B are square matrices, then both A B and B A are de ned, but
in general we have that A B 6= B A. Failure of commutativity results in a
very rich arithmetic.
Matrices may also represent certain functions , which assign to a vector
v in a vector space V1 the unique vector A v in another space V2 . (The
multiplication between a matrix and a vector turns out to be a special
case of multiplication between matrices.) The properties of matrices then
become properties of these functions.
> A+B:%=evalm(%);
A + B = 24 10
Matrix multiplication is non-commutative: if A and B are n by n ma-
trices, then AB 6= BA, in general. Maple supports the non-commutative
multiplication operator &*, which preserves the order of the operands.
> evalm(A&*B);
10.5. MATRICES WITH MAPLE 193
1 1
5 1
> evalm(B&*A);
0 2
3 0
> evalm(A*B);
Error, (in evalm/evaluate) use the &* operator for matrix/vector multipli-
cation
Maple knows that the commutative multiplication operator * is ambiguous
for matrices, and refuses to carry out the multiplication.
The following example illustrates further the di erences between com-
mutative and non-commutative multiplication, and clari es Maple's strat-
egy with expressions involving matrices.
> K:=array([[1,1],[1,0]]):
> L:=array([[2,1],[1,1]]):
The matrices K and L commute, that is, KL = LK .
> evalm(K&*L),evalm(L&*K);
3 2
2 1
; 32 21
Hence the commutator of K and L, given by KL LK , is equal to the 2
by 2 null matrix
> evalm(K&*L-L&*K);
0 0
0 0
However, if we instead use commutative multiplication, the result is quite
di erent (and wrong!)
> evalm(K*L-L*K);
0
> whattype(%%),whattype(%);
array; integer
Using commutative multiplication, we have obtained the integer 0 rather
than the 2 by 2 null matrix! Why? Also, why hasn't Maple complained |
as it did before | when we used commutative multiplication with matrices?
The reason is the following. Before accessing a matrix with evalm, Maple
carries out some automatic algebraic simpli cations, regarding K and L as
symbols. (This could result in massive eÆciency savings when dealing with
large arrays.) Thus, Maple rst transforms K*L into L*K, (or vice versa,
depending on the context), which is legitimate because * is commutative.
194 CHAPTER 10. VECTOR SPACES
Therefore, K*L-L*K evaluates to the integer 0, which is a valid argument
for evalm, that treats it as a 1 1 matrix.
As for vectors, matrices can be generated directly by assigning to a
variable the value of an evaluated matrix. In the next example, the value
of the variable C is A, which points to an array, while the variable D points
to a copy of the array A.
> C:=A:
> D:=evalm(A):
> A[1,1]:=22:
> evalm(C),evalm(D);
22 1
3 1
; 03 1
1
So changing A changes C but not D.
The library linalg supports a vast number of functions associated with
matrix arithmetic
> M:=array([[a,b],[c,d]]);
M := ac db
We begin with determinant and trace
> det(M),trace(M);
ad bc; a + d
Next we construct the characteristic polynomial of M , in the indeterminate
y
> charpoly(M,y);
y2 yd ya + ad bc
> collect(%,y);
y2 + ( | d{z a} ) y + ad bc
| {z }
Tr(M ) det(M )
We list some linalg functions applied to a matrix with integer coeÆcients
> A:=([[0,1],[3,-1]]):
> evalm(A),trace(A),det(A),rank(A),transpose(A),
> inverse(A),eigenvalues(A);
1 1p 1p
1 1
0 1 1
3 1
; 1; 3; 2; 01 3
1
; 3 3 ;
1 0
+ 13; 13
2 2 2 2
> map(expand,%);
[x2 + 2x + 1 x4 ]
> x:=22:
> evalm(v);
[x + 1 x2 ]
> map(eval,v);
[23 484]
It should be noted that map acts on the entries of a matrix, even though
the latter are not its top-level operands (see chapter 6). With A as above,
we have
> op(evalm(A));
> seq(whattype(x),x=[%]);
::; ::; list
We see that the operands of a matrix are two ranges specifying the dimen-
sions, and a list of equations de ning the entries. An array is a special case
of a more general construct called a table. See online documentation for
more information.
Exercises
Exercise 10.2. Load the linalg package.
(a) Consider the following vectors in Z3
u = (88; 2; 33) v = ( 7; 27; 1):
Compute their sum and their scalar product. Determine which one is the
longest, using the 2-norm.
(b) Construct the following matrices
0 1
1 2
1 1 1
A = @2 0 A B= 4 6 2
:
3 1
Then modify one entry of A as follows (without rede ning the entire ma-
trix!) 0 1
1 2
A=@ 2 0 A (10.4)
5 1
198 CHAPTER 10. VECTOR SPACES
Then, using (10.4), compute BA, A B T , and AT + B (the superscript T
denotes the transpose of a matrix).
Exercise 10.3. Consider the recursive sequence of matrices
A0 = 21 11 A +1 = A2
t t A
t I2 ; t 0;
where I2 is the 2 2 identity matrix.
(a) Compute A5 , using a do-loop. Do not display any intermediate output.
(b) Compute and display the elements of the sequence of integers Dt =
trace(At ) for t = 1; : : : ; 5. No other output should be displayed.
Exercise 10.4. The commutator [A; B ] of two square matrices A and B
is de ned as
[A; B ] = AB BA:
(Note that if A and B were numbers, [A; B ] would be zero.)
(a) Construct a user-de ned function comm(A,B) whose value is [A; B ].
(b) Given the matrices
A = 01 1
1
B = 01 0
1
(d) Using Maple, prove by induction your conjecture of part (c). (This is
a computer-assisted proof.)
(e) Let M be as in (10.5). Consider the recursive sequence of vectors of Z3
v0 = (1; 0; 0) v +1 = M v + v0 ;
k k k 0:
Compute the smallest integer k for which the scalar product of vk and v0
exceeds 50.
Exercise 10.6. Let A and B be 3 by 3 matrices. Prove with Maple that
0
s 0 0 24 1
A(s) = B 1 s 0 50 C
:
@
0 1 s 35 A
0 0 1 10 s
Modular arithmetic
201
202 CHAPTER 11. MODULAR ARITHMETIC
Note that the modulus m has data type posint, a positive integer.
204 CHAPTER 11. MODULAR ARITHMETIC
Exercises
Exercise 11.1. Construct the sets F 2 , F3 , and F 7 , as in (11.1).
Exercise 11.2. Verify the following congruences
6 5030497 (mod 503); 210 + 1013 29274 0 (mod 1000003):
Exercise 11.3. Determine the largest integer less than a million which
belongs to the class [777]1009 in F1009 .
Exercise 11.4. Prove that the union of the elements of F p is Z, and that
they are pairwise disjoint (they form a partition of Z.)
This theorem guarantees that for each class [b]p 6= [0]p we can nd a
unique class 1=[b]p , satisfying (11.2). But then for each [a]p and nonzero
[b]p , the quantity [a]p =[b]p is uniquely de ned, since
[a]p [1]
= [a]p p :
[b]p [b]p
The construction of the inverse of b rests on Euclid's algorithm for the
greatest common divisor of two integers, according to which if b and p are
coprime, we can nd integers x and y such that x b + y p = 1, that is,
x b 1 (mod p).
and every nonzero square is the image under f of two distinct elements of
Z.
The squares in Z form a `thin set', in the sense that the probability that
an integer taken at random is a square tends to zero as the integer becomes
large. For instance, 40% of the integers less than 10 are squares, 10% of
the integers less than 100 are squares, and only some 3% of the integers
less than 1000 are squares.
The squares are also de ned in Fp , where the situation is quite di erent.
We begin with the case p = 7.
> F7:=f$0..6g;
f0 ; 1; 2; 3; 4; 5; 6g
To square the elements of F 7 , we make use of the operator mod
> f:=x->x^2 mod 7:
and to apply the function f to every element of F 7 we use the standard
library function map
> map(f,F7);
f0; 1; 2; 4g
This calculation shows that half of the 6 nonzero elements in F 7 are squares.
Speci cally, modulo 7, we have
1 2 6 2 1; 2 2 5 2 4; 3 2 4 2 = 2:
Next we count the squares in F 101
> f$0..100g:
> map(x->x^2 mod 101,%):
> nops(%)-1;
50
Thus, also in F101 , half of the nonzero elements are squares. This result is
true in general.
> F29:=f$0..28g:
> f:=(x,b,p)->x^b mod p:
> map(f,F29,2,29):
> map(f,F29,4,29):
> %% minus %;
f4 ; 5; 6; 9; 13; 22; 28g
Polynomials
Since Fp is a eld, de ning polynomials with coeÆcient in F p presents no
conceptual diÆculty: one represents the coeÆcients as integers (or rationals
with denominator coprime to p). The operator mod will then reduce them
to the range 0; : : : ; p 1.
> 40*x^2-x/3-19 mod 13;
2
x +4x+7
For polynomial divisibility, Maple provides the inert functions Factor, Quo,
and Rem, to be used in conjunction with mod.
Let us consider the polynomial g (x) = x2 +1 in F 5 . Since 5 0 (mod 5)
and 6 1 (mod 5), we have
( ) = (x + 2) (x + 3) = x2 + 5x + 6 x2 + 1 (mod 5):
g x
We see that g (x) factors in F 5 as (x + 2)(x +p3), much in the same way
as it factors in C as (x + i)(x i), where i = 1 is the imaginary unit.
Therefore, 2 3 and 3 2 are the two square roots of 1 in F 5 .
> Fg:=p->Factor(x^2+1) mod p:
> Fg(5);
(x + 2) (x + 3)
Writing it all out in detail, we have
( ) = [1]5 x2 + [1]5 = (x
g x [2]5 ) (x [3]5 ); g ([2]5 ) = g ([3]5 ) = [0]5 ;
that is, g(x) has two distinct roots in F 5
Let us look into the factorization of x2 + 1 in F p , for other values of p
> seq(Fg(ithprime(k)),k=1..6);
(x + 1)2 ; x2 + 1; (x + 3) (x + 2) ; x2 + 1; x2 + 1; (x + 8) (x + 5)
> map(whattype,[%]);
[^; +; ; +; +; ]
Three types of factorizations occur, corresponding to x2 + 1 having one
double root (data type ^), two distinct roots (data type ), or no roots at
all (data type +) modulo p. It turns out that these are the only possibilities.
We explore this phenomenon for a large set of primes
> data:=map(whattype,[seq(Fg(ithprime(k)),k=1..1000)]):
Now the variable data contains the data type of the factored polynomials,
which in this case is preferable to storing the polynomials themselves. We
count the occurrence of each of the three types, using the function select
in conjunction with nops
11.3. SOME ARITHMETICAL CONSTRUCTIONS IN FP 211
> seq(nops(select((x,y)->evalb(x=y),data,y)),y=[`^`,`*`,`+`]);
1; 495; 504
Thus, the double root occurs only once (for the prime p = 2), while the
other two cases occur with roughly equal probability.
Exercises
Exercise 11.5. Determine the cubes in F101 .
Exercise 11.6. Show that all elements of F 29 have at least one cube root.
Exercise 11.7. Determine all primitive roots modulo 101.
Exercise 11.8. Convince yourself that for every positive integer k the
polynomial x2 + 1 has two distinct roots modulo 5k . That is, there exist
integers ak and bk such that x2 + 1 (x ak )(x bk ) (mod 5k ). Represent
ak and bk to the base p: what do you observe as k becomes large?
Note that the integers 0 and 1 enjoy a special status, in that addition
by 0 and multiplication by 1 do not alter the elements of Z. The properties
IV should indeed be taken as the de nition of the identity elements of
addition and multiplication, respectively. Any set satisfying these axioms
(with 0 and 1 possibly replaced by two elements of the set) will have many
properties in common with Z. Sets of this type are called integral domains:
they are the integers' closest relatives.
213
214 CHAPTER 12. SOME ABSTRACT STRUCTURES
(x; y) =
X jx
n
k yk j:
k=1
217
Index
3x + 1 conjecture, 176 data type
exprseq, 62
abs, 19, 28, 30, 37, 105, 108 oat, 97
add, see sum fraction, 27
arccos, arcsin, arctan, 108 integer, 27
associativity, 8, 9, 44, 187, 191, list, 64
213{215 posint, 203
procedure, 51
binomial range, 62
binomial, 153, 164 set, 45
coeÆcient, 78 string, 66
theorem, 80 symbol, 52
de Moivre's theorem, 108
Cassini identity, 85 degree, 128
ceil, 108, 109 denom, 27, 28, 37, 54, 93, 109, 178
commutativity diff, 2
commutator, 193 digits
non-commutative multiplica- Digits, 90
tion operator, 192 number of, 37, 38, 109
of addition, 187, 191, 213{ of rationals, see rational num-
215 bers
of logical operators, 47 pseudo-random, 100
of multiplication, 213{215 Diophantus of Alexandria, 24
complex conjugate, 104 discrete logarithm, 209
computer-assisted proof, 76, 158, distance
174, 199 in a metric space, 214
conjugate, 105 in a vector space, 188
constants in C , 104
p, 2, 89 in Q , 27
2, see square root in R, 96
e (Napier's constant), 109, 179 in Z, 7
continued fractions, 31 triangle inequality, 7, 28, 105
convert, 142 distributivity, 215
cos, cosh, 108 ditto variables, see variables
cot, coth, 108 divisors
csc, csch, 108 greatest common, 22, 24
219
220 INDEX