discretemaths book

195
Discrete Mathematics Notes c David A. Santos Philadelphia, PA May 29, 2002

Upload: matt-malthus

Post on 21-Nov-2014

109 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Discretemaths Book

Discrete Mathematics Notes c©

David A. Santos

Philadelphia, PA May 29, 2002

Page 2: Discretemaths Book
Page 3: Discretemaths Book

Contents

Preface v

1 An Introduction to C++ 11.1 C++ Alphabet . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.1.1 Comments . . . . . . . . . . . . . . . . . . . . . . . 21.1.2 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . 31.1.3 Keywords . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2 Predefined Types . . . . . . . . . . . . . . . . . . . . . . . . 41.2.1 Notation for Types . . . . . . . . . . . . . . . . . . . 5

1.3 Variables and Constants . . . . . . . . . . . . . . . . . . . . 71.3.1 Scalar Types . . . . . . . . . . . . . . . . . . . . . . 71.3.2 Aggregate Types . . . . . . . . . . . . . . . . . . . . 91.3.3 Constants . . . . . . . . . . . . . . . . . . . . . . . . 13

1.4 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . 141.5 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1.5.1 Unary Operators . . . . . . . . . . . . . . . . . . . . 161.5.2 Arithmetical Binary Operators . . . . . . . . . . . . . 161.5.3 Assignment Operators . . . . . . . . . . . . . . . . . 191.5.4 Relational Operators . . . . . . . . . . . . . . . . . . 241.5.5 Logical Operators . . . . . . . . . . . . . . . . . . . 241.5.6 Conditional Operator . . . . . . . . . . . . . . . . . . 251.5.7 Comma Operator . . . . . . . . . . . . . . . . . . . . 26

1.6 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261.7 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

1.7.1 main() Function . . . . . . . . . . . . . . . . . . . . 31

iii

Page 4: Discretemaths Book

iv CONTENTS

1.7.2 Function Calls . . . . . . . . . . . . . . . . . . . . . 321.7.3 Parameter Passing . . . . . . . . . . . . . . . . . . . 34

1.8 if{}else{} Statement . . . . . . . . . . . . . . . . . . . . . 371.9 switch{} Statement . . . . . . . . . . . . . . . . . . . . . . 401.10 for(){} Loop . . . . . . . . . . . . . . . . . . . . . . . . . . 421.11 while(){} Loop . . . . . . . . . . . . . . . . . . . . . . . . 441.12 Examples of Full Programmes . . . . . . . . . . . . . . . . 45

2 Essential Techniques 12.1 Reductio ad Absurdum . . . . . . . . . . . . . . . . . . . . . 12.2 Pigeonhole Principle . . . . . . . . . . . . . . . . . . . . . . 42.3 Inclusion-Exclusion . . . . . . . . . . . . . . . . . . . . . . . 102.4 Parity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

3 Counting 193.1 Introductory Problems . . . . . . . . . . . . . . . . . . . . . 193.2 Two Basic Counting Principles . . . . . . . . . . . . . . . . 233.3 Permutations . . . . . . . . . . . . . . . . . . . . . . . . . . 283.4 Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . 323.5 Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . 373.6 Combinatorial Identities . . . . . . . . . . . . . . . . . . . . 383.7 The Binomial Theorem . . . . . . . . . . . . . . . . . . . . . 403.8 Multinomial Theorem . . . . . . . . . . . . . . . . . . . . . . 57

4 Arithmetic 614.1 Division Algorithm . . . . . . . . . . . . . . . . . . . . . . . 614.2 The Decimal Scale . . . . . . . . . . . . . . . . . . . . . . . 664.3 Non-decimal Scales . . . . . . . . . . . . . . . . . . . . . . 734.4 Well-Ordering Principle . . . . . . . . . . . . . . . . . . . . 774.5 Mathematical Induction . . . . . . . . . . . . . . . . . . . . 804.6 Congruences . . . . . . . . . . . . . . . . . . . . . . . . . . 874.7 Miscellaneous Problems with Integers . . . . . . . . . . . . 94

5 Sums, Products, and Recursions 1035.1 Telescopic cancellation . . . . . . . . . . . . . . . . . . . . 1035.2 Arithmetic Sums . . . . . . . . . . . . . . . . . . . . . . . . 1075.3 Geometric Sums . . . . . . . . . . . . . . . . . . . . . . . . 1115.4 Fundamental Sums . . . . . . . . . . . . . . . . . . . . . . 114

Page 5: Discretemaths Book

CONTENTS v

5.5 First Order Recursions . . . . . . . . . . . . . . . . . . . . . 1195.6 Second Order Recursions . . . . . . . . . . . . . . . . . . . 1265.7 Applications of Recursions . . . . . . . . . . . . . . . . . . 128

Page 6: Discretemaths Book

vi CONTENTS

Page 7: Discretemaths Book

Preface

These notes started during the Spring of 2002

Page 8: Discretemaths Book

viii

Page 9: Discretemaths Book

Chapter 1An Introduction to C++

1.1 C++ Alphabet

1 Definition The C++ alphabet is the collection of the following characters:

a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G

H I J K L M N O P Q R S T U V W X Y Z

+ - * / % \ " ’ # , . ; : ! ? ( ) [ ] { } < > ^ &

~ | _ 0 1 2 3 4 5 6 7 8 9

2 Definition A token is a string of characters recognised as meaningful bythe C++ grammar (set of rules of the C++ language). The source of thesecharacters is the C++ alphabet

The C++ tokens are: the keywords, the identifiers, the constants, thestring-literals, the operators, and the punctuators, which will be definedshortly.

3 Definition White space is the name given to spaces (blanks), horizontaland vertical tabs, newline characters and comments.

! White space does not belong to the C++ alphabet. Whitespace servesto indicate where tokens start and end: beyond this, any surplus of whites-pace is discarded.

1

Page 10: Discretemaths Book

2 Chapter 1

1.1.1 Comments

4 Definition A comment is an explanatory note for the humans reading theC++ code and it is otherwise ignored by the compiler.

Comments in C++ may occur in one of two forms. They may begin with adouble slash and end in the same line where they started, as in

// This is a valid C++ comment.

// This is another valid C++ comment.

This is an invalid comment. It does not start with slashes.

Another way of writing comments, inherited from C, is by enclosing thecomment within an opening comment symbol /∗ (no space between theslash and the asterisk) and a closing comment symbol ∗/. This methodallows the use of multiple lines, as in/* ’Twas brillig, And the slithy toves did gyre and gimble

in the wabe. All mimsy were the borogoves and the mome raths

outgrabe. */

! Comments of the type / ∗ COMMENT ∗ / cannot be nested. Thecode/* Something /* will go */ awry */

will produce a compiler error. This is because the compiler matches the first/∗ with the first ∗/ that it encounters, and so the second /∗ is viewed as partof the outer comment.

5 Definition A punctuator of C++ is any of the following characters (or inthe case of the three successive dots ..., a group of characters):

[ ] ( ) { } , ; : ... * = #

Page 11: Discretemaths Book

C++ Alphabet 3

1.1.2 Blocks

6 Definition A block is a grouping delimited by a left brace { and a match-ing right brace }. Any two blocks may be either disjoint or nested.

7 Example The following blocks are disjoint.{ // opens block I

.............

} // closes block I { // opens block II

.............

} // closes block II

8 Example In the following example, blocks II and III are disjoint, and bothare nested into block I.{// opens block I ................

{// opens block II

.................

}// closes block II

{// opens block III

..................

}// closes block III

................. }// closes block I

! Indentation, albeit ignored by the C++ compiler, is recommended whendealing with nested blocks. Two disjoint blocks at the same level shouldhave the same indentation, a block nested into another should be indentedtowards the right.

1.1.3 Keywords

9 Definition A C++ keyword is a string of characters having a predeter-mined meaning in the C++ language.

A list of standard C++ keywords appears in table 1.1.

Page 12: Discretemaths Book

4 Chapter 1

and and_eq asm auto bitand

bitor bool break case catch

char class compl const const_cast

continue default delete do double

dynamic_cast else enum explicit export

extern false float for friend

goto if inline int long

mutable namespace new not not_eq

operator or or_eq private protected

public register reinterpret_cast return short

signed sizeof static static_cast struct

switch template this throw true

try typedef typeid typename union

unsigned using virtual void volatile

wchar_t while xor xor_eq

Table 1.1: Standard C++ Keywords.

! All standard C++ keywords are in lowercase letters.

1.2 Predefined Types

10 Definition A type is a set of values that can be associated with givendata items.

Some predefined types in C++ are:

• the type void. This type is effectively the empty set.

• the type bool. This type is the boolean type, which can only assumethe values true or false.

• the type char. This is the character type. Used to represent singlecharacters like those forming the C++ alphabet.

• the type wchar_t. This is the long character type.

Page 13: Discretemaths Book

Predefined Types 5

• the type string. This is the string type. Used to represent strings ofcharacters.

• the type int. This is the integer type. The range of the integers ismachine-dependent, but we can at least assure ourselves that inte-gers in the interval [−231; 231 − 1] will be represented.

• the type float. This is the real number type. Its range is machine-dependent, but we can at least assure ourselves that the interval[3.4 × 10−38; 3.4 × 1038] will be represented.

• the type double. These are real numbers with double precision. Nor-mally in the range [1.7 × 10−308; 1.7 × 10308].

The range of the int type can be modified by the keywords long or short.For example, in some machines a long int is in the range [−263; 263 − 1].

! For the purposes of these notes we will restrict to numerical data oftype int for integers or double for real numbers.

1.2.1 Notation for Types

C++ distinguishes between decimal (base 10), octal (base 8), and hex-adecimal (base 16) integers. To indicate the compiler the different type ofbase desired, we use:

• (decimal integers) no prefix, the ten digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},and an optional sign {+,−}.

• (octal integers) the prefix 0 (zero), the eight digits {0, 1, 2, 3, 4, 5, 6,7}. The negative sign may not be used in some implementations.

• (hexadecimal integers) the prefix 0x (zero x) or 0X, the digits {0, 1,2, 3, 4, 5, 6, 7, 8, 9, A, a, B, b, C, c, D, d, E, e, F, f}. The negativesign may not be used in some implementations.

11 Example The following are decimal integers234 -10101

The following are octal integers

Page 14: Discretemaths Book

6 Chapter 1

0234 0101 The following are hexadecimal integers0X23A4F 0x10101

To indicate the compiler that we are using a real floating point (decimal)number we use a decimal point, the ten digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},the letters e or E to indicate a power of 10, and an optional sign {+,−}.

12 Example The following are floating point numbers2. 2.34 -101.01e23 123.01E-23

! The compiler distinguishes between, say, “7” (no decimal point) and“7.” (with a decimal point). “7” is an integer, “7.” is a floating point number.

To indicate the compiler that we are using a character type, we enclosethe character in single quotes.

13 Example The following are character types’2’ ’A’ ’)’

Some characters cannot be found on the keyboard or have predeterminedmeanings in the C++ grammar. In order to represent them we need abackslash character. For example,

• ’\n’ denotes the newline character (to go to the beginning of thenext line),

• ’\t’ denotes a horizontal tab character,

• ’\v’ denotes a vertical tab character,

• ’\b’ denotes a backspace character,

• ’\a’ denotes the bell or audible alert character,

• ’\\ ’ denotes a backslash character,

• ’\’’ denotes the single quote character,

• ’\"’ denotes the double quote character,

Page 15: Discretemaths Book

Variables and Constants 7

• ’\?’ denotes the question mark character,

• ’\r’ denotes the carriage return (return to the beginning of the presentline) character,

• ’\0’ denotes the null character.

To indicate the compiler that we are using a string type, we enclose thestring in double quotes.

14 Example The following are string types ”A” ”Anthony” ”I break forcamels!”

! There is a distinction between ’A’ (a character type) and "A" (a stringtype).

15 Example To produce the output”What is your name silly?”, said the idiot.

we need to type”\”What is your name silly\?\”,\nsaid the idiot.\n”

1.3 Variables and Constants

1.3.1 Scalar Types

16 Definition An identifier is a finite string of characters which:

• does not contain whitespace,

• does not form a keyword,

• is composed of letters, underscores, or digits only, and,

• whose first character is not a digit.

17 Example The following are valid C++ identifiers:R_7 long_nose_size is_prime PI Pi _first_of_many Float

Page 16: Discretemaths Book

8 Chapter 1

Notice that since C++ is case sensitive, the identifiers PI and Pi are dis-tinct.

18 Example The following are not valid as C++ identifiers:freedom-of-thought //illegal character - 2_out_of_3

//begins with a digit float //keyword £amount

//illegal character £

19 Definition A variable is an instance of a type. It is a memory locationwith a specific address and identifier.

Variables can be modified during the life programme. The type of thevariable specifies how much memory is allocated to the variable.

20 Definition A variable declaration is a statement with syntaxtype identifier;

! The declaration of the variable allocates space for it and a specificlocation for it in the computer memory. Thus a variable cannot be utilisedwithout first being declared.

C++ allows multiple variables of the same type to be declared on one lineby means of a comma. For instance, the codeint var_a; int var_b; double var_c;

is equivalent to the codeint var_a, var_b; double var_c;

It is possible to declare and initialise a variable at the same time, bymeans of the syntaxtype identifier = particular_value;

21 Example The following are examples of variable declarations.int number_of_humps, a, b, c; doubleage_of_camel, x, y; char sex_of_camel; boolis_the_camel_married; string _name_of_camel;

Page 17: Discretemaths Book

Variables and Constants 9

22 Example The following are examples of variable declarations withinitialisations.int number_of_humps = 3, a = -18, b = 018, c =

0xA32; double age_of_camel = 22.4, x = 1.e22, y = -1.2E-3;char sex_of_camel = ’M’; bool is_the_camel_married =

true; string _name_of_camel = ”Kabubi”;

! A variable may not be declared multiple times in the same block.Thus the following fragment will produce a compiler error.

int var_a;int var_a, var_b; //ERROR multiple declaration of var_a

double var_b //ERROR multiple declaration of var_b

1.3.2 Aggregate Types

23 Definition A 1-dimensional array is an aggregate of homogeneoustypes. A 1-dimensional array is declared with the following syntax:type identifier[size];

Here size must be a positive integral constant.

! Array elements in C++ are indexed from 0.

24 Example The elements of the arrayint a[3];

are a[0], a[1], and a[2].

! C++ does not check for array bounds. Invoking the non-existent “ele-ments” a[3], a[4], say, of the array in example 24 will result in an error.

25 Example Here is an example of an initialisation of a 1-dimensionalarray:int a[4] = {2, 3, 4, 5};

Page 18: Discretemaths Book

10 Chapter 1

Thus a[0] is 2, a[1] is 3, a[2] is 4, and a[3] is 5.

26 Example If we come short of initialisers, then the array will be paddedwith zeros. Thus inint a[4] = {2, 3};

a[0] is 2, a[1] is 3, a[2] is 0, and a[3] is 0. However, if we put moreinitialisers than the size of the array, we will get an error.

27 Example The second form of initialisation allows us to drop the size ofthe array in an initialisation. Thus, the codeint a[2] = {2, 3};

is equivalent to the codeint a[] = {2, 3};

! We may not omit the size of the array if we are simply declaring thearray. Thusint a[]; //ERROR

will result in error. The compiler does not know how much space to allocateto the array.

28 Definition A 2-dimensional array is declared with the following syntax:type identifier[size_1][size_2];

Here size_1 size_2 must be positive integral constants.

29 Example The elements of the arrayint b[2][3]; are b[0][0], b[0][1], b[0][2], b[1][0]—, b[1][1], b[1][2].

30 Example Here is an example of an initialisation of a 2-dimensionalarray:int b[3][5] = {

{1, 2, 3, 4,5}, //initialises the first row

{6, 7, 8, 9, 10}, //initialises the second row

Page 19: Discretemaths Book

Variables and Constants 11

{11, 12, 13, 14, 15} //initialises the third row

};

Hence, say, b[0][1] is 2, b[1][1] is 7 and b[2][4] is 15.

! If we initialise all the elements of the array, we may omit the internalbraces. Thus the above initialisation is equivalent toint b[3][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14, 15};

The first method of initialisation, however, is preferable.

31 Example Ifint t[2][3] = {{1, 2}, {3}};

then t[0][2], t[1][1], t[1][2] are all 0.

If the braces delimiting the rows are omitted, a totally different answer maybe obtained.

32 Example Inint t[2][3] = {1, 2, 3};

t[0][0] is 1, t[0][1] is 2, t[0][2] is 3, and t[1][0], t[0][1], t[0][2]

are all 0.

! To prevent any kind of ambiguity from ensuing, prefer the initialisationstyle which includes all the braces.

33 Definition A structure is an aggregate of heterogeneous types. Thedeclaration of a structure has syntaxstruct struct_tag {type_1 identifier_1;

type_2 identifier_2;.

.

.

Page 20: Discretemaths Book

12 Chapter 1

type_N identifier_N;

}s_instance_1, s_instance_2, ..., s_instance_M;

It is not necessary to attach a tag to the structure.

34 Example Here is an example of a structure declarationstruct Prince { string country;

int rank;string name;double dollar_worth;

} Charles, Abdullah, Cuahtemoc;

35 Example Here is an example of an tagless structure declarationstruct {string last_name;

char sex;int age;double income;

} Peter, Paul, Mary;

We initialise a given structure the way we initialise arrays, thus forinstancePaul = {”Whatever”, ’M’, 66, 75001.03};

To access individual fields of a given structure we use the dot . operator.Thus for instance, we can provide the following initialisations in the abovestructure.Peter.last_name = ”Seager”; Peter.sex = ’M’;

Mary.last_name =”I_dont_remember”; Mary.sex = ’F’;

36 Example We do not need to declare all instances of a given structurewhen the structure is declared. Instance declaration can be postponed to

Page 21: Discretemaths Book

Variables and Constants 13

later in the code.struct terrorist{

...

};

. . . struct terrorist Osama_and_his_Momma,Ramzi_Rabid_Dog_Yousef;

1.3.3 Constants

37 Definition Constants are tokens representing fixed numeric or charac-ter values throughout the life of a programme.

C++ constants may be anonymous constants (without identifiers) or con-stants with identifiers.

38 Example The following are anonymous constants.12 //int 1.2 //double ’A’ //char "A"

//string 012 //octal int 0x12 //hexadecimal int 1.2e3

//double

A constant with identifier must be declared and initialised at declarationtime. A constant declaration is a statement of the formconst type identifier = fixed_value;

39 Example The following are valid constant declarationsconst double conversion_factor = 2.2; const intweeks_in_a_year = 52; const char sex_of_camel = ’M’; const stringmiddle_name = ”Anthony”;

! We cannot declare a constant and provide a value for it afterwards.The following code is illegal.const double conversion_factor; conversion_factor = 2.2;//ERROR illegal code

Page 22: Discretemaths Book

14 Chapter 1

1.4 Input and Output

We will make use of the stream cout for output and the stream cin forinput. These are not commands of C++ properly, they belong to a specialfile (the iostream header file).

40 Definition The syntax of the output stream is as follows:cout << stuff_to_output ;

! The arrowheads << point towards cout, suggesting that stuff_to_outputis going “out.”

41 Example If we want to output the stringCamels of the World: Unite!

we typecout << ”Camels of the World: Unite!”;

42 Example If we want to output the two separate linesCamels of the World: Unite!

we typecout << ”Camels of the World:\nUnite!”;

43 Example The output of the fragmentint a = 5, b = 1; cout << ”The sum a + b ” << a + b <<

”is.”;

isThe sum of a + b 6is.

Notice that there is no space between the “6” and the “i”. Notice thatwe did not need quotes around a +b—, since we wanted the computer tocalculate this sum, rather than to output the string a + b.

44 Example A double quote “ character must have a matching doublequote character on the same line. Unmatched double quotes produce

Page 23: Discretemaths Book

Operators 15

errors.cout << ”Camels of the World: //ERROR.

Unite!”; //Unmatched double quote.

45 Definition The syntax of the input stream is as follows:cin >> stuff_to_input ;

Any data to be input must first be declared.

! The arrowheads >> point towards stuff_to_input, suggesting thatwhat goes “in” is the stuff_to_input.

46 Example The fragmentint a; string b; cout << ”Enter your number of camels: ”;cin >> a; cout << ”Enter the name of your favourite camel: ”; cin>> b;

asks the user for an integer variable to be input (which we enter through,say, the keyboard, and then press ENTER), and then for a string of charac-ters to be input. The computer does not know that data has been entereduntil the ENTER key is pressed.

47 Example The code fragmentint a; cout << ”Enter your number of camels: ”; cin >> a;cout << ”Enter the name of your favourite camel: ”; cin >> b;

will cause a compiler error. The variable b is called for input, but it has notbeen declared.

1.5 Operators

48 Definition An operator is a character, or string of characters, used toperform an action on some entities. These entities are called the operands.

Page 24: Discretemaths Book

16 Chapter 1

49 Definition The priority or precedence of an operator is the order bywhich it is applied to its operands. Parentheses ( ) are usually used tocoerce precedence among operators. When two or more operators ofthe same precedence are in an expression, we define the associativityto be the order which determines which of the operators will be executedfirst. Left-associative operators are executed from left to right and right-associative operators are executed from right to left.

There are 55 operators in ISO C++, with 18 layers of precedence (17 is thehighest level of precedence, 0 the lowest). Table 1.2 lists some of them.

We will now discuss the operators which we will most use.

1.5.1 Unary Operators

These are the the unary + (plus) sign and the unary − (minus) sign. Theyare used to indicate the sign of an integer, a floating point quantity, or theexponent of the power of 10 in a floating point quantity.

1.5.2 Arithmetical Binary Operators

The usual binary operations of arithmetic may be performed on integersand floating point numbers: addition, subtraction, multiplication and divi-sion have the symbols + − ∗ /, and have algebraic precedence rules. Asexpected, division by 0 will cause an error.

Integral division, however, truncates the decimal part of the quotient.For example, 16/3 yields 5 and 20/3 yields 6. If at least one of theoperands is negative, the result is compiler-dependent. Some compilerschoose a/b == ba/bc whilst others choose a/b == da/be. For example,on some compilers 16/ − 3 == −6 and on others, 16/ − 3 == −5. If wewish to obtain a decimal value for 16/3 then we must cast at least one ofthe numbers into a double.16.0/3 // or else 16/3.0 // or

else 16.0/3.0 // or else double(16)/3 // or

else 16/double(3) // or else double(16)/double(3)

Page 25: Discretemaths Book

Operators 17

Symbol Meaning Precedence Associativity[] Array Index 16 Left(type) type casting 16 Leftsizeof size in bytes 16 Left++ Post-increment 16 Right-- Post-decrement 16 Right++ Pre-increment 15 Right-- Pre-decrement 15 Right- Unary Minus 15 Right+ Unary Plus 15 Right! Boolean NOT 15 Right* Contents of 15 Right& Address of 15 Right* Product 13 Left/ Division 13 Left% Remainder 13 Left+ Addition 12 Left- Subtraction 12 Left< Less than 10 Left<= Less than or equal 10 Left> Greater than 10 Left>= Greater than or equal 10 Left== Equal to 9 Left!= Not equal to 9 Left&& Boolean AND 5 Left|| Boolean OR 4 Left? : Conditional 3 Right= Assignment 2 Right+= Sum Assignment 2 Right*= Product Assignment 2 Right-= Difference Assignment 2 Right/= Quotient Assignment 2 Right%= Remainder Assignment 2 Right, Comma Operator 0 Left

Table 1.2: Some C++ Operators.

Page 26: Discretemaths Book

18 Chapter 1

50 Example An integer variable initialised as a float, will truncate thedecimal part, losing this information forever. The fragmentint a = 3.14;

cout << ”a = ”<< a<< ’\n’ ;

float b;b = a - 0.14;cout << ”b = ”

<< b;

will printa = 3 b = 2.86

There is a further operand on integers: % (read “mod” or “modulo”). Herea % b gives the remainder of a upon division by b. Again, if at least one of

the operands is negative, the result is compiler dependent, but we alwayshave

(a/b) ∗ b + a%b is identical to a.

The % operator has the same level of precedence as multiplication or divi-sion, and it is left-associative.

51 Example 17 % 7 yields 3 since 17 = 2 ∗ 7 + 3. 17 % 5 yields 2 since17 = 3 ∗ 5 + 2. 17 % 18 yields 17 since 17 = 0 ∗ 18 + 17.

52 Example What will the fragmentint a;a = 5 * 30 / 4 % 7 + 17 % 4 * 2;cout << ”a = ” << a;

print?

Solution: Simplify each term:

5 ∗ 30/4%7 = (5 ∗ 30)/4%7 = (150/4)%7 = 37%7 = 2

and17%4 ∗ 2 = (17%4) ∗ 2 = 1 ∗ 2 = 2.

Page 27: Discretemaths Book

Operators 19

Therefore the fragment will print a = 4.

1.5.3 Assignment Operators

53 Definition The assignment operator = is used to indicate that what is tothe left of it acquires the value to the right of it.

54 Example The fragmentint a = 4; cout << ”a = ” << a << ”.”; a = a + 19; cout <<”\na = ” << a << ” now.”;

will printa = 4. a = 23 now.

55 Example The fragmentint a = 4; cout << ”a = ” << a << ”.”; a = a + 19; cout <<”a = ” << a << ”.”; a = 2*a - 1; cout << ”a = ” << a << ”.”;

will printa = 4. a = 23. a = 45.

! The assignment operator = does not test for equality. To test for equal-ity, C++ uses the equality comparison operator ==.

56 Example The following code exchanges two declared variables.temporary_variable = first_variable; first_variable =

second_variable; second_variable = temporary_variable;

! The following code fragment will not work. Why?

// False swapping: will not work!

x = y;y = x;

Page 28: Discretemaths Book

20 Chapter 1

57 Example The following code fragment swaps variables a and b withoutthe need to introduce a temporary variable

a = a + b;b = a - b;a = a - b;

58 Definition An lvalue (left-value) is a storage area (memory location)bound to a variable during the programme execution.

59 Definition An rvalue (right-value) is the encoded value stored in the lo-cation associated with the variable.

60 Example Algebraic combinations of variables containing operatorscannot occur on the left hand side of an assignment operator, i.e., are notlvalues. Thusint x = 4, y; //OK, x and y are lvalues x + 1 = 5;

//ERROR, x + 1 is not an lvalue x + y = x //ERROR, x + y is

not an lvalue

61 Example The = operator has right associativity, meaning that in astatement containing several = operators, we start by reading therightmost one. The following multiple assignment assigns the constant 3to each of i, j, k.int i = 1, j = 2, k = 4; // i is 1, j is 2, k is 4 i =

j = k = 3; //Now i, j, k are all 3.

62 Example Multiple assignments, however, cannot occur in declarations.The following code is incorrect.int i = j = k = 3; //ERROR, multiple assignment in

declaration.

The correct declaration is

Page 29: Discretemaths Book

Operators 21

int i = 3, j = 3, k = 3;

C++ has several combined assignment operators:a += expression; // means a = a + expression; a -=

expression; // means a = a - expression; a *= expression; // means

a = a * expression; a /= expression; // means a = a / expression;

a %= expression; // means a = a % expression;

! There is no whitespace between the two consecutive characters form-ing combined assignment operators.

63 Example The value of k changes as indicated in the commented code.int k = 9; k += 10; // k becomes 19 k -=

3; // k becomes 16 k /= 8; // k becomes 2

64 Example What is the value of x after executing the following piece ofcode?int x = 1, y = 3, z = 5; x += y -= z *= -2;

Solution: Since assignment operators are right-associative, first, the valueof z becomes z == 5 ∗ (−2) == −10. Next, y == 3 − (−10) == 13. Finally,x == 1 + 13 == 14.

65 Example Though a variable may not be declared more than once inany given block, the same identifier for a variable can be used in differentblocks. If two blocks are nested, a new declaration in the inner blockhides the declaration in the outer block. If two blocks are disjoint,declarations are independent of one another. In the following threeblocks, there are three declarations of the variable a.{//opens block I int a = 1; //the a of block I

{//opens block II

int a = 2; //hides the a in block I

a *= 8; //the a in block II now becomes 16

}//closes block II

a += 4; //the a in block I becomes 5

Page 30: Discretemaths Book

22 Chapter 1

{//opens block III

a += 2;//the a in block I is visible, and becomes 7

int a = -9;//hides the a’s in blocks I and II

a *= 2;//the a in block III now becomes -18

}//closes block III

a -= 8; //the a in block I becomes -1. }//closes block I

Two rather peculiar operators of C++ are the ++ pre-increment/post-increment and the −− pre-decrement/postdecrement operators.

The post-increment and post-decrement operators work as follows. Sup-pose x is a numerical real number variable and x++ (or x−− ) is encoun-tered. Then the operation where x++ (or x−−) was involved is performedfirst, and upon leaving the statement, the value of x is increased (de-creased in the case of −−) by 1. These operators have higher precedencethan the arithmetic operators.

66 Example Find the values of a, b, c, and d after execution of thefollowing code.int a, b , c = 3, d = -7; a = c++ * d ; // line 1 b =

(c++) + d++; //line 2 a *= a++; //line 3

Solution: On line 1, c == 3, d == −7 and so a == −21. Observe that cnow is incremented to 4 upon leaving the statement. On line 2, c == 4 andd == −7, so b == −3. Observe that upon leaving line 3, c == 5 and d ==

−6. On line 3, a == −21, and it becomes a == (−21)∗(−21) == 441. a isincremented upon leaving the statement, and finally, a == 442. Thereforea == 442, b == −3, c == 5, d == −6.

67 Example Post-increment operators may not be on the left hand side ofan assignment operator, i.e., are not lvalues. The following code willproduce a compiler error.int a = 8; a++ *= 9; // ERROR

This is because the above is interpreted as a + 1 *= 9;, and a + 1 is nota memory location. Notice that the written expression is not equivalent to

int a = 8;

Page 31: Discretemaths Book

Operators 23

a *= 9;a++;

which is a perfectly legitimate expression.

68 Example Similarly, an expression like 3++; will produce a compiler er-ror.

69 Example Oddly enough, pre-increment operators are lvalues. An ex-pression like ++a *= 9; is interpreted as ++a; a *= 9;.

70 Example Find the final value of a after execution of the fragmentint a = 8; (++a)++;

Solution: First the value of a is increased to 9, and upon leaving the state-ment is increased to 10. Thus a == 10.

71 Example The following code will produce a compiler error. Why?int a = 8; ++a++;

Solution: Since the post-increment operator has higher precedence thanthe pre-increment operator, the fragment ++a++ is equivalent to ++(a++).This means that this code is equivalent to the following.int a = 8; a++ = (a++) + 1; Since post-increment operators cannot beon the left hand side of an assignment, the compiler will produce an error.

C++ code can get really perverse due to the fact that most of timeswhite space is ignored. Consider the following example.

72 Example Find the values of a, b, c after execution of the fragment inta, b = 2, c = 9; a = b+++c;

Solution: We have three + signs in a row. Thus there is an incrementoperator and an addition (and why not an increment operator and a unary+?). Is the increment operator pre-increment or post-increment? That is,do we have (b++) + c or b + (++c)? The compiler will choose (b++) +c, processing as many characters as can possibly be combined into anoperator token. So we end up with a == 11, b == 3, c == 9.

Page 32: Discretemaths Book

24 Chapter 1

! C++ grammar does not define the order of evaluations of expressionsinvolving multiple increment and decrement operators, and hence, the usein expressions like the following should be avoided.int x, n = 6; x = ++n * (--n) + ++n; //UGLY! avoid writing

code like this

1.5.4 Relational Operators

The relational operators <,<=, >,>=,==, ! = evaluate to 1 if the relationis true and to 0 if the relation is false. The operator != is read “not equalto.”

73 Example In the fragment

int x = 1, y = 4, z, w, a;z = x < y ;

w = (x + 5) <= y;a = y != 5;

z becomes 1 since x < y is true. w becomes 0 since (x + 5) < = y isfalse. Finally, a becomes 1 since y!=5 is true.

1.5.5 Logical Operators

The logical negation operator ! gives output according to the followingrules:

!1 gives 0 !0 gives 1

The logical AND operator && gives output according to the following rules:

1 && 1 gives 1 1 && 0 gives 0 0 && 1 gives 0 0 && 0 gives 0

Notice the similarity between this operator and regular multiplication by 0.The logical OR operator || gives output according to the following

rules:

1 || 1 gives 1 1 || 0 gives 1 0 || 1 gives 1 0 || 0 gives 0

Page 33: Discretemaths Book

Operators 25

Notice the similarity between this operator and regular addition to 0.A value different from 0 will be interpreted as 1 by these operators.

74 Example The fragment of codeint a = -1, b = 3, x, y, z, w, r, s; x = a && b; y = (a +

1) || b; z = (a + 1) && (b - 3); w = (a!=b) && (a <= 0); r = (a <

b) || (y==0); s = !b;

will produce x==1, y==1, z==0, w==1, r==1, and s==0.

1.5.6 Conditional Operator

C++’s only ternary operator is the conditional operator ?: which has thefollowing syntax

test ? alternative_1 : alternative_2

and evaluates as follows. test is evaluated first. If it is true, then the wholeexpression becomes alternative_1, and alternative_2 is not evaluatedat all. If test is false, alternative_1 is skipped, and the whole expressionbecomes alternative_2.

75 Example In the fragment

int a = 3, b = 6, c;c = (a==b) ? (a + 1) : (b - 8);

c becomes -2, since a==b is false and so the conditional expression as-sumes the second alternative b-8.

76 Example The fragmentint_1 >= int_2 ? int_1 : int_2gives the maximum of int_1 and int_2.

Page 34: Discretemaths Book

26 Chapter 1

1.5.7 Comma Operator

The comma , operator takes its arguments and evaluates them from leftto right and returns the value of the rightmost expression.

77 Example In the fragment

int a = 1, b;b = (a += 1, a += 2, a + 5);

the comma operator first evaluates a+=1 which makes a == 2. The nextexpression a+=2 is evaluated and so a == 4. Finally, the last expressiona + 5 is evaluated becoming a + 5 == 9. Hence b == 9.

1.6 Pointers

C++ allows the address of a variable to be accessed through the contentsof another variable.

78 Definition A pointer is a variable which contains the address of anothervariable.

Pointers are declared by means of the following syntax:type *identifier;

This creates a variable, identifier, which holds the address (“points to”)of another variable of the same type.

! The above declaration is equivalent to the following, where the * iscloser to the type rather than identifier:type* identifier;

and to the following, where there is whitespace around *:type * identifier;

Page 35: Discretemaths Book

Pointers 27

We will prefer the first way of declaring pointers, as the latter two formssuggests to some, quite erroneously, that the * distributes over theidentifiers. In fact, if more than one pointer is declared on the samestatement, each identifier must be given an *. For example, intype *identifier_1, identifier_2; type *identifier_3,*identifier_4;

identifier_2 is not a pointer, but both identifier_3 and identifier_4,are pointers.

When a pointer is declared of a given type, its initialisation is with theaddress of a previously declared variable of the same type. Pointers areinitialised as follows:type identifier_2; ...... ...... type *identifier_1 =

&identifier_2;

If a pointer is first declared and then an address assigned to it, thesyntax is as follows:

type *identifier _1, identifier_2;identifier_1 = &identifier_2;

! Memory locations are allocated at compile time, and thus are fixed.Therefore, a variable which is not declared as a pointer cannot be given theaddress of another variable, since a variable may be modified.

79 Example There are some mistakes in the following code: float a, *b= &a, *c, d; // OK, b points to an existing

object a int *k = &j, j, q;//ERROR, k is pointing to something

undeclared char *x, y, z; c = &d; //OK, *c and d are of the same

type. *x = &q; //ERROR, *x and q are not of the same type y = &z;

//ERROR, a non-pointer variable cannot hold an address

If a variable points to a second variable, we may access the contents ofthe second variable indirectly, by means of the dereference (“contents of”)operator asterisk *.

Page 36: Discretemaths Book

28 Chapter 1

80 Example Consider the following code.int x, *y = &x; // y holds the address of x x = 10;

// *y == x == 10 *y += *y; // x == *y == 20

81 Example What is the error with the following piece of code?float *p ; *p = 2.2;

Solution: p has not been initialised, it is not pointing to any variable, andhence, it cannot modify the contents of this variable by inserting the value2.2.

1.7 Functions

Fragments of code that need to be recycled can be put into functions.In mathematics, a function is composed of 5 elements: (1) a name, (2) arule, (3) the name of a typical input, (4) a domain, and (5) a target set.Thus a function has the form

f :A → B

x 7→ f(x),

where f is the name of the function, x the name of a typical input,x 7→ f(x) the assignment rule, A is the domain, and B is the target set(range). In C++ an analogous situation arises. The declaration of afunction f has the form

target_set f(domain).

The domains and target set must come from an available type.

82 Definition A function declaration has the formtype function_identifier(type var_1, type var_2, ..., type

var_N);

83 Example An example of a function declaration isdouble Area_of_Triangle(double s_1, double s_2, double

s_3);

Page 37: Discretemaths Book

Functions 29

The same can also be accomplished with the declarationdouble Area_of_Triangle(double , double , double );

the parameters s_1, s_2, s_3, being dummy.

84 Example Another example of a function declaration (with initialisation)isbool g_camel(string name = ”Kabubi”, double height, int

humps = 1);

85 Example A perfectly legal function isvoid nothing();

which receives no input and returns no output.

86 Definition A function definition has syntaxtype function_identifier(type var_1, type var_2, ..., type

var_N)

{

body_of_functionreturn some_value ;

}

The return is optional.

87 Example Here is a function which determines the maximum of threeintegers a, b, c.

int maximum_of_3(int a, int b, int c){

return (a >= b ? a : b) >= c ? (a >= b ? a : b) : c;}

88 Example Here is a function which determines whether three givenlengths can form a trianglebool is_triangle(double a, double b, double c)

{

Page 38: Discretemaths Book

30 Chapter 1

return !(a + b <= c || a + c <= b || b + c <= a);}

It returns 1 (true) if the lengths do form a triangle and 0 (false) otherwise.

89 Example Here is a function which finds the area of a triangle uponbeen fed the lengths a, b, c, of its three sides.double Area_of_Triangle(double a, double b, double c) {

return .25*sqrt((a + b + c)*(b + c - a)*(c + a - b)*(a + b - c));}

It uses Heron’s formula

Area =√

s(s − a)(s − b)(s − c) =1

4

(a + b + c)(b + c − a)(c + a − b)(a + b − c),

wheres =

a + b + c

2

is the semi-perimeter of the triangle. It also uses the square root functionsqrt(), which is found in the cmath header file.

90 Example Here is a function which prompts the user to enter the radiusof a circlevoid prompt_radius()

{

cout << ”Please enter the radius of a circle: ”;}

91 Example Here is a function which reads the radius of a circle enteredby a userdouble read_radius()

{ double r;cin >> r;return r;}

Page 39: Discretemaths Book

Functions 31

92 Example Here is a function which finds the area of a circle upon beenfed its radiusdouble Area_of_Circle(double radius)

{

const double PI = 3.14159;return PI*radius*radius;}

93 Example Here is a function which prints the area of a circle upon beenfed with it.void print_area_of_circle(double Area)

{

cout << ”Area of circle is ” << Area;}

1.7.1 main() Function

All C++ programmes must have a function main(){}. Its definition isint main(int argc, char *argv[])

{

body

return 0;}

The value 0 returned by main(){} means that everything has gone well.The optional programme parameters argc and *argv[] are run from thecommand line.

94 Example The smallest syntactically correct C++ programme, isint main()

{

return 0;}

Page 40: Discretemaths Book

32 Chapter 1

This is a completely useless programme, as it does nothing.

1.7.2 Function Calls

Functions called within the body of main(){} must be declared beforethey are called. This can be accomplished in two ways.

• We can define the functions before main(){}.

• We can declare the functions before main(){} and then define themafter main(){}.

An example is in order. Suppose we want to write a programme that callsthe user to input the radius of a circle and then outputs the area of thecircle (in square units). We will use the functions of examples 90 through93 in order to illustrate both

95 Example Here is the programme, using the first style.void prompt_radius() {

cout << ”Please enter the radius of a circle: ”;} double read_radius() {

double r;cin >> r;return r;

} double Area_of_Circle(double radius) {

const double PI = 3.14159;return PI*radius*radius;

} void print_area_of_circle(double Area) {

cout << ”Area of circle is ” << Area;} int main() {

prompt_radius();print_area_of_circle(Area_of_Circle(read_radius()));return 0;

}

96 Example The second style yields the following code.void prompt_radius(); double Area_of_Circle(double

Page 41: Discretemaths Book

Functions 33

radius); double read_radius(); double Area_of_Circle(doubleradius); void print_area_of_circle(double Area); int main() {

prompt_radius();print_area_of_circle(Area_of_Circle(read_radius()));return 0;

} void prompt_radius() {

cout << ”Please enter the radius of a circle: ”;} double read_radius() {

double r;cin >> r;return r;

} double Area_of_Circle(double radius) {

const double PI = 3.14159;return PI*radius*radius;

} void print_area_of_circle(double Area) {

cout << ”Area of circle is ” << Area;}

For the (tiny) C++ programmes that we will see in these notes we willprefer the first style of writing programmes.

97 Example The fragmentint foo(int a){return 2*a;} int main() { int b, c = 7; b =

foo(c); cout << ”b = ” << b << ” and c = ” << c; return 0; }

will print b = 14 and c = 7.

98 Example The scope rules seen in example 65 hold within the bodies offunctions. The fragmentint a = 17, b = -3; int foo(int c)

{

int b;b = 2*c + a;cout << ”II: within foo, a = ” << a << ”, b = ”

<< b << ”, and c = ” << c << ’\n’;return b;}

Page 42: Discretemaths Book

34 Chapter 1

void faa(void){

int c = b*a;cout << ”III: within faa, a = ” << a << ”, b = ”<< b << ”, and c = ” << c << ’\n’;}

int main() { int a = -5, c = 7; a = foo(c); cout <<”I: withinmain, a = ” << a << ”, b = ”

<< b << ”, and c = ” << c << ’\n’;faa(); return 0; }

will printII: within foo, a = 17, b = 31, and c = 7 I: within main,

a = 31, b = -3, and c = 7 III: within faa, a = 17, b = -3, and c =

-51

1.7.3 Parameter Passing

In example 65 we saw how blocks can hide information from other blocks.Thus if a variable is to be processes by different functions, it must beexternal to these functions and it must be declared before these functionsare defined.

99 Definition When a variable is passed to a function, a new copy of theoriginal variable is made and it is this copy what the function processes.The original value is unaffected. This is called passing by value.

100 Example The fragmentint f(int a){a += a; return a;} int main() {

int i = 3;cout << ”f(i) = ”<< f(i) << ” and i = ” << i;return 0;

}

Page 43: Discretemaths Book

Functions 35

will print f(i) = 6 and i = 3, that is, even though the value of i sufferedchanges within f(), it was not altered by f().

We see then that simply passing the value of a variable to a function doesnot change the contents of the variable. In order to change the contentsof the variable, we must pass the address of the variable rather than itcontents. This is called passing by reference.

101 Example The fragmentint foo(int a) { a = 5;

cout << ”Within foo, a = ” << a << ’\n’;return 0;

} int faa(int *x) { *x = 5;cout << ”Within faa, a = ” << *x << ’\n’;return 0;

} int main() {

int a = 18;cout << ”Printing I in main, a = ” << a << ’\n’;foo(a);cout << ”Printing II in main, a = ” << a << ’\n’;faa(&a);cout << ”Printing III in main, a = ” << a << ’\n’;return 0;

}

will printPrinting I in main, a = 18 Within foo, a = 5 Printing II

in main, a = 18 Within faa, a = 5 Printing III in main, a = 5

102 Example The code following code fragment contains two functionswhich purportedly exchange the values of two variables.int swap_bad(int x, int y) {

int temp;temp = x;x = y;y = temp;

Page 44: Discretemaths Book

36 Chapter 1

cout << ”Within swap_bad first_var = ” << x << ” and second_var= ” << y << ’\n’;

return 0;} int swap_good(int *x, int *y) {

int temp;temp = *x;*x = *y;*y = temp;cout << ”Within swap_good first_var = ” << *x << ” and

second_var = ” << *y << ’\n’;return 0;

} int main() {

int first_var = 1, second_var = 2;cout << ”I: in main first_var = ”

<< first_var<< ” and second_var = ”<< second_var << ’\n’;

swap_bad(first_var, second_var);cout << ”II: in main first_var = ”<< first_var<< ” and second_var = ”<< second_var << ’\n’;swap_good(&first_var, &second_var);cout << ”III: in main first_var = ”

<< first_var<< ” and second_var = ”<< second_var << ’\n’;

return 0;}

will printI: In main first_var = 1 and second_var = 2 Within

swap_bad first_var = 2 and second_var = 1 II: In main first_var =

1 and second_var = 2 Within swap_good first_var = 2 and second_var= 1 III: In main first_var = 2 and second_var = 1

Page 45: Discretemaths Book

if{}else{} Statement 37

1.8 if{}else{} Statement

103 Definition The if...else control statement has the following syntaxif (expression)

{statement_1;}else

{statement_2;}

and evaluates as follows. If expression is true then statement_1 isexecuted. Otherwise statement_2 is executed.

104 Example In the following programme segmentint a = 3;if (a < 1 || a > 4)

b = 5;else

b = 4;

the value of b will be 4, since the test produces a true result.

105 Example The following programme segmentint a = 0;

if (a=0)cout << ”Aha!”;

will not print anything. a=0 is not a test, it is an assignment, and so this isequivalent to if(0). The programmer perhaps meant to write

int a = 0;if (a==0)

cout << ”Aha!”;

This is a fairly common programming error. Again, the programmesegment

int a = 45;if (0 < a < 5) //ERROR

Page 46: Discretemaths Book

38 Chapter 1

cout << ”The number is in the right range.”;elsecout << ”The number is not in the right range.”;

will erroneously print The number is in the right range. This isbecause the compiler will first evaluate 0<a giving 1, then it will evaluate1< 5 giving 1 again. Thus the test evaluates to true. The intended code israther

int a = 45;if (0 < a && a < 5)cout << ”The number is in the right range.”;elsecout << ”The number is not in the right range.”;

106 Example The logical AND and the logical OR have a short-circuitingproperty that it is at times rather useful. If the first argument in a && b is0, then b is not evaluated and the output of a && b is 0 no matter what bmight be. Similarly, if the first argument in a || b is 1, then b is notevaluated and the output of a || b is 1 no matter what b might be. Forexample, the programme segment

int a = -3, b = 0;if ((a + 3) && a/b)

will not cause division by 0, as the test will be short-circuited because a+3

evaluates to 0.

107 Example Care must be exercised with the grouping. C++ ignoresindentation. The fragment

int a = 4, b = 5;if (a < 3)

b = 7;a = a + b;

cout << ”a = ” << a;

Page 47: Discretemaths Book

if{}else{} Statement 39

will print a = 9, whereas the fragmentint a = 4, b = 5;if (a < 3)

{b = 7;a = a + b;}

cout << ”a = ” << a;

will print a = 4.

! In the case of nested if...else constructs, an else matches themost recent if in the same block that does not have its own else, unlesscoerced by means of braces to do otherwise.

108 Example What is the value of c at the end of the following codefragment?

int a=0,b=1,c=2;if(a) if(b) c=3;else c=4;

Solution: First notice that both ifs are in the same block and hence theelse belongs to the nearest if, that is, to if(c) k=3;. Thus this fragmentcan be written as

int a=0,b=1,c=2;if(a) {if(b) c=3; else c=4;}

Now the test in if(a) is false, hence control is passed after the brace.This means that c has not changed at all, and hence c==2.

109 Example What is the value of c at the end of the following codefragment?

int a=0,b=1,c=2;if(a) {if(b) c=3;}else c=4;

Page 48: Discretemaths Book

40 Chapter 1

Solution: In this case the else does not to belong to the same block asif(b), but it is in the same block as if(a), and so it belongs to it. Thetest in if(a) evaluates to false and hence control is passed to the else.This makes c==4.

1.9 switch{} Statement

C++ provides a way to avoid repeated if...else statements.

110 Definition The switch statement has the syntaxswitch(condition){

case constant_1: option_1; break;case constant_2: option_2; break;.

.

.

case constant_N: option_N; break;default : default_option; break;}

Here condition must be of int or char type, or a related type that can beconverted into one of these two. The cases must all be different. If one ofthe cases is met, its option is executed and the control is passed to theclosing brace. If break; does not appear in a case, the next case will beexecuted. The default statement is optional, and only one default

statement is allowed.

111 Example The fragmentint a = 2, b = 2;switch(b){

case 1: a = -1; break;case 2: a = 0; break;case 3: a = 1; break;}

cout << ”a = ” << a;

Page 49: Discretemaths Book

switch{} Statement 41

will print a = 0.

112 Example The fragmentint a = 2, b = 2;switch(b){

case 1: a = -1; break;case 2: a = 0;case 3: a = 1; break;}

cout << ”a = ” << a;

will print a = 1. This is because case 2: is met, it assigns 0 to a. Sincethere is no break; control is passed to the next case, which assigns 1 toa.

113 Example The fragmentint a = 2, b = 0;switch(b){

case 1: a = -1; break;case 2: a = 0; break;case 3: a = 1; break;}

cout << ”a = ” << a;

will print a = 2, as there is no case for b equaling 0, so the body of theswitch statement does not modify a.

114 Example The fragmentint a = 2, b = 0;switch(b){

case 1: a = -1; break;case 2: a = 0; break;case 3: a = 1; break;

Page 50: Discretemaths Book

42 Chapter 1

default: a = 27;}

cout << ”a = ” << a;

will print a = 27.

115 Example The fragmentint a = 3, b = 1, c = 1;switch(c){

case (a - b): a = -1; break;case 3: a = 1; break;default: a = 27;}

cout << ”a = ” << a;

is incorrect. This is because a - b is not a constant.

116 Example The fragmentint a = 3, b = 2,;switch(b){

case (1||2): a = -1; break;case 3: a = 1; break;default: a = 27;}

cout << ”a = ” << a;

will print a = 27. This is because 1||2 evaluates to 1.

1.10 for(){} Loop

117 Definition The for() loop has syntaxfor(initial_condition; test_condition ; loop_changes )

{body of loop}

Page 51: Discretemaths Book

for(){} Loop 43

Any of the parts in a for loop may be omitted. Here we start with the initialcondition. It if meets the test condition, the body of the loop is executedand then the loop changes. This procedure goes on until the testcondition evaluates to false.

118 Example The code

int a = 2, b = 5, i;for(i = 3; i < 8; i += 2, b--)

{//opens body of for

a += b;cout << ”a is ”

<< a<< ”, b is ”<< b << ”, and i is ”<< i << ’\n’;

}//closes body of for

will printa is 7, b is 5, and i is 3 a is 11, b is 4, and i is 5 a

is 14, b is 3, and i is 7We start with i == 3. We test whether i < 8, which is true. The body ofthe loop is executed and we get a == 7. The loop changes are executedand we get i == 5 and b == 4. We test again i < 8, which is true. Thebody of the loop is executed and we get a == 11. The loop changes areexecuted and we get i == 7 and b == 3. We test whether i < 8, which istrue. The body of the loop is executed and we get a == 14. The loopchanges are executed and we get i == 9 and b == 2. We test whetheri < 8, which is false. We stop.

119 Example The code in example 118 could have been written asint a = 2, b = 5;for(int i = 3; i < 8; i += 2, b--)

{//opens body of for

a += b;cout << ”a is ”

<< a

Page 52: Discretemaths Book

44 Chapter 1

<< ”, b is ”<< b << ”, and i is ”<< i << ’\n’;

}//closes body of for

This fragment will execute as in 118, but there is a subtle difference. In118 the variable i is available for later use, as it is not local to the block inthe for loop. In this second case, some compilers will not make i

available for later use.

120 Example Here is a rudimentary calculation of n!.long int n;cout << ”Enter a positive integer: ” ;

cin >> n;int factorial = 1;for(int i=1; i <= n; i++) factorial = factorial*i;cout << n <<”! is ” << factorial << ’\n’;

Since these numbers grow rather fast and memory space is scarce, mysystem is only able to calculate up to 16! accurately.

1.11 while(){} Loop

121 Definition The while() loop has syntaxwhile(test){body_of_loop}

The commands in the body of the loop will be executed as long as thetest evaluates to true.

122 Example The fragmentint a = 10, b = 4;while(a - b){

a--; b++;cout << ”a is ” << a << ” and b is ” << b << ’\n’;

Page 53: Discretemaths Book

Examples of Full Programmes 45

}

will printa is 9 and b is 5 a is 8 and b is 6 a is 7 and b is 7

! Had we writtenint a = 10, b = 3;while(a - b){

a--; b++;cout << ”a is ” << a << ” and b is ” << b << ’\n’;}

we would have obtained an infinite loop. In general while(1) will producean infinite loop.

123 Example for( ; ; ) produces an infinite loop.

1.12 Examples of Full Programmes

We are now ready to write complete C++ programmes. The programmeshere are rather elementary and do not exploit the full capabilities of theC++ language. I wrote these programmes in a hurry, so if you discoverbugs, please tell me. Remember that in a cout line there must bematching double quotes. If they don’t match in the presentation here, it isbecause of the idiosyncracies of LaTeX. I will improve these programmessome time in the future.

124 Example Write a programme that reverses the digits of a positiveinteger. The programme must prompt the user for a series of integers,one integer at the time. The programme will stop when the user enters anegative integer or 0.

Solution: We use use to our advantage that division of a positive integerwill truncate the decimal part of a quotient, and so, effectively,

Page 54: Discretemaths Book

46 Chapter 1

copy_int_input/10 truncates the last digit of copy_int_input. We createthe following functions: (1) a function that prompts the user for input or tostop, (2) a function reading the input, (3) a function reversing the digits ofthe integer. The programme will then will resemble the one below.//digit_reversal.cpp

#include <iostream> using namespace std; void prompt_user()

{//opens prompt_user

cout << ”\nLet\’s reverse the digits of a positive integer.\n”;cout << ”Enter a positive integer.\n”;cout << ”Enter 0 or a negative integer to stop: ”;}//closes prompt_user

int read_input(){//opens read_input

int a;cin >> a;if(a <=0) {//opens if

cout << ”You have decided to quit. Good Bye.\n”;return 0;}//closes if

else return a;}//closes read_input

int reverse(int b){//opens reverse

int x = 0;while (b)

{//opens while

x = x *10 + b % 10; //x accumulates the truncated digit

b = b/10; //truncates a digit of the input integer

}//closes while

return x;}//closes reverse

int main(){//opens main

prompt_user();int input_var = read_input();while(input_var)

{//opens while

cout << input_var

Page 55: Discretemaths Book

Examples of Full Programmes 47

<< ” reversed is ”<< reverse(input_var);prompt_user();input_var = read_input();

}//closes while

return 0;} //closes main

The directive \#include <iostream> loads the input-output class library,where the stream cout is defined. It instructs the compiler to replace theline #include <iostream> with the contents of the header file iostream.h

Each #include directive requires its own line. For example, you cannotwrite \#include <iostream> using.The statement using namespace std; localises the name of thedirectives and functions making reference to them easier. All statementsmust end in semicolon.

125 Example Recall that a positive integer p > 1 is a prime if its onlypositive integral divisors are 1 and p itself. The series of primes goes thuslike 2, 3, 5, 7, 11, 13, 17, 19, 23, . . .. Write a programme determining whethera given integer is a prime.

Solution: Our input will only accept positive integers. Observe that 2 isthe only even prime. Thus we test the input integer to see whether it is 1 (which is neither prime nor composite), 2 (which is a prime), or an evennumber greater than 2 (in which case the number is composite). If neitherof these applies, then the number, call it p, is odd and greater than 1. Wethen divide p by every odd integer up to p − 2. If p is not divisible by anyof these integers, then p must be prime. Observe that this algorithmmakes about p/2 tests. In the Number Theory Chapter we will see thatthe same algorithm used here only requires about

√p tests.

//rudimentary_primality.cpp

#include <iostream> using namespace std; void prompt_user()

{//opens prompt_user

cout << ”\nLet\’s test the primality of a positive integer.\n”;cout << ”Enter a positive integer.\n”;cout << ”Enter 0 or a negative integer to stop: ”;

}//closes prompt_user int read_input() {//opens read_input

Page 56: Discretemaths Book

48 Chapter 1

int a;cin >> a;if(a <=0){cout << ”You have decided to quit. Good Bye.\n”;

return 0;}else if (a==1) {cout << a

<< ” is neither prime nor composite.\n”;return a;}

else if (a==2) {cout << a << ” is prime.\n”; return a;}else if (a%2 == 0) {cout << a << ” is an even

number.\n” ; return a; }

else return a;}//closes read_input void primality_test(int p) {//opens

primality_testif(p%2!=0) {// opens if(p%2!=0)

bool flag = true;for(int i=3; i < p; i += 2) if (p%i ==0) {flag = false;

break;}if (flag) cout << p << ” is prime.\n” << ’\n’;else cout << p

<< ” is not prime.”<< ” Its smallest prime factor is ”<< i << ’\n’;

}//closes if(p%2!=0)

}//closes primality_test int main() {//opens main prompt_user();

int input_var = read_input(); while(input_var){//opens while

primality_test(input_var);prompt_user();input_var = read_input();}//closes while

return 0; }//closes main

126 Example The programme#include <iostream> #include <cstdlib> #include <ctime>

using namespace std; void prompt_user() {//opens prompt_user

cout << ”\nLet\’s play PAPER, ROCK, or SCISSORS.\n”;cout << ”Enter \’P\’, \’R’, or \’S\’.\n”;cout << ”Enter \’Q\’ to stop: ”;

Page 57: Discretemaths Book

Examples of Full Programmes 49

}//closes prompt_user char user_input() {//opens user_input

char a;bool flag;do{ cin >> a;flag=false;if(!(a ==’P’ || a == ’p’ || a == ’R’ || a == ’r’ || a ==

’S’ || a == ’s’ ||

a == ’Q’ || a == ’q’)){flag = !flag; cout <<”Unrecognisedchoice. Try again.\n”; prompt_user();}

else break;}

while(flag);if(a == ’Q’ || a == ’q’ ) {cout << ”You have decided to quit.

Good Bye.\n”; exit(1);}else switch(a){case ’p’: return ’P’; break;case ’P’: return ’P’; break;case ’r’: return ’R’; break;case ’R’: return ’R’; break;case ’s’: return ’S’; break;case ’S’: return ’S’; break;}

}//closes user_input char computer_input()

{//opens computer_input

srand(time(NULL));switch((rand() % 3)){

case 0: return ’P’; break;case 1: return ’R’; break;case 2: return ’S’; break;}

}//closes computer_input

void decide_winner(char computer, char user) { if(computer==user){cout << ”Computer picked ” << user << ” too! It is a

tie!\n”;}else if ((user == ’P’ && computer == ’S’)

|| (user == ’S’ && computer== ’R’)

|| (user==’R’ && computer== ’P’) )

Page 58: Discretemaths Book

50 Chapter 1

cout << ”Computer picked ” << computer << ”against your ” << user << ”. Computer wins.”;

else cout << ”Computer picked ” << computer << ”against your ” << user << ”. User wins.”;}

int main(){//opens main

int user_choice, comp_choice;while(1){prompt_user();comp_choice = computer_input();user_choice = user_input();decide_winner(comp_choice, user_choice);}

return 0;}//closes main

simulates the game “PAPER, ROCK, SCISSORS.” The user plays againstthe computer, which prompts him for an option of these three andproduces its random choice (the computer does not cheat!). The functionrand() resides in the header file cstdlib and produces a pseudo-randominteger. In order to further randomise the computer choice, we put atime-dependent seed via the instruction srand(time(NULL));. Since weonly have three choices, we reduce num_equiv_comp modulo 3 to producea random integer in the set {0, 1, 2}.

127 Example Write a programme where the user inputs three numericalreal number coefficients a, b, and c and then solves the equationax2 + bx + c. If the equation is quadratic, it must provide for the cases

Page 59: Discretemaths Book

Examples of Full Programmes 51

when the roots are complex. If the equation is linear, it must tell the userthat it is a linear equation. If the equation is degenerate, it must tell theuser so.

Solution: One possible solution is the following.//solving_quadratics.cpp

#include <iostream>

#include <cmath>

using namespace std;int main(){ //opens main

double a, b, c, discriminant;cout << ”Enter a first coefficient: ”;cin >> a;cout << ”\nEnter a second coefficient: ”;cin >> b;cout << ”\nEnter a third coefficient: ”;cin >> c;discriminant = b*b - 4*a*c;if (a==0 && b==0)

cout << ”\nThe equation axˆ2 + bx + c = 0 isdegenerate.” << ’\n’;

else if (a==0 && b!=0){ //opens linear

cout << ”\nThe equation axˆ2 + bx + c = 0 islinear.”;

cout << ”\nIt has a unique root x = ” << -c/b <<

’\n’;} //closes linear

else{ //opens quadratic

cout << ”\nThe equation axˆ2 + bx + c = 0 isquadratic.”;

if (discriminant < 0){ //opens complex

cout << ”\nIt has two complex roots: x_1 = ”<< -b/(2*a)

Page 60: Discretemaths Book

52 Chapter 1

<< ” + i”<< sqrt(-discriminant)/(2*a);

cout << ” and x_2 = ”<< -b/(2*a)<< ” - i”<< sqrt(-discriminant)/(2*a) << ’\n’;

} //closes complex

else{ //opens real

cout << ”\nIt has two real roots: x_1 = ”<< (-b + sqrt(discriminant))/(2*a);

cout << ” and x_2 = ”<< (-b - sqrt(discriminant))/(2*a) <<

’\n’ ;

} //closes real

} //closes quadratic

return 0;} //closes main

We had to recur to the header file cmath which contains the square rootfunction. Observe that since we had to use several blocks, wecommented each brace. This is a good practice in general.

128 Example Write a programme where the user inputs three realnumbers a, b, and c and tells whether these numbers form the lengths ofa triangle. If a triangle can be formed with the given input, then it will findits area by means of Heron’s formula

Area =√

s(s − a)(s − b)(s − c),

wheres =

a + b + c

2

is the semi-perimeter of the triangle. If at a given point the user enters anon-positive number, the programme should stop then.

Solution: One possible solution is#include <iostream>

Page 61: Discretemaths Book

Examples of Full Programmes 53

#include <cmath>

using namespace std;int main()

{ //opens main

double a, b, c;cout << ”Enter a first length: ”;cin >> a;if (a<=0) {cout << ”Bad input.\n”; exit(1);}cout << ”\nEnter a second length: ”;cin >> b;if (b<=0) {cout << ”Bad input.\n”; exit(1);}cout << ”\nEnter a third length: ”;cin >> c;if (b<=0) {cout << ”Bad input.\n”; exit(1);}if (a + b <= c || a + c <= b || b + c <= a)

cout << ”Sorry, these don’t form a triangle.\n”;else

{//opens area computation

double s = (a + b + c)/2;cout << ”The area of the triangle is ”

<< sqrt(s*(s - a)*(s - b)*(s - c))<< ” square units.\n”;

} //closes area computation

return 0;} //closes main

Here the instruction exit(1) causes normal programme terminationwhen this instruction is reached.

129 Example The following programme converts a decimal hindu-arabicnumeral between 1 and 3999 to a roman numeral.

//arabic_to_roman.cpp

#include <iostream>

#include <string>

using namespace std;int main(){ //opens main

int input_int;

Page 62: Discretemaths Book

54 Chapter 1

cout << ”Enter a decimal hindu-arabic number between 1and 3999: ”;

cin >> input_int;while(input_int < 1 || input_int > 3999){ //opens input-prompting while

cout << ”Sorry, wrong input, try again.\n”;cout << ”Enter a decimal hindu-arabic number between 1

and 3999: ”;cin >> input_int;}//closes input-prompting while

int copy_input = input_int, a[] = {1000, 900, 500, 400,100, 90, 50, 40, 10, 9, 5, 4, 1}, r_position = 0;

string roman_conv = ””, r[] = {”M”, ”CM”,”D”,”CD”,”C”,”XC”,”L”,”XL”,”X”,”IX”,”V”,”IV”,”I”};for(int a_position = 0; a_position<=12; a_position++,

r_position++){while(input_int >= a[a_position]){input_int -= a[a_position];

roman_conv += r[r_position]; }}

cout << ”The roman conversion of ” << copy_input << ” is: ”<< roman_conv << ’\n’;

return 0;} //closes main

130 Example A locker room contains 100 lockers, numbered 1 through100. Initially all are open. An attendant performs a number of operationsT2, T3, . . . , T100 whereby with the operation Tk, 2 ≤ k ≤ 100, the condition ofbeing locked or unlocked is changed for all those lockers and only thoselockers whose numbers are multiples of k. Write C++ code to determinewhich lockers remain open.

Solution: Here is one possible solution. Later on in the Number Theorychapter we will see that only the lockers whose numbers are perfectsquares remain open.

//locker_problem.cpp

#include <iostream>

using namespace std;

Page 63: Discretemaths Book

Examples of Full Programmes 55

int main(){ //opens main

bool locker[101];//setting every locker open == true.

for(int i = 1; i <= 100; i++) locker[i] = true;for(int j = 2; j <=100; j++){for(int k = 1; k <= 100; k++) if(k % j == 0) locker[k] =

!locker[k]; }

for(int l = 1; l <= 100 ; l++ ) if(locker[l]) cout << ”Locker ”<< l << ” remains open.\n ”;

return 0;} //closes main

Page 64: Discretemaths Book

56 Chapter 1

Page 65: Discretemaths Book

Chapter 2Essential Techniques

2.1 Reductio ad Absurdum

In this section we will see examples of proofs by contradiction. That is, intrying to prove a premise, we assume that its negation is true and deduceincompatible statements from this.

131 Example Show, without using a calculator, that 6 −√

35 <1

10.

Solution: Assume that 6 −√

35 ≥ 1

10. Then 6 −

1

10≥

√35 or 59 ≥ 10

√35.

Squaring both sides we obtain 3841 ≥ 3500, which is clearly nonsense.

Thus it must be the case that 6 −√

35 <1

10.

132 Example Let a1, a2, . . . , an be an arbitrary permutation of thenumbers 1, 2, . . . , n, where n is an odd number. Prove that the product

(a1 − 1)(a2 − 2) · · · (an − n)

is even.

Solution: First observe that the sum of an odd number of odd integers isodd. It is enough to prove that some difference ak − k is even. Assume

1

Page 66: Discretemaths Book

2 Chapter 2

contrariwise that all the differences ak − k are odd. Clearly

S = (a1 − 1) + (a2 − 2) + · · · + (an − n) = 0,

since the ak’s are a reordering of 1, 2, . . . , n. S is an odd number ofsummands of odd integers adding to the even integer 0. This isimpossible. Our initial assumption that all the ak − k are odd is wrong, soone of these is even and hence the product is even.

133 Example Prove that√

2 is irrational.

Solution: For this proof, we will accept as fact that any positive integergreater than 1 can be factorised uniquely as the product of primes (up tothe order of the factors).Assume that

√2 =

a

b, with positive integers a, b. This yields 2b2 = a2.

Now both a2 and b2 have an even number of prime factors. So 2b2 has anodd numbers of primes in its factorisation and a2 has an even number ofprimes in its factorisation. This is a contradiction.

134 Example Let a, b be real numbers and assume that for all numbersε > 0 the following inequality holds:

a < b + ε.

Prove that a ≤ b.

Solution: Assume contrariwise that a > b. Hencea − b

2> 0. Since the

inequality a < b + ε holds for every ε > 0 in particular it holds for

ε =a − b

2. This implies that

a < b +a − b

2or a < b.

Thus starting with the assumption that a > b we reach the incompatibleconclusion that a < b. The original assumption must be wrong. Wetherefore conclude that a ≤ b.

135 Example Show that there are infinitely many prime numbers.

Page 67: Discretemaths Book

Reductio ad Absurdum 3

Solution: We need to assume for this proof that any integer greater than 1is either a prime or a product of primes. The following beautiful proofgoes back to Euclid.Assume that {p1, p2, . . . , pn} is a list that exhauts all the primes. Considerthe numberN = p1p2 · · ·pn + 1. This is a positive integer, clearly greaterthan 1. Observe that none of the primes on the list {p1, p2, . . . , pn} dividesN, since division by any of these primes leaves a remainder of 1. Since N

is larger than any of the primes on this list, it is either a prime or divisibleby a prime outside this list. Thus we have shown that the assumption thatany finite list of primes leads to the existence of a prime outside this list.This implies that the number of primes is infinite.

136 Example Let n > 1 be a composite integer. Prove that n has a primefactor p ≤ n.

Solution: Since n is composite, n can be written as n = ab where botha > 1, b > 1 are integers. Now, if both a >

√n and b >

√n then

n = ab >√

n√

n = n, a contradiction. Thus one of these factors must be≤ √

n and a fortiori it must have a prime factor ≤ √n.

The preceding result can be used to test for primality. For example, toshow that 101 is prime, we compute b

√101c = 10. By the preceding

problem, either 101 is prime or it is divisible by 2, 3, 5, or 7 (the primessmaller than 10). Since neither of these primes divides 101, we concludethat 101 is prime.

Ad Pleniorem Scientiam

137 Problem The product of 34 integers is equal to 1. Show that their sumcannot be 0.

138 Problem Let a1, a2, . . . , a2000 be natural numbers such that

1

a1

+1

a2

+ · · · + 1

a2000

= 1.

Prove that at least one of the ak’s is even.

(Hint: Clear denominators.)

Page 68: Discretemaths Book

4 Chapter 2

139 Problem Prove that log2 3 is irrational.

140 Problem A palindrome is an integer whose decimal expansion issymmetric, e.g. 1, 2, 11, 121, 15677651 (but not 010, 0110) are palindromes.Prove that there is no positive palindrome which is divisible by 10.

141 Problem In 4ABC, ∠A > ∠B. Prove that BC > AC.

142 Problem Let 0 < α < 1. Prove that√

α > α.

143 Problem Let α = 0.999 . . . where there are at least 2000 nines. Provethat the decimal expansion of

√α also starts with at least 2000 nines.

144 Problem Prove that a quadratic equation ax2 + bx + c = 0, a 6= 0 hasat most two solutions.

145 Problem Prove that if ax2 + bx + c = 0 has real solutions and ifa > 0, b > 0, c > 0 then both solutions must be negative.

146 Problem Let a, b, c be odd integers. Prove that ax2 + bx + c = 0

cannot have a rational root.

2.2 Pigeonhole Principle

The Pigeonhole Principle states that if n + 1 pigeons fly to n holes, theremust be a pigeonhole containing at least two pigeons. This apparentlytrivial principle is very powerful. Thus in any group of 13 people, there arealways two who have their birthday on the same month, and if theaverage human head has two million hairs, there are at least three peoplein NYC with the same number of hairs on their head.The Pigeonhole Principle is useful in proving existence problems, that is,we show that something exists without actually identifying it concretely.Let us see some more examples.

Page 69: Discretemaths Book

Pigeonhole Principle 5

147 Example (PUTNAM 1978) Let A be any set of twenty integers chosenfrom the arithmetic progression 1, 4, . . . , 100. Prove that there must be twodistinct integers in A whose sum is 104.

Solution: We partition the thirty four elements of this progression intonineteen groups

{1}, {52}, {4, 100}, {7, 97}, {10, 94}, . . . , {49, 55}.

Since we are choosing twenty integers and we have nineteen sets, by thePigeonhole Principle there must be two integers that belong to one of thepairs, which add to 104.

148 Example Show that amongst any seven distinct positive integers notexceeding 126, one can find two of them, say a and b, which satisfy

b < a ≤ 2b.

Solution: Split the numbers {1, 2, 3, . . . , 126} into the six sets

{1, 2}, {3, 4, 5, 6}, {7, 8, . . . , 13, 14}, {15, 16, . . . , 29, 30},

{31, 32, . . . , 61, 62} and {63, 64, . . . , 126}.

By the Pigeonhole Principle, two of the seven numbers must lie in one ofthe six sets, and obviously, any such two will satisfy the stated inequality.

149 Example No matter which fifty five integers may be selected from

{1, 2, . . . , 100},

prove that one must select some two that differ by 10.

Solution: First observe that if we choose n + 1 integers from any string of2n consecutive integers, there will always be some two that differ by n.This is because we can pair the 2n consecutive integers

{a + 1, a + 2, a + 3, . . . , a + 2n}

into the n pairs

{a + 1, a + n + 1}, {a + 2, a + n + 2}, . . . , {a + n, a + 2n},

Page 70: Discretemaths Book

6 Chapter 2

and if n + 1 integers are chosen from this, there must be two that belongto the same group.So now group the one hundred integers as follows:

{1, 2, . . . 20}, {21, 22, . . . , 40},

{41, 42, . . . , 60}, {61, 62, . . . , 80}

and{81, 82, . . . , 100}.

If we select fifty five integers, we must perforce choose eleven from somegroup. From that group, by the above observation (let n = 10), there mustbe two that differ by 10.

150 Example (AHSME 1994) Label one disc “1”, two discs “2”, three discs“3”, . . . , fifty discs ‘‘50”. Put these 1 + 2 + 3 + · · ·+ 50 = 1275 labeled discsin a box. Discs are then drawn from the box at random withoutreplacement. What is the minimum number of discs that must me drawnin order to guarantee drawing at least ten discs with the same label?

Solution: If we draw all the 1 + 2 + · · · + 9 = 45 labelled “1”, . . . , “9” andany nine from each of the discs “10”, . . . , “50”, we have drawn45 + 9 · 41 = 414 discs. The 415-th disc drawn will assure at least tendiscs from a label.

151 Example (IMO 1964) Seventeen people correspond by mail with oneanother—each one with all the rest. In their letters only three differenttopics are discussed. Each pair of correspondents deals with only one ofthese topics. Prove that there at least three people who write to eachother about the same topic.

Solution: Choose a particular person of the group, say Charlie. Hecorresponds with sixteen others. By the Pigeonhole Principle, Charliemust write to at least six of the people of one topic, say topic I. If any pairof these six people corresponds on topic I, then Charlie and this pair dothe trick, and we are done. Otherwise, these six correspond amongstthemselves only on topics II or III. Choose a particular person from thisgroup of six, say Eric. By the Pigeonhole Principle, there must be three of

Page 71: Discretemaths Book

Pigeonhole Principle 7

the five remaining that correspond with Eric in one of the topics, say topicII. If amongst these three there is a pair that corresponds with each otheron topic II, then Eric and this pair correspond on topic II, and we aredone. Otherwise, these three people only correspond with one anotheron topic III, and we are done again.

152 Example Given any set of ten natural numbers between 1 and 99

inclusive, prove that there are two disjoint nonempty subsets of the setwith equal sums of their elements.

Solution: There are 210 − 1 = 1023 non-empty subsets that one can formwith a given 10-element set. To each of these subsets we associate thesum of its elements. The maximum value that any such sum can achieveis 90 + 91 + · · · + 99 = 945 < 1023. Therefore, there must be at least twodifferent subsets that have the same sum.

153 Example Given any 9 integers whose prime factors lie in the set{3, 7, 11} prove that there must be two whose product is a square.

Solution: For an integer to be a square, all the exponents of its primefactorisation must be even. Any integer in the given set has a primefactorisation of the form 3a7b11c. Now each triplet (a, b, c) has one of thefollowing 8 parity patterns: (even, even, even), (even, even, odd), (even,odd, even), (even, odd, odd), (odd, even, even), (odd, even, odd), (odd,odd, even), (odd, odd, odd). In a group of 9 such integers, there must betwo with the same parity patterns in the exponents. Take these two. Theirproduct is a square, since the sum of each corresponding exponent willbe even.

Ad Pleniorem Scientiam

154 Problem Prove that among n integers, there are always two whosedifference is always divisible by n.

155 Problem (AHSME 1991) A circular table has exactly sixty chairsaround it. There are N people seated at this table in such a way that thenext person to be seated must sit next to someone. What is the smallestpossible value of N?

Page 72: Discretemaths Book

8 Chapter 2

Answer: 20.

156 Problem Show that if any five points are all in, or on, a square of side1, then some pair of them will be at most at distance

√2/2.

157 Problem (HUNGARIAN MATH OLYMPIAD, 1947) Prove that amongstsix people in a room there are at least three who know one another, or atleast three who do not know one another.

158 Problem Show that in any sum of nonnegative real numbers there isalways one number which is at least the average of the numbers and thatthere is always one member that it is at most the average of the numbers.

159 Problem We call a set “sum free” if no two elements of the set add upto a third element of the set. What is the maximum size of a sum freesubset of {1, 2, . . . , 2n − 1}.

Hint: Observe that the set {n + 1, n + 2, . . . , 2n − 1} of n + 1 elements issum free. Show that any subset with n + 2 elements is not sum free.

160 Problem (MMPC 1992) Suppose that the letters of the Englishalphabet are listed in an arbitrary order.

1. Prove that there must be four consecutive consonants.

2. Give a list to show that there need not be five consecutiveconsonants.

3. Suppose that all the letters are arranged in a circle. Prove that theremust be five consecutive consonants.

161 Problem (STANFORD 1953) Bob has ten pockets and forty four silverdollars. He wants to put his dollars into his pockets so distributed thateach pocket contains a different number of dollars.

1. Can he do so?

Page 73: Discretemaths Book

Pigeonhole Principle 9

2. Generalise the problem, considering p pockets and n dollars. Theproblem is most interesting when

n =(p − 1)(p − 2)

2.

Why?

162 Problem Let M be a seventeen-digit positive integer and let N be thenumber obtained from M by writing the same digits in reversed order.Prove that at least one digit in the decimal representation of the numberM + N is even.

163 Problem No matter which fifty five integers may be selected from

{1, 2, . . . , 100},

prove that you must select some two that differ by 9, some two that differby 10, some two that differ by 12, and some two that differ by 13, but thatyou need not have any two that differ by 11.

164 Problem Let mn + 1 different real numbers be given. Prove that thereis either an increasing sequence with at least n + 1 members, or adecreasing sequence with at least m + 1 members.

165 Problem If the points of the plane are coloured with three colours,show that there will always exist two points of the same colour which areone unit apart.

166 Problem Show that if the points of the plane are coloured with twocolours, there will always exist an equilateral triangle with all its vertices ofthe same colour. There is, however, a colouring of the points of the planewith two colours for which no equilateral triangle of side 1 has all itsvertices of the same colour.

167 Problem (USAMO 1979) Nine mathematicians meet at aninternational conference and discover that amongst any three of them, atleast two speak a common language. If each of the mathematicians canspeak at most three languages, prove that there are at least three of themathematicians who can speak the same language.

Page 74: Discretemaths Book
Page 75: Discretemaths Book
Page 76: Discretemaths Book
Page 77: Discretemaths Book

Inclusion-Exclusion 13

175 Example How many integers between 1 and 600 inclusive are notdivisible by neither 3, nor 5, nor 7?

Solution: Let Ak denote the numbers in [1, 600] which are divisible byk = 3, 5, 7. Then

|A3| = b6003c = 200,

|A5| = b6005c = 120,

|A7| = b6007c = 85,

|A15| = b60015c = 40

|A21| = b60021c = 28

|A35| = b60035c = 17

|A105| = b600105

c = 5

By Inclusion-Exclusion there are 200 + 120 + 85 − 28 − 21 − 17 + 5 = 325

integers in [1, 600] divisible by at least one of 3, 5, or 7. Those notdivisible by these numbers are a total of 600 − 325 = 275.

176 Example In a group of 30 people, 8 speak English, 12 speak Spanishand 10 speak French. It is known that 5 speak English and Spanish, 5

Spanish and French, and 7 English and French. The number of peoplespeaking all three languages is 3. How many do not speak any of theselanguages?

Solution: Let A be the set of all English speakers, B the set of Spanishspeakers and C the set of French speakers in our group. We fill up thefollowing Venn diagram successively. In the intersection of all three weput 8. In the region common to A and B which is not filled up we put5 − 2 = 3. In the region common to A and C which is not already filled upwe put 5 − 3 = 2. In the region common to B and C which is not alreadyfilled up, we put 7 − 3 = 4. In the remaining part of A we put8 − 2 − 3 − 2 = 1, in the remaining part of B we put 12 − 4 − 3 − 2 = 3, andin the remaining part of C we put 10 − 2 − 3 − 4 = 1. Each of the mutuallydisjoint regions comprise a total of 1 + 2 + 3 + 4 + 1 + 2 + 3 = 16 persons.Those outside these three sets are then 30 − 16 = 14.$.4in]

Ad Pleniorem Scientiam

177 Problem How many integers between 1 and 3012 are divisible by 5 or7 but not by both numbers?

Page 78: Discretemaths Book

14 Chapter 2

178 Problem Would you believe a market investigator who reports that of1000 people, 816 like candy, 723 ice cream, 645 cake, while 562 like bothcandy and ice cream, 463 like both candy and cake, 470 like both icecream and cake, while 310 like all three?

179 Problem (Lewis Carroll) In a very hotly fought battle, at least 70% ofthe combatants lost an eye, at least 75% an ear, at least 80% an arm, andat least 85% a leg. What can be said about the percentage that lost allfour members?

180 Problem (IMO 1966) In a mathematical contest, three problems, A, B,C, were posed. Amongst the participants there were twenty-five studentswho solved at least one problem each. Of all the contestants who did notsolve problem A, the number who solved problem B was twice the numberwho solved problem C. The number of students who solved only problemA was one more than the number of students who solved problem A andat least one other problem. Of all the students who solved just oneproblem, half did not solve A. How many students solved only problem B?

181 Problem A group of students took exams in French, English, andMaths. Of them, 92 passed French, 72 English, and 63 Maths. It is alsoknown that at most 65 passed French and English, at most 54 French andMaths, and at most 48 English and Maths. Find the largest possiblenumber of students that could have passed all three subjects.

182 Problem How many integers between 1 and 49000 inclusive are notdivisible by 3, 5 or 7?

183 Problem How many integers from 1 to 1 000 000 inclusive are neitherperfect squares, perfect cubes, nor perfect fourth powers?

184 Problem Compute φ(120), φ(500) and φ(700).

185 Problem Let x, y be real numbers. Prove that

x + y = min(x, y) + max(x, y)

Page 79: Discretemaths Book

Parity 15

186 Problem Let x, y, z be real numbers. Prove that

max(x, y, z) = x + y + z − min(x, y) − min(y, z) − min(z, x) + min(x, y, z)

2.4 Parity

187 Example Two diametrically opposite corners of a chess board aredeleted. Show that it is impossible to tile the remaining 62 squares with 31

dominoes.

Solution: Each domino covers one red square and one black squares.But diametrically opposite corners are of the same colour, hence thistiling is impossible.

188 Example All the dominoes in a set are laid out in a chain according tothe rules of the game. If one end of the chain is a 6, what is at the otherend?

Solution: At the other end there must be a 6 also. Each number of spotsmust occur in a pair, so that we may put them end to end. Since there areeight 6’s, this last 6 pairs off with the one at the beginning of the chain.

189 Definition The midpoint of the line joining (x, y) to (x1, y1) is the point(

x + x1

2,y + y1

2

)

.

190 Example The numbers 1, 2, . . . , 10 are written in a row. Show that nomatter what choice of sign ± is put in between them, the sum will neverbe 0.

Solution: The sum 1 + 2 + · · · + 10 = 55, an odd integer. Since parity isnot affected by the choice of sign, for any choice of sign ±1 ± 2 ± · · · ± 10

will never be even, in particular it will never be 0.

191 Definition A lattice point (m,n) on the plane is one having integercoordinates.

Page 80: Discretemaths Book

16 Chapter 2

192 Example Five lattice points are chosen at random. Prove that onecan always find two so that the midpoint of the line joining them is also alattice point.

Solution: There are four parity patterns: (even, even), (even, odd), (odd,odd), (odd, even). By the Pigeonhole Principle among five lattice pointsthere must be two having the same parity pattern. Choose these two. It isclear that their midpoint is an integer.For the following examples we will need to know the names of thefollowing tetrominoes. $.4in]L-tetromino$.4in]

T-tetromino$.4in]

Straight-tetromino$.4in]

Skew-tetromino$.4in]

Show that an 8 × 8 chessboard cannot be tiles with 15 straighttetrominoes and one L-tetromino.

Solution: Colour rows 1, 3, 5, 7 black and colour rows 2, 4, 6, and 8 red.A straight tetromino will always cover an even number of red boxes andthe L-tetromino will always cover an odd number of red squares. If thetiling were possible, then we would be covering an odd number of redsquares, a contradiction.

Ad Pleniorem Scientiam

193 Problem Twenty-five boys and girls are seated at a round table. Showthat both neighbours of at least one student are girls.

194 Problem A closed path is made of 2001 line segments. Prove thatthere is no line, not passing through a vertex of the path, intersectingeach of the segments of the path.

195 Problem The numbers 1, 2, . . . , 2001 are written on a blackboard. Onestarts erasing any two of them and replacing the deleted ones with theirdifference. Will a situation arise where all the numbers on the blackboardbe 0?

196 Problem Show that a 10 × 10 chessboard cannot be tiled with 25

straight tetrominoes.

Page 81: Discretemaths Book

Parity 17

197 Problem Show that an 8 × 8 chess board cannot be tiled with 15

T-tetrominoes and one square tetromino.

Page 82: Discretemaths Book

18 Chapter 2

Page 83: Discretemaths Book

Chapter 3Counting

3.1 Introductory Problems

198 Example n equally spaced points 1, 2, . . . , n are marked on acircumference. If 15 directly opposite to 49, how many points are theretotal?

Solution: Points 16, 17, . . . , 48 are 33 in total and are on the same side ofthe diameter joining 15 to 49. For each of these points there is acorresponding diametrically opposite point. There are thus a total of2 · 33 + 2 = 68 points.

199 Example How many factors of 295 are larger than 1, 000, 000?

Solution: The 96 factors of 295 are 1, 2, 22, . . . , 295. Observe that210 = 1024 and so 220 = 1048576. Hence219 = 524288 < 1000000 < 1048576 = 220. The factors greater than1,000,000 are thus 220, 221, . . . 295. This makes for 96 − 20 = 76 factors.

200 Example How many positive divisors does 283952 have? What is thesum of these divisors?

Solution: We will assume that the positive integers may be factorised in a

19

Page 84: Discretemaths Book
Page 85: Discretemaths Book

Introductory Problems 21

locker n remains closed if and only if n has an odd number of divisors.But from (5.1) d(n) will be odd if and only if n is a perfect square. Thusthe lockers that remain closed are the ones with a perfect square number:lockers 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

202 Example How many positive integers have (i) 2 digits? (ii) 3 digits?(iii) 4 digits?

Solution: There are 99 integers in the range 1, 2, . . . , 99. Of those, 9 have1 digit so there are 99 − 9 = 90 positive integers with two digits. Similarly,there are 999 − 99 = 900 positive integers with three digits and9999 − 999 = 9000 positive integers with four digits.

203 Example To write a book 1890 digits were utilised. How many pagesdoes the book have?

Solution: A total of1 · 9 + 2 · 90 = 189

digits are used to write pages 1 to 99, inclusive. We have of1890 − 189 = 1701 digits at our disposition which is enough for1701/3 = 567 extra pages (starting from page 100). The book has99 + 567 = 666 pages.

204 Example All the natural numbers, starting with 1, are listedconsecutively

123456789101112131415161718192021 . . .

Which digit occupies the 1002nd place?

Solution: When the number 99 is written down, we have used

1 · 9 + 2 · 90 = 189

digits. If we were able to write 999, we would have used

1 · 9 + 2 · 90 + 3 · 900 = 2889

digits, which is more than 1002 digits. The 1002nd digit must be amongthe three-digit positive integers. We have 1002 − 189 = 813 digits at our

Page 86: Discretemaths Book

22 Chapter 3

disposal, from which we can make b8133c = 271 three-digit integers, from

100 to 270. When the 0 in 270 is written, we have used189 + 3 · 271 = 1002 digits. The 1002nd digit is the 0 in 270.

205 Example (AHSME 1994) When n standard six-sided dice are rolled,the probability of obtaining a sum of 1994 is greater than zero and is thesame as the probability of obtaining a sum of S. What is the smallestpossible value of S?

Solution: Since the probability of obtaining the sum 1994 is positive, there

are n ≥ b1994

6c = 333 dice. Let x1 + x2 + · · · + xn = 1994 be the sum of

the faces of the n dice adding to 1994. We are given that

(7 − x1) + (7 − x2) + · · · + (7 − xn) = S

or 7n − 1994 = S. The minimal sum will be achieved with the minimumdice, so putting n = 333 we obtain the minimal S = 7(333) − 1994 = 337.

206 Example (AIME 1993) How many ordered four-tupels of integers(a, b, c, d) with 0 < a < b < c < d < 500 satisfy a + d = b + c andbc − ad = 93?

Solution: Since a + d = b + c, we can write the four-tuple (a, b, c, d) as(a, a + x, a + y, a + x + y), with integers x, y, 0 < x < y. Now,

93 = bc − ad = (a + x)(a + y) − a(a + x + y) = xy.

This gives (x, y) = (1, 93) or (x, y) = (3, 31). In the first case

(a, b, c, d) = (a, a + 1, a + 93, a + 94)

is in the desired range for 1 ≤ a ≤ 405 and in the second case

(a, b, c, d) = (a, a + 3, a + 31, a + 34)

is in the desired range for 1 ≤ a ≤ 465. The two sets of four-tuples aredisjoint, and so the sought number is 870.

Ad Pleniorem Scientiam

Page 87: Discretemaths Book

Two Basic Counting Principles 23

207 Problem Find d(1260) and σ(1260).

208 Problem The integers from 1 to 1000 are written on a circle. Startingwith 1, each 15th is marked (that is, 1, 16, 31, etc. are marked). Thisoperation is repeated until a number is marked again. How many integersremain unmarked

Answer: 800

209 Problem All the positive integers are written in succession.

123456789101112131415161718192021222324 . . .

Which digit occupies the 206790th place?

Answer: 4

210 Problem Four comrades are racing down a dusty staircase. Oli goesdown two steps at a time, Gooh three, Phree four, and Nyck five. If theonly steps with all four’s footprints are at the top and at the bottom, howmany steps have just one footprint?

211 Problem (AHSME 1992) For how many integers between 1 and 100

doesx2 + x − n

factor into the product of two linear factors with integer coefficients?

212 Problem (AHSME 1989) Five people are sitting at a round table. Letf ≥ 0 be the number of people sitting next to at least one female andm ≥ 0 be the number of people sitting next to at least one male. Find thenumber of possible ordered pairs (f,m).

Answer: 8

3.2 Two Basic Counting Principles

We denote by (E) the cardinality (number of elements) of the set E. Forexample if E = {x ∈ Z : |x| ≤ 3} then (E) = 7, since x ∈ E if and only if

Page 88: Discretemaths Book

24 Chapter 3

x = −3,−2,−1, 0, 1, 2 or 3.

The Addition Principle: If Em ∩ En = ∅ (the sets are pairwise disjoint)

(E1 ∪ E2 ∪ · · · ∪ Ek) = (E1) + (E2) + · · · + (Ek).

213 Example Find the number of lattice points (x, y) that satisfy|x| + |y| ≤ 3.

Solution: E = {(x, y) : x ∈ Z, y ∈ Z, |x| + |y| ≤ 3}. Then E can bedecomposed into the pairwise disjoint sets Ek, whereEk = {(x, y) : x ∈ Z, y ∈ Z, |x| + |y| = k} and k = 0, 1, 2, 3. Now

1. b0 = {(0, 0)}

E1 = {(−1, 0), (1, 0), (0,−1), (0, 1)}

E2 = {(−2, 0), (2, 0), (0,−2), (0, 2), (−1,−1), (−1, 1), (1,−1)(1, 1)}

E3 = {(−1,−2), (−1, 2), (1,−2), (1, 2)}

Hence(E) = E0 ∪ E1 ∪ E2 ∪ E3)

= (E0) + (E1) + (E2) + (E3)

= 1 + 4 + 8 + 4 = 17.

The Cartesian Productk∏

i=1

Ei = E1 × E2 × · · · × Ek is the collection of

ordered k-tuples {(e1, e2, . . . , ek) : ei ∈ Ei}.

The Multiplication Principle: Letk∏

i=1

Ei = {(e1, e2, . . . , ek) : ei ∈ Ei} be

the Cartesian product of the sets Ei. Then(

k∏

i=1

Ei

)

=

k∏

i=1

(Ei).

214 Example A license plate is to be made according to the followingprovision: it has four characters, the first two characters can be any letterof the English alphabet and the last two characters can be any digit. Howmany different license plates can be made?

Page 89: Discretemaths Book

Two Basic Counting Principles 25

Solution: The number of different license plates is the number of differentfour-tuples (Letter 1, Letter 2, Digit 1, Digit 2). The first letter can bechosen in 26 ways, the second letter can be chosen in 26 ways, the firstdigit can be chosen in 10 ways and the last digit can be chosen in 10

ways. By the multiplication principle, the number of different four-tuples is26 · 26 · 10 · 10 = 67600.

215 Example How many positive integers have 4 digits? None of theintegers is to have 0 as its leftmost digit.

Solution: Each four-digit integer 1000a + 100b + 10c + d can beconsidered as an ordered four-tuple (a, b, c, d) with a ∈ {1, 2, . . . , 9} andb, c, d ∈ {0, 1, 2, . . . , 9}. By the multiplication principle, the number of suchfour-tuples is 9 · 10 · 10 · 10 = 9000.

216 Example A palindrome is an integer whose decimal expansion issymmetric, e.g. 1, 2, 11, 121, 15677651 (but not 010, 0110) are palindromes.How many positive palindromes are there with (i) 4 digits?, (ii) 5 digits?

Solution: Each four-digit palindrome 1000a + 100b + 10b + a can beconsidered as an ordered four-tuple (a, b, b, a) with a ∈ {1, 2, . . . , 9} andb ∈ {0, 1, 2, . . . , 9}. By the multiplication principle, the number of suchfour-tuples is 9 · 10 = 90. Similarly, each five-digit palindrome10000a + 1000b + 100c + 10b + a can be considered as an orderedfive-tuple (a, b, c, b, a) with a ∈ {1, 2, . . . , 9} and b, c ∈ {0, 1, 2, . . . , 9}.Again, by the multiplication principle, the number of such five-tuples is9 · 10 · 10 = 900.

217 Example How many different n-digit positive integers do not have thedigit 5?

Solution: The first (leftmost) digit, can be one of the 8 digits{1, 2, 3, 4, 6, 7, 8, 9}. The remaining n − 1 digits can be one of 9 digits{0, 1, 2, 3, 4, 6, 7, 8, 9}. The number of such integers is thus 8 · 9n−1.

218 Example Prove that a set with n elements has 2n different subsets(including the empty set and the set itself).

Page 90: Discretemaths Book

26 Chapter 3

Solution: We motivate the idea of the proof for the case when n = 3.

Suppose S = {a, b, c} is a set with three elements. Which each subset ofthe set we associate a dyadic (0-1) ternary sequence writing 0 if theelement is not in the set, and 1 if the element belongs to the set. So with

the set ∅ we associate the sequence 000

the set {a} we associate the sequence 100

the set {b} we associate the sequence 010

the set {c} we associate the sequence 001

the set {a, b} we associate the sequence 110

the set {a, c} we associate the sequence 101

the set {b, c} we associate the sequence 011

the set {a, b, c} we associate the sequence 111

In the general case, we may supposed the elements of the set to beordered as (x1, x2, . . . , xn). To each subset T of the set, we associate adyadic sequence as above, writing 1 if xi ∈ T and 0 if xi 6∈ T . For each ofn positions we have two choices, and so the total elements of subsets is2 · · · 2︸ ︷︷ ︸n 2 ′s

= 2n.

219 Example Out of nine different pairs of shoes, in how many ways couldI choose a right shoe and a left shoe, which do not form a pair?

Solution: The left shoe can be chosen in 9 ways, the right shoe can bechosen in 8 ways, so as not to pair up with the first. The total number ofways is thus 9 · 8 = 72.

Aliter: There are 9 · 9 = 81 ways of choosing a left shoe and a right shoe.Of those, 9 pairs match, so the required number of ways is 81 − 9 = 72.Sometimes we need to combine both the addition and the multiplicationprinciple.

220 Example There are five Golden retrievers, six Irish setters, and eightPoodles at the pound. In how many ways can two dogs be chosen if theyare not the same kind?

Page 91: Discretemaths Book

Two Basic Counting Principles 27

Solution: Notice that the order in which the dogs are chosen isunimportant. One Golden retriever and one Irish setter can be chosen in5 · 6 = 30 ways. One Golden retriever and one Poodle can be chosen in5 · 8 = 40 ways. One Irish setter and one Poodle can be chosen in6 · 8 = 48 ways. The total number of ways of choosing a pair that are notof the same breed is thus 30 + 40 + 48 = 118.

221 Example How many triplets (a, b, c) with a, b, c ∈ {1, 2, . . . , 101}

simultaneously satisfy a < b and a < c?

Solution: We condition on a, which can take any of the valuesa = 1, 2, . . . , 100. Given a, b can be any of the 101 − a values in{a + 1, a + 2, . . . , 101}. Similarly, c can be any of the 101 − a values in{a + 1, a + 2, . . . , 101}. Given a then, b and c may be chosen in(101 − a)(101 − a) = (101 − a)2 ways. The number of triplets is thereforeby formula (4.5),100∑

a=1

(101−a)2 = 1002+992+982+· · ·+12 =100(100 + 1)(2(100) + 1)

6= 338350.

222 Example (PUTNAM 1987) The sequence of digits

12345678910111213141516171819202122 . . .

is obtained by writing the positive integers in order. If the 10n digit of thissequence occurs in the part in which the m-digit numbers are placed,define f : N → N by f(n) = m. For example f(2) = 2, because thehundredth digit enters the sequence in the placement of the two-digitinteger 55. Find, with proof, f(1987).

Solution: There are 9 · 10j−1 j-digit positive integers. The total number ofdigits in numbers with at most r digits is the arithmetic-geometric sum

g(r) =

j∑

j=1

j · 9 · 10j−1 = r10r −10r − 1

9.

As 0 <10r − 1

9< 10r, we get

(r − 1)10r < g(r) < r10r.

Page 92: Discretemaths Book
Page 93: Discretemaths Book
Page 94: Discretemaths Book

30 Chapter 3

last digit, 8 for the penultimate and 7 for the antepenultimate. This gives280 ways. The total number sought is therefore 224 + 224 + 280 = 728.

231 Example Seven different beads are arranged in a circular necklace.In how many different ways can they be arranged?

Solution: Let the beads be A,B,C,D, E, F,G. If the beads were in a row,we would have 7! = 7 · 6 · 5 · 4 · 3 · 2 · 1 arrangements, but because thesebeads are arranged in a circle, where there is no beginning and no end,different linear permutations may produced identical circularpermutations. For example, the 7 permutations

ABCDEFG,BCDEFGA,CDEFGAB,DEFGABC, EFGABCD, FGABCDE,GABCDEF

are identical when arranged in a circle. Since we may choose any of thebeads as the first bead, the sought numbers of arrangements is7!/7 = 6! = 720.

Ad Pleniorem Scientiam

232 Problem If repetitions are forbidden, (i) how many three-digitnumbers can be formed from the set {2, 3, 5, 6, 7, 9}, (ii) how many of thesenumbers are less than 400?, (iii) how many of the set of all three digitnumbers formed are even?, (iii) how many are odd?, (iv) how many aremultiples of 5?

Answer: 120, 40, 40, 80, 20

233 Problem In how many ways can three students and two professors sitin a row? In how many ways can they sit in a row if the students and theprofessors are to sit within themselves (that is, we have a row of juststudents and just professors, or viceversa)? In how many ways can theysit if only the professors are to sit together (that is, the two professor arealways next to one another)?

Answer: 120, 24, 48

234 Problem Three Mathematics majors and two Computer Sciencemajors sit around a round table. In how many ways can they do so if theMathematics majors insist in sitting next to each other?

Page 95: Discretemaths Book

Permutations 31

Answer: 12

235 Problem How many different license plates are there involving threeletters and four digits if the three letters must appear together either at thebeginning or at the end of the plate?

Answer: 1042632! = 351520000

236 Problem In how many ways can a committe of k people be chosenfrom ten people if k can be any of 1, 2, 3, . . . 10?

Answer 210 − 1 = 1023

237 Problem Find the number of ways in which five different Englishbooks, six French books, three German books, and seven Russian bookscan be arranged together on a shelf so that all books of the samelanguage are together.

Answer: 3!4!5!6!7!

238 Problem How many ways are there to seat ten boys and ten girlsaround a circular table?

Answer: 19!

239 Problem A child has blocks of six different colours. If the child selectsone block of each colour, in how many ways can these be arranged in aline?

240 Problem How many ten-digit multiples of five are there if no digit is tobe repeated?

Answer: 8 · 8! + 9!

241 Problem Find the number of seven-digit palindromes none of whosedigits are used more than twice.

Answer: 4536

Page 96: Discretemaths Book

32 Chapter 3

3.4 Combinations

Each of the groups or selections which can be made by taking some or allof a number of things is called a combination.The combinations which can be made by taking the numbers {1, 2, 3, 4}

two at the time are six in number, namely,

12, 13, 14, 23, 24, 34.

This number can be obtained without enumerating these possibilities. Forthere are four ways of choosing the first number and three ways ofobtaining the second number. Thus there are twelve ways of choosing afirst and then a second number. Now, we are not interested in the ordersince, for example, {12} is as good as {21}. We must then divide by thenumber of ways of permuting these repeats, namely, 2!. Therefore thetotal number of combinations is 4 · 3/2! = 6.

Observe that in forming combinations we are not interested in thepossible orders of the selections, as we are in permutations. We denotethe number of combinations of n objects taken k at the time by

(

n

k

)

, read“n choose k.” With regards to order, we can choose k objects in

n(n − 1)(n − 2) · · · (n − k + 1),

as there are n ways of choosing the first object, n − 1 ways of choosingthe second, etc.. But these k objects chosen can be arranged in k! waysamongst themselves, and hence we conclude that

(

n

k

)

=n(n − 1)(n − 2) · · · (n − k + 1)

k!(3.3)

If we multiply the above fraction by(n − k)!

(n − k)!, we can easily see that

(

n

k

)

=n!

k!(n − k)!(3.4)

We now give some examples of the use of combinations.

242 Example How many different committees of seven people can beformed from twenty people?

Page 97: Discretemaths Book
Page 98: Discretemaths Book

34 Chapter 3

there are(

9

3

)

= 84 three-digit numbers with all the digits increasing. Theasked total is hence 120 + 84 = 204.

246 Example There are twenty students in a class. In how many wayscan the twenty students take five different tests if the four of the studentsare to take each test?

Solution: We can choose the four students who are going to take the firsttest in

(

20

4

)

. From the remaining ones, we can choose students in(

16

4

)

totake the second test. The third test can be taken in

(

12

4

)

ways, the fourthin(

8

4

)

and the fifth in(

4

4

)

ways. The required number of ways is thus(

20

4

)(

16

4

)(

12

4

)(

8

4

)(

4

4

)

=20!

4!4= 7332965640000.

247 Example In how many ways can a woman choose three or morelovers from seven eligible suitors?

Solution: She might choose three in(

7

3

)

ways, four in(

7

4

)

ways, etc.. Thetotal number of ways is

(

7

3

)

+

(

7

4

)

+

(

7

5

)

+

(

7

6

)

+

(

7

7

)

= 99.

248 Example How many times is the digit 3 listed in the numbers 1 to1000?

Solution: We count those numbers that have exactly one 3, exactly two 3sand exactly three 3s. There is only one numberin the range 1—1000 thathas 3 three times, namely 333. Suppose xyz, where x, y, z are digits, is tohave the digit 3 exactly twice. We can choose these two positions in(

3

2

)

= 3 ways. The third position can be filled with any of the remainingnine digits. Thus there are 9 · 3 = 27 numbers in the range 1—1000 thatcontain the digit 3 exactly twice. Similarly, there are 92

(

3

1

)

= 243 numbersthat use the digit 3 exactly once. This means that the digit 3 appears3 · 1 + 2 · 27 + 1 · 243 = 300 times.

Page 99: Discretemaths Book

Combinations 35

249 Example (AHSME 1994) Nine chairs in a row are to be occupied bysix students and Professors Alpha, Beta and Gamma. These threeprofessors arrive before the six students and decide to choose theirchairs so that each professor will be between two students. In how manyways can Professors Alpha, Beta and Gamma choose their chairs?

Solution: Align the six students first. There are five spaces betweenthem. A professor will be between two students if and only if he occupiesone of these five spaces. Thus we must choose three spaces out of five,which can be done in

(

5

3

)

= 10 different ways. Since the order of theprofessors can be permuted, the final answer is 10 × 3! = 60.

250 Example In how many ways can a deck of playing cards can bearranged if no two hearts are adjacent?

Solution: We align the 39 cards which are not hearts first. There arethirty-eight spaces between them, and one extra space before the firstcard and another extra space after the last card, giving a total of 40spaces where the hearts can be placed. There are thus

(

40

13

)

ways ofchoosing the places where the hearts can go. Since we are interested inarrangements, we can arrange the hearts in 13! ways and the nonehearts in 39! ways. The total number of arrangements is thus

(

40

13

)

13!39!.

251 Example Given a positive integer n find the number of integerquadruples (a, b, c, d) with 0 ≤ a ≤ b ≤ c ≤ d ≤ n.

Solution: By letting (a ′, b ′, c ′, d ′) = (a, b + 1, c + 2, d + 3) we establish aone-to-one correspondence between the vectors(a, b, c, d), 0 ≤ a ≤ b ≤ c ≤ d and the vectors

(a ′, b ′, c ′, d ′), 0 ≤ a ′ < b ′ < c ′ < d ′ ≤ n + 3.

To count this last set we choose four different numbers from the set{0, 1, 2, . . . , n + 3}. This can be done in

(

n+4

4

)

ways.

Ad Pleniorem Scientiam

252 Problem (AHSME 1989) Mr. and Mrs. Zeta want to name baby Zetaso that its monogram (first, middle, and last initials) will be in alphabeticalorder with no letters repeated. How many such monograms are possible?

Page 100: Discretemaths Book

36 Chapter 3

253 Problem In how many ways can you pack twelve books into fourparcels if one parcel has one book, another parcel has five books, andanother has two books, and another has four books?

Answer:(

12

1

)(

11

5

)(

6

2

)(

4

4

)

254 Problem In how many ways can a person invite three of his sixfriends to lunch every day for twenty days if he has the option of invitingthe same or different friends from previous days?

Answer:(

6

3

)20

255 Problem A committee is to be chosen from a set of nine women andfive men. In how many ways can one form a committee that has threemen and three women?

Answer:(

9

3

)(

5

3

)

256 Problem In how many ways can the following prizes be given away toa class of twenty students: first and second Latin, first and secondMathematics, first Science, and first French?

Answer: 57760000

257 Problem How many integers less than 10000 can be made with theeight digits 0, 1, 2, 3, 4, 5, 6, 7?

Answer: 4095

258 Problem In how many ways can seven persons form a ring? In howmany ways can seven Englishmen and seven Americans sit down at around table, no two Americans being together?

Answer: 720; 3628800

259 Problem From three guavas, four peaches, and two oranges, howmany selections of fruit can be made, taking at least one of each kind?

Page 101: Discretemaths Book

Distributions 37

Answer:((

3

1

)

+

(

3

2

)

+

(

3

3

))((

4

1

)

+

(

4

2

)

+

(

4

3

)

+

(

4

4

))((

2

1

)

+

(

2

2

))

= 315

260 Problem (AIME 1984) A gardener plants three maple trees, four oaktrees and five birch trees in a row. He plants them in random order, eacharrangement being equally likely. Let m/n in lowest terms be theprobability that no two birch trees are next to each other. Find m + n.

Answer: 106.

3.5 Distributions

261 Theorem Let n be a positive integer. The number of positive solutionsto

x1 + x2 + · · · + xr = n

is(

n − 1

r − 1

)

.

262 Example In how many ways can 100 be written as the sum of four(positive) summands?

263 Corollary Let n be a positive integer. The number of non-negativesolutions to

x1 + x2 + · · · + xr = n

is(

n + r − 1

r − 1

)

.

Ad Pleniorem Scientiam

264 Problem There are thirty students in a class. In how many ways canthe class elect a President and a Vice-President if no student can beelected for both offices?

Page 102: Discretemaths Book

38 Chapter 3

Answer: 870

3.6 Combinatorial Identities

265 Example Give a combinatorial interpretation of the symmetryIdentity

(

n

k

)

=

(

n

n − k

)

(3.5)

266 Example Give a combinatorial proof that for integer n ≥ 1,

(

2n

n

)

=

n∑

k=0

(

n

k

)2

(3.6)

267 Example Give a combinatorial interpretation of Newton’s Identity(

n

r

)(

r

k

)

=

(

n

k

)(

n − k

r − k

)

(3.7)

for 0 ≤ k ≤ r ≤ n.

268 Example Give a combinatorial interpretation of the Pascal’s Identity(

n

k

)

=

(

n − 1

k

)

+

(

n − 1

k − 1

)

(3.8)

269 Example Let n ≥ 1. Prove that

n∑

k=0

(

n

k

)

= 2n(3.9)

270 Example (AIME 1989) Ten points are marked on a circle. How manydistinct convex polygons of three or more sides can be drawn using some(or all) of the ten points as vertices? (Polygons are distinct unless theyhave exactly the same vertices.)

Page 103: Discretemaths Book

Combinatorial Identities 39

Solution: Choosing k points, 3 ≤ k ≤ 10 will determine a k-sided polygon,since the polygons are convex and thus have no folds. The answer is thus

10∑

k=3

= 210 −

(

10

0

)

(

10

1

)

(

10

2

)

= 968.

271 Example (AIME 1988) One commercially available ten-button lockmay be opened by depressing —in any order— the correct five buttons.Suppose that there locks are redesigned so that sets of as many as ninebuttons or as few as one button could serve as combinations. How manyadditional combination would this allow?

Solution: There are 210 configurations of the ten buttons. We are toexclude those in which none or all of the buttons are depressed. The totalnumber of additional combinations is therefore

210 − 1 − 1 −

(

10

5

)

= 770.

272 Example Form a set of n ≥ 3 points on the plane, no three collinear,

1. how many lines are determined?

2. how many straight lines pass through a particular point?

3. how many triangles are determined?

4. how many triangles have a particular point as a vertex?

Solution: (i) Since the points are not collinear and any two pointsdetermine a straight line, there are

(

n

2

)

lines in total.(ii) Once a point is chosen, there remains to choose another point. Wecan do this in n − 1 ways.(iii) A triangle is determined by 3 non-collinear points. We can choosetriangles then in

(

n

3

)

ways.(iv) Once a point is chosen, there remain to choose two points from theremaining n − 1 points. We can do this in

(

n−1

2

)

ways.

Page 104: Discretemaths Book

40 Chapter 3

Aliter: The number of triangles that can be formed by excluding anyparticular point is

(

n−1

3

)

. From the total number of triangles, we subtractthose that do not have a particular point as a vertex:

(

n

3

)

−(

n−1

3

)

=(

n−1

2

)

as before.

Ad Pleniorem Scientiam

273 Problem Four writers must write a book containing seventeenchapters. The first and third writers must each write five chapters, thesecond must write four chapters, and the fourth writer must write threechapters. How many ways can the book be written? What if the first andthird writers had to write ten chapters combined, but it did not matterwhich of them wrote how many (e.g., the first could write ten and the thirdnone, the first could write none and the third one, etc.)?

Answer:(

17

5

)(

12

5

)(

7

4

)(

3

3

)

;(

17

3

)(

14

4

)

210

3.7 The Binomial Theorem

We recall that the symbol(

n

k

)

, n, k ∈ N, 0 ≤ k ≤ n, counts the number ofways of selecting k different objects from n different objects. UsingPascal’s Identity

(

n

k

)

=

(

n − 1

k − 1

)

+

(

n − 1

k

)

,(3.10)

we obtain Pascal’s Triangle.(

0

0

)

(

1

0

) (

1

1

)

(

2

0

) (

2

1

) (

2

2

)

(

3

0

) (

3

1

) (

3

2

)(

3

3

)

Page 105: Discretemaths Book

The Binomial Theorem 41

(

4

0

) (

4

1

) (

4

2

) (

4

3

) (

4

4

)

(

5

0

) (

5

1

) (

5

2

) (

5

3

) (

5

4

) (

5

5

)

(

6

0

) (

6

1

) (

6

2

) (

6

3

) (

6

4

) (

6

5

) (

6

6

)

......................................

When the numerical values are substituted, the triangle then looks likethis.

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

.....................................

We see from Pascal’s Triangle that binomial coefficients are symmetric.This symmetry is easily justified by the identity

(

n

k

)

=(

n

n−k

)

. We alsonotice that the binomial coefficients tend to increase until they reach themiddle, and that they decrease symmetrically. That is, the

(

n

k

)

satisfy(

n

0

)

<(

n

1

)

< · · · <(

n

[n/2]−1

)

<(

n

[n/2]

)

>(

n

[n/2]+1

)

>(

n

[n/2]+2

)

> · · · >(

n

n−1

)

>(

n

n

)

if n is even, and that(

n

0

)

<(

n

1

)

< · · · <(

n

[n/2]−1

)

<(

n

[n/2]

)

=(

n

[n/2]+1

)

>(

n

[n/2]+2

)

>(

n

[n/2]+3

)

> · · · >(

n

n−1

)

>(

n

n

)

for odd n. We call this property theunimodality of the binomial coefficients. For example, without finding theexact numerical values we can see that

(

200

17

)

<(

200

69

)

and that(

200

131

)

=(

200

69

)

<(

200

99

)

.We now present some examples on the use of binomial coefficients.

274 Example Prove Newton’s Identity:(

n

i

)(

i

j

)

=

(

n

j

)(

n − j

j − i

)

,

for integers 0 ≤ j ≤ i ≤ n.

Page 106: Discretemaths Book

42 Chapter 3

Solution: We have(

n

i

)(

i

j

)

=n!i!

i!(n − i)!j!(i − j)!=

n!(n − j)!

(n − j)!j!(n − i)!(i − j)!

which is the same as(

n

j

)(

n − j

i − j

)

.

275 Example Prove Pascal’s Identity:(

n

k

)

=

(

n − 1

k − 1

)

+

(

n − 1

k

)

,

for integers 1 ≤ k ≤ n.

Solution: We have(

n − 1

k − 1

)

+

(

n − 1

k

)

=(n − 1)!

(k − 1)!(n − k)!+

(n − 1)!

k!(n − k − 1)!

=(n − 1)!

(n − k − 1)!(k − 1)!

(

1

n − k+

1

k

)

=(n − 1)!

(n − k − 1)!(k − 1)!

n

(n − k)k

=n!

(n − k)!k!

=

(

n

k

)

276 Example The Catalan number of order n is defined as

Cn =1

n + 1

(

2n

n

)

.

Prove that Cn is an integer for all natural numbers n.

Solution: Observe that1

n + 1

(

2n

n

)

=

(

2n

n

)

(

2n

n − 1

)

,

the difference of two integers.

Page 107: Discretemaths Book

The Binomial Theorem 43

277 Example (PUTNAM 1972) Shew that no four consecutive binomialcoefficients

(

n

r

)

,

(

n

r + 1

)

,

(

n

r + 2

)

,

(

n

r + 3

)

(n, r positive integers and r + 3 ≤ n) are in arithmetic progression.

Solution: Assume thata =

(

n

r

)

, a + d =(

n

r+1

)

, a + 2d =(

n

r+2

)

, a + 3d =(

n

r+3

)

. This yields

2

(

n

r + 1

)

=

(

n

r

)

+

(

n

r + 2

)

,

or equivalently

2 =r + 1

n − r+

n − r − 1

r + 2(∗).

This is a quadratic equation in r, having r as one of its roots. Thecondition that the binomial coefficients are in arithmetic progressionmeans that r + 1 is also a root of (∗). Replacing r by n − r − 2 we alsoobtain

2 =n − r − 1

r + 2+

r + 1

n − r,

which is the same as (∗). This means that n − r − 3 and n − r − 2 are alsoroots of (∗). Since a quadratic equation can only have two roots, we musthave r = n − r − 3. The four binomial coefficients must then be

(

2r + 3

r

)

,

(

2r + 3

r + 1

)

,

(

2r + 3

r + 2

)

,

(

2r + 3

r + 3

)

.

But these cannot be in an arithmetic progression, since binomialcoefficients are unimodal and symmetric.

278 Example Let N(a) denote the number of solutions to the equationa =

(

n

k

)

for nonnegative integers n, k. For example,N(1) = ∞, N(3) = N(5) = 2,N(6) = 3, etc. Prove that N(a) ≤ 2 + 2 log2 a.

Solution: Let b be the first time that(

2b

b

)

> a. By the unimodality of thebinomial coefficients,

(

i+j

i

)

=(

i+j

j

)

is monotonically increasing in i and j.Hence

(

b + i + b + j

b + j

)

≥(

b + b + j

b

)

≥(

2b

b

)

> a

Page 108: Discretemaths Book

44 Chapter 3

for all i, j ≥ 0. Hence(

i+j

j

)

= a implies i < b, or j < b. Also, for each fixedvalue of i (or j),

(

i+j

i

)

= a has at most one solution. It follows thatN(a) < 2b. Since

a ≥(

2(b − 1)

b − 1

)

≥ 2b−1,

it follows that b ≤ log2 a + 1, and the statement is proven.We now use Pascal’s Triangle in order to expand the binomial

(a + b)n.

The Binomial Theorem states that for n ∈ Z, n ≥ 0,

(1 + x)n =

n∑

k=0

(

n

k

)

xk.

As a way of proving this, we observe that expanding

(1 + x)(1 + x) · · · (1 + x)︸ ︷︷ ︸n times

consists of adding up all the terms obtained from multiplying either a 1 ora x from the first set of parentheses times either a 1 or an x from thesecond set of parentheses etc. To get xk, x must be chosen from exactlyk of the sets of parentheses. Thus the number of xk terms is

(

n

k

)

. Itfollows that

(1 + x)n =

(

n

0

)

+

(

n

1

)

x +

(

n

2

)

x2 + · · · +(

n

n

)

xn =

n∑

k=0

(

n

k

)

xk.

279 Example Prove thatn∑

k=0

(

n

k

)

= 2n.

Solution: This follows from letting x = 1 in the expansion

(1 + x)n =

n∑

k=0

(

n

k

)

xk.

Page 109: Discretemaths Book

The Binomial Theorem 45

280 Example Prove that for integer n ≥ 1,

n∑

j=i

(

n

j

)(

j

i

)

=

(

n

i

)

2n−i, i ≤ n.

Solution: Recall that by Newton’s Identity

(

n

j

)(

j

i

)

=

(

n

i

)(

n − i

j − i

)

.

Thus

n∑

j=0

(

n

j

)(

j

i

)

=

(

n

i

) n∑

j=0

(

n − i

j − i

)

.

But upon re-indexing

n∑

j=0

(

n − i

j − i

)

=

n−i∑

j=0

(

n − i

j

)

= 2n−i,

by the preceding problem. Thus the assertion follows.

281 Example Prove that

k≤n

(

m + k

k

)

=

(

n + m + 1

n

)

.

Page 110: Discretemaths Book

46 Chapter 3

Solution: Using Pascal’s Identityn∑

k=0

(

k + m

k

)

=

(

0 + m

−1

)

+

(

0 + m

0

)

+

(

1 + m

1

)

+

(

2 + m

2

)

+

(

3 + m

3

)

+ · · · +(

n − 1 + m

n − 1

)

+

(

n + m

n

)

=

(

1 + m

0

)

+

(

1 + m

1

)

+

(

2 + m

2

)

+

(

3 + m

3

)

+ · · · +(

n − 1 + m

n − 1

)

+

(

n + m

n

)

=

(

2 + m

1

)

+

(

2 + m

2

)

+

(

3 + m

3

)

+ · · · +(

n − 1 + m

n − 1

)

+

(

n + m

n

)

=

(

3 + m

2

)

+

(

3 + m

3

)

+ · · · +(

n − 1 + m

n − 1

)

+

(

n + m

n

)

...

=

(

n + m

n − 1

)

+

(

n + m

n

)

=

(

n + m + 1

n

)

,

which is what we wanted.

282 Example Find a closed formula for∑

0≤k≤m

(

m

k

)(

n

k

)−1

n ≥ m ≥ 0.

Solution: Using Newton’s Identity,∑

0≤k≤m

(

m

k

)(

n

k

)−1

=

(

n

m

)−1 ∑

0≤k≤m

(

n − k

m − k

)

.

Re-indexing,∑

0≤k≤m

(

n − k

m − k

)

=∑

k≤m

(

n − m + k

k

)

=

(

n + 1

m

)

,

Page 111: Discretemaths Book

The Binomial Theorem 47

by the preceding problem. Thus

0≤k≤m

(

m

k

)(

n

k

)−1

=

(

n + 1

m

)

/

(

n

m

)

=n + 1

n + 1 − m.

283 Example Simplify∑

0≤k≤50

(

100

2k

)

.

Solution: By the Binomial Theorem

(1 + 1)100 =

(

100

0

)

+

(

100

1

)

+

(

100

2

)

+ . . . +

(

100

99

)

+

(

100

100

)

(1 − 1)100 =

(

100

0

)

(

100

1

)

+

(

100

2

)

− . . . −

(

100

99

)

+

(

100

100

)

,

whence summing both columns

2100 = 2

(

100

0

)

+ 2

(

100

2

)

+ . . . + 2

(

100

100

)

.

Dividing by 2, the required sum is thus 299.

284 Example Simplify50∑

k=1

(

100

2k − 1

)

.

Solution: We know that100∑

k=0

(

100

k

)

= 2100

and50∑

k=0

(

100

2k

)

= 299.

The desired sum is the difference of these two values 2100 − 299 = 299.

Page 112: Discretemaths Book
Page 113: Discretemaths Book

The Binomial Theorem 49

and(

10

4

)

(2x)4(9)6 ≥(

10

5

)

(2x)5(9)5.

After simplifying the factorials, the two inequalities sought are

x ≥ 18/7

and15/4 ≥ x.

The only integral x that satisfies this is x = 3.

288 Example Prove that for integer n ≥ 1,

n∑

k=1

k

(

n

k

)

= n2n−1.

Solution: Let f(x) = (1 + x)n =∑n

k=0

(

n

k

)

xk. Then

f ′(x) = n(1 + x)n−1 =

n∑

k=0

k

(

n

k

)

xk−1.

Letting x = 1 we obtain

n2n−1 =

n∑

k=0

k

(

n

k

)

,

which is what we wanted.Aliter: Using the absorption identity

n∑

k=0

n

(

n − 1

k − 1

)

=

n∑

k=0

k

(

n

k

)

,

with the convention that(

n−1

−1

)

= 0. But since

n∑

k=0

(

n − 1

k − 1

)

=

n−1∑

k=0

(

n − 1

k

)

= 2n−1,

we obtain the result once again.

Page 114: Discretemaths Book

50 Chapter 3

289 Example Prove that for all n ∈ N, n ≥ 1,

n∑

k=1

k2

(

n

k

)

= n(n + 1)2n−2.

Solution: Let f(x) = (1 + x)n =∑n

k=0

(

n

k

)

xk. Then

f ′(x) = n(1 + x)n−1 =

n∑

k=0

k

(

n

k

)

xk−1

and

xf ′(x) = nx(1 + x)n−1 =

n∑

k=0

k

(

n

k

)

xk.

Differentiating this last expression,

f ′(x) + xf ′′(x) = n(1 + x)n−1 + n(n − 1)x(1 + x)n−2

which equalsn∑

k=0

k2

(

n

k

)

xk−1.

Letting x = 1 in the above expression,

n2n−1 + n(n − 1)2n−1 =

n∑

k=0

k2

(

n

k

)

,

whence the result follows.

290 Example Find a closed formula for

n∑

k=0

1

k + 1

(

n

k

)

.

Solution: We have∫ 1

0

(1 + x)n dx =

∫ 1

0

n∑

k=0

(

n

k

)

xk dx.

Page 115: Discretemaths Book

The Binomial Theorem 51

Evaluating these integrals,

2n+1

n + 1−

1

n + 1=

n∑

k=0

(

n

k

)

1

k.

Aliter: Using the absorption identityn∑

k=0

1

k + 1

(

n

k

)

=1

n + 1

n∑

k=0

(

n + 1

k + 1

)

=1

n + 1(2n+1 − 1).

291 Example Let n ∈ N. Prove thatn∑

k=1

(−1)k+1

k

(

n

k

)

= 1 + 1/2 + 1/3 + · · · + 1/n.

Solution: We evaluate the integral∫ 1

0

1 − (1 − x)n

xdx

in two different ways. First, making the substitution u = 1 − x, we obtain∫ 1

0

1 − (1 − x)n

xdx =

∫ 1

0

1 − un

1 − udu =

∫ 1

0

1 + u + u2 + · · · + un−1 du,

from where we obtain the dextral side of the identity we want to prove. Onthe other hand, evaluating the integral directly by expanding using theBinomial Theorem,

∫ 1

0

1 − (1 − x)n

xdx =

∫ 1

0

n∑

k=1

(

n

k

)

(−1)k−1xk−1 dx,

which, after integration, becomes the sinistral side of the equality wewanted to establish.

292 Example Prove that if m, n are nonnegative integers then(

n + 1

m + 1

)

=

n∑

k=m

(

k

m

)

.

Page 116: Discretemaths Book

52 Chapter 3

Solution: Using Pascal’s Identityn∑

k=m

(

k

m

)

=

(

m

m + 1

)

+

(

m

m

)

+

(

m + 1

m

)

+ · · · +(

n

m

)

=

(

m + 1

m + 1

)

+

(

m + 1

m

)

+

(

m + 2

m

)

+ · · · +(

n

m

)

=

(

m + 2

m + 1

)

+

(

m + 2

m

)

+

(

m + 3

m

)

+ · · · +(

n

m

)

...

=

(

n

m + 1

)

+

(

n

m

)

=

(

n + 1

m + 1

)

.

293 Example Find a closed form for∑

k≤n

k(k + 1).

Solution: LetS =

k≤n

k(k + 1).

Then

S/2! =∑

k≤n

k(k + 1)

2!=

k≤n

(

k + 1

2

)

.

By the preceding problem∑

k≤n

(

k + 1

2

)

=

(

n + 2

3

)

.

We gather that S = 2(

n+2

3

)

= n(n + 1)(n + 2)/3.

Ad Pleniorem Scientiam

294 Problem Prove that∑

0≤k≤n/2

(

n

2k + 1

)

= 2n−1.

Page 117: Discretemaths Book
Page 118: Discretemaths Book

54 Chapter 3

303 Problem Find a closed formula for∑

0≤k≤n

k

(

m − k − 1

n − k − 1

)

m > n ≥ 0.

(Hint: Write k = m − (m − k). Use the absorption identity to evaluaten∑

k=0

(m − k)

(

m − k − 1

n − k − 1

)

.)

304 Problem What is the exact numerical value of∑

k≤100

5k

k + 1

(

100

k

)

?

305 Problem Find n if(

10

4

)

+(

10

3

)

=(

n

4

)

.

Answer: 11.

306 Problem If(

1991

1

)

+

(

1991

3

)

+

(

1991

5

)

+ · · · +(

1991

1991

)

= 2a,

find a.

Answer: a = 1990.

307 Problem True or False:(

20

5

)

=

(

20

15

)

.

Answer: True.

308 Problem True or False:

49

(

48

9

)

= 10

(

49

10

)

.

Page 119: Discretemaths Book

The Binomial Theorem 55

Answer: True.

309 Problem What is the coefficient of x24y24 in the expansion

(2x3 + 3y2)20?

Answer:(

20

8

)

(28)(312).

310 Problem What is the coefficient of x12y7 in the expansion

(x3/2 + y)15?

Answer:(

15

8

)

.

311 Problem What is the coefficient of x4y6 in

(x√

2 − y)10?

Answer: 840.

312 Problem Shew that the binomial coefficients satisfy the followinghexagonal property:

(

n − 1

k − 1

)(

n

k + 1

)(

n + 1

k

)

=

(

n − 1

k

)(

n + 1

k + 1

)(

n

k − 1

)

.

313 Problem (AIME 1991) In the expansion

(1 + 0.2)1000 =

1000∑

k=0

(

1000

k

)

(0.2)k,

which one of the 1001 terms is the largest?

Answer: 166-th

314 Problem (PUTNAM 1971) Shew that for 0 < ε < 1 the expression

(x + y)n(x2 − (2 − ε)xy + y2)

Page 120: Discretemaths Book

56 Chapter 3

is a polynomial with positive coefficients for integral n sufficiently large.For ε = .002 find the smallest admissible value of n.

315 Problem Prove that for integer n ≥ 1,

n∑

k=1

k3

(

n

k

)

= n2(n + 3)2n−3.

316 Problem Which one of the following quantities is the largest?

(A)(

49

49

)

+(

50

49

)

+ · · · +(

99

49

)

(B)(

100

50

)

(C)(

50

0

)2+(

50

1

)2+ · · · +

(

50

50

)2

(D)(

200

100

)

Answer: D.

317 Problem Expand and simplify

(√

1 − x2 + 1)7 − (√

1 − x2 − 1)7.

318 Problem Which of the following assertions is false?

(A)(

200

17

)

<(

200

20

)

(B)(

199

25

)

+(

199

26

)

=(

200

174

)

(C)(

200

17

)

>(

200

184

)

(D) 200(

200

16

)

= 17(

199

17

)

Answer: D.

319 Problem Simplify(

5

5

)

+

(

6

5

)

+

(

7

5

)

+ · · · +(

999

5

)

Answer:(

1000

6

)

320 Problem Simplify(

15

1

)

(

15

2

)

+

(

15

3

)

(

15

4

)

· · · +(

15

13

)

(

15

14

)

Page 121: Discretemaths Book
Page 122: Discretemaths Book
Page 123: Discretemaths Book

Multinomial Theorem 59

328 Problem Find the coefficient of x4 in the expansion of

(1 + 3x + 2x3)10?

Answer: 6(

10

1,1,8

)

+ 34(

10

0,4,6

)

.

329 Problem Find the coefficient of x2y3z5 in the expansion of

(x + y + z)10?

Answer:(

10

2,3,5

)

.

Page 124: Discretemaths Book

60 Chapter 3

Page 125: Discretemaths Book

Chapter 4Arithmetic

4.1 Division Algorithm

330 Definition If a 6= 0, b are integers, we say that a divides b if there isan integer c such that ac = b. We write this as a|b.

If a does not divide b we write a 6 |b. It should be clear that if a|b andb 6= 0 then 1 ≤ |a| ≤ |b|.

331 Theorem 1. If c divides a and b then c divides any linearcombination of a and b. Tha is, if a, b, c,m,n are integers withc|a, c|b, then c|(am + nb).

2. Division by an integer is transitive. That is, if x, y, z are integers withx|y, y|z then x|z.

Proof There are integers s, t with sc = a, tc = b. Thus

am + nb = c(sm + tn),

giving c|(am + bn). Also, there are integers u, v with xu = y, yv = z.

Hence xuv = z, giving x|z.A very useful property of the integers is the following:

61

Page 126: Discretemaths Book

62 Chapter 4

332 Theorem Division Algorithm Let a, b be integers, a > 0. There existintegers q and r satisfying

b = aq + r, 0 ≤ r < a(4.1)

For example, 39 = 4 · 9 + 3. The Division Algorithm thus discriminatesintegers according to the remainder they leave upon division by a. Forexample, if a = 2, then according to the Division Algorithm, the integersmay be decomposed into the two families

A0 = {. . . − 4,−2, 0, 2, 4, . . .},

A1 = {. . . ,−5,−3,−1, 1, 3, 5, . . .}.

Therefore, all integers have one of the forms 2k or 2k + 1. We mention inpassing that every integer of the form 2k + 1 is also of the form 2t − 1, for2k + 1 = 2(k + 1) − 1, so it suffices to take t = k + 1.If a = 4 we may decompose the integers into the four families

B0 = {. . . ,−8,−4, 0, 4, 8, . . .},

B1 = {. . . ,−7,−3, 1, 5, 9, . . .},

B2 = {. . . ,−6,−2, 2, 6, 10, . . .},

B3 = {. . . ,−5,−1, 3, 7, 11, . . .}.

Therefore any integer will take one of the forms 4k, 4k + 1, 4k + 2 or4k + 3. Again, any integer of the form 4k + 1 is also of the form 4t − 3 andany integer of the form 4k + 3 is also of the form 4t − 1.

333 Example Show that the square of any integer is of the form 4k or ofthe form 4k + 1. That is, the square of any integer is either divisible by 4

or leaves remainder 1 upon division by 4.

Solution: If n is even, that is n = 2a, then n2 = (2a)2 = 4a2, which is ofthe form 4k. If n is odd, say n = 2t + 1, then n2 = (2t + 1)2 = 4(t2 + t) + 1,

which is of the form 4k + 1.

334 Example Show that no integer in the sequence

11, 111, 1111, 11111, . . .

is a perfect square.

Page 127: Discretemaths Book

Division Algorithm 63

Solution: Clearly 11 is not a square, so assume, that an integer of thissequence has n > 2 digits. If n > 2,

11 . . . 1︸ ︷︷ ︸n 1 ′s

= 11 . . . 11︸ ︷︷ ︸n−2 1 ′s

00 + 12 − 1 = 100 · 11 . . . 11︸ ︷︷ ︸n−2 1 ′s

+12 − 1.

Hence any integer in this sequence is of the form 4k − 1. By thepreceding problem, no integer of the form 4k − 1 can be a square. Thisfinishes the proof.

335 Example Show that n2 + 23 is divisible by 24 for infinitely many valuesof n.

Solution: Observe that n2 + 23 = n2 − 1 + 24 = (n − 1)(n + 1) + 24.Therefore the families of integers n = 24m ± 1,m = 0,±1,±2,±3, . . .

produce infinitely many values such that n2 + 23 is divisible by 24.

336 Example Show that the square of any prime greater than 3 leavesremainder 1 upon division by 12.

Solution: If p > 3 is prime, then p is of one of the forms 6k ± 1.Now,

(6k ± 1)2 = 12(3k2 ± k) + 1,

proving the assertion.

337 Example Prove that if p and 8p − 1 are prime, then 8p + 1 iscomposite.

Solution: If p = 3, 8p − 1 = 23 and 8p + 1 = 25, then the assertion is truefor p = 3. If p > 3, then either p = 3k + 1 or p = 3k + 2. Ifp = 3k + 1, 8p − 1 = 24k − 7 and 8p + 1 = 24k − 6, which is divisible by 6and hence not prime. If p = 3k + 2, 8p − 1 = 24k − 15 is not a prime, .

338 Example Show that if 3n + 1 is a square, then n + 1 is the sum ofthree squares.

Solution: Clearly 3n + 1 is not a multiple of 3, and so 3n + 1 = (3k ± 1)2.

Page 128: Discretemaths Book

64 Chapter 4

Therefore

n + 1 =(3k ± 1)2 − 1

3+ 1 = 3k2 ± 2k + 1 = k2 + k2 + (k ± 1)2,

as we wanted to show.

339 Example (AHSME 1976) Let r be the common remainder when1059, 1417 and 2312 are divided by d > 1. Find d − r.

Solution: By the division algorithm there are integers q1, q2, q3 with1059 = dq1 + r, 1417 = dq2 + r and 2312 = dq3 + r. Subtracting we get1253 = d(q3 − q1), 895 = d(q3 − q2) and 358 = d(q2 − q1). Notice that d isa common divisor of 1253, 895, and 358. As 1253 = 7 · 179, 895 = 5 · 179,and 358 = 2 · 179, we see that 179 is the common divisor greater than 1 ofall three quantities, and so d = 179. Since 1059 = 179q1 + r, and1059 = 5 · 179 + 164, we deduce that r = 164. Finally, d − r = 15.

340 Example Show that from any three integers, one can always choosetwo so that a3b − ab3 is divisible by 10.

Solution: It is clear that a3b − ab3 = ab(a − b)(a + b) is always even, nomatter which integers are substituted. If one of the three integers is of theform 5k, then we are done. If not, we are choosing three integers that liein the residue classes 5k ± 1 or 5k ± 2. By the Pigeonhole Principle, twoof them must lie in one of these two groups, and so there must be twowhose sum or whose difference is divisible by 5. The assertion follows.

Ad Pleniorem Scientiam

341 Problem Find all positive integers n for which

n + 1|n2 + 1.

342 Problem If 7|3x + 2 prove that 7|(15x2 − 11x + 14.).

343 Problem Show that the square of any integer is of the form 3k or3k + 1.

Page 129: Discretemaths Book

Division Algorithm 65

344 Problem Prove that if 3|(a2 + b2), then 3|a and 3|b

(Hint: Argue by contradiction. Assume a = 3k ± 1 or b = 3m ± 1.)

345 Problem Show that if the sides of a right triangle are all integers, then3 divides one of the lengths of a side.

346 Problem Given that 5 divides (n + 2), which of the following aredivisible by 5

n2 − 4, n2 + 8n + 7, n4 − 1, n2 − 2n?

347 Problem Prove that there is no prime triplet of the form p, p + 2, p + 4,except for 3, 5, 7.

348 Problem Find the largest positive integer n such that

(n + 1)(n4 + 2n) + 3(n3 + 57)

be divisible by n2 + 2.

Answer: 13

349 Problem Demonstrate that if n is a positive integer such that 2n + 1 isa square, then n + 1 is the sum of two consecutive squares.

350 Problem Show that the product of two integers of the form 4n + 1 isagain of this form. Use this fact and an argument by contradiction similarto Euclid’s to prove that there are infinitely many primes of the form 4n−1.

351 Problem Prove that there are infinitely many primes of the form6n − 1.

352 Problem Prove that there are infinitely many primes p such that p − 2

is not prime.

353 Problem Demonstrate that there are no three consecutive oddintegers such that each is the sum of two squares greater than zero.

Page 130: Discretemaths Book

66 Chapter 4

354 Problem Let n > 1 be a positive integer. Prove that if one of thenumbers 2n − 1, 2n + 1 is prime, then the other is composite.

355 Problem Prove that there are infinitely many integers n such that4n2 + 1 is divisible by both 13 and 5.

356 Problem Prove that any integer n > 11 is the sum of two positivecomposite numbers.

Hint: Think of n − 6 if n is even and n − 9 if n is odd.

357 Problem Prove that 3 never divides n2 + 1.

358 Problem Show the existence of infinitely many natural numbers x, y

such that x(x + 1)|y(y + 1) but

x 6 |y and (x + 1) 6 |y,

and alsox 6 |(y + 1) and (x + 1) 6 |(y + 1).

Hint: Try x = 36k + 14, y = (12k + 5)(18k + 7).

4.2 The Decimal Scale

Any natural number n can be written in the form

n = a010k + a110

k−1 + a210k−2 + · · · + ak−110 + ak

where 1 ≤ a0 ≤ 9, 0 ≤ aj ≤ 9, j ≥ 1. This is the decimal representation ofn. For example

65789 = 6 · 104 + 5 · 103 + 7 · 102 + 8 · 10 + 9.

359 Example Find a reduced fraction equivalent to the repeating decimal0.123 = 0.123123123 . . . .

Page 131: Discretemaths Book

The Decimal Scale 67

Solution: Let N = 0.123123123 . . . . Then 1000N = 123.123123123 . . . .

Hence 1000N − N = 123, whence N =123

999=

41

333.

360 Example What are all the two-digit positive integers in which thedifference between the integer and the product of its two digits is 12?

Solution: Let such an integer be 10a + b, where a, b are digits. Solve10a + b − ab = 12 for a getting

a =12 − b

10 − b= 1 +

2

10 − b.

Since a is an integer, 10 − b must be a positive integer that divides 2. Thisgives b = 8, a = 2 or b = 9, a = 3. Thus 28 and 39 are the only suchintegers.

361 Example Find all the integers with initial digit 6 such that if this initialinteger is suppressed, the resulting number is 1/25 of the original number.

Solution: Let x be the integer sought. Then x = 6 · 10n + y where y is apositive integer. The given condition stipulates that

y =1

25(6 · 10n + y) ,

that is,

y =10n

4= 25 · 10n−2.

This requires n ≥ 2, whence y = 25, 250, 2500, 25000, etc.. Thereforex = 625, 6250, 62500, 625000, etc..

362 Example (IMO 1968) Find all natural numbers x such that the productof their digits (in decimal notation) equals x2 − 10x − 22.

Solution: Let x have the form

x = a0 + a110 + a2102 + · · · + an10n, ak ≤ 9, an 6= 0.

Let P(x) be the product of the digits of x, P(x) = x2 − 10x − 22. NowP(x) = a0a1 · · ·an ≤ 9nan < 10nan ≤ x (strict inequality occurs when x

Page 132: Discretemaths Book
Page 133: Discretemaths Book

The Decimal Scale 69

365 Example (AIME 1987) An ordered pair (m,n) of non-negativeintegers is called simple if the addition m + n requires no carrying. Findthe number of simple ordered pairs of non-negative integers that add to1492.

Solution: Observe that there are d + 1 solutions to x + y = d, where x, y

are positive integers and d is a digit. These are

(0 + d), (1 + d − 1), (2 + d − 2), . . . , (d + 0)

Since there is no carrying, we search for the numbers of solutions of thisform to x+y = 1, u+ v = 4, s+ t = 9, and a+b = 2. Since each separatesolution may combine with any other, the total number of simple pairs is

(1 + 1)(4 + 1)(9 + 1)(2 + 1) = 300.

366 Example (AIME 1992) For how many pairs of consecutive integers in

{1000, 1001, . . . , 2000}

is no carrying required when the two integers are added?

Solution: Other than 2000, a number on this list has the formn = 1000 + 100a + 10b + c, where a, b, c are digits. If there is no carryingin n + n + 1 then n has the form

1999, 1000 + 100a + 10b + 9, 1000 + 100a + 99, 1000 + 100a + 10b + c

with 0 ≤ a, b, c ≤ 4, i.e., five possible digits. There are 53 = 125 integersof the form 1000 + 100a + 10b + c, 0 ≤ a, b, c ≤ 4, 52 = 25 integers of theform 1000 + 100a + 10b + 9, 0 ≤ a, b ≤ 4, and 5 integers of the form1000 + 100a + 99, 0 ≤ a ≤ 4. The total of integers sought is thus125 + 25 + 5 + 1 = 156.

367 Example (AIME 1994) Given a positive integer n, let p(n) be theproduct of the non-zero digits of n. (If n has only one digit, then p(n) isequal to that digit.) Let

S = p(1) + p(2) + · · · + p(999).

Find S.

Page 134: Discretemaths Book

70 Chapter 4

Solution: If x = 0, put m(x) = 1, otherwise put m(x) = x. We use threedigits to label all the integers, from 000 to 999 If a, b, c are digits, thenclearly p(100a + 10b + c) = m(a)m(b)m(c). Thus

p(000) + p(001) + p(002) + · · · + p(999) = m(0)m(0)m(0) + m(0)m(0)m(1) + m(0)m(0)m(2) + · · ·· · · + m(9)m(9)m(9)

= (m(0) + m(1) + · · · + m(9))3

= (1 + 1 + 2 + · · · + 9)3

= 463

= 97336.

HenceS = p(001) + p(002) + · · · + p(999)

= 97336 − p(000)

= 97336 − m(0)m(0)m(0)

= 97335.

368 Example (AIME 1992) Let S be the set of all rational numbers r,0 < r < 1, that have a repeating decimal expansion of the form

0.abcabcabc . . . = 0.abc,

where the digits a, b, c are not necessarily distinct. To write the elementsof S as fractions in lowest terms, how many different numerators arerequired?

Solution: Observe that 0.abcabcabc . . . =abc

999, and that 999 = 33 · 37. If

abc is neither divisible by 3 nor by 37, the fraction is already in lowestterms. By Inclusion-Exclusion there are

999 −

(

999

3+

999

37

)

+999

3 · 37 = 648

such fractions. Also, fractions of the forms

37where s is divisible by 3 but

not by 37 are in S. There are 12 fractions of this kind (with s = 3, 6, 9, 12,

. . . , 36). We do not consider fractions of the forml

3t, t ≤ 3 with l divisible

by 37 but not by 3, because these fractions are > 1 and hence not in S.The total number of distinct numerators in the set of reduced fractions isthus 640 + 12 = 660.

Page 135: Discretemaths Book

The Decimal Scale 71

Problems

369 Problem Find an equivalent fraction for the repeating decimal 0.3172.

370 Problem A two-digit number is divided by the sum of its digits. Whatis the largest possible remainder?

371 Problem Show that the integer

11 . . . 11︸ ︷︷ ︸221 1 ′s

is a composite number.

372 Problem Let a and be be the integers

a = 111 . . . 1︸ ︷︷ ︸m 1 ′s

b = 1 000 . . . 0︸ ︷︷ ︸m−1 0 ′s

5.

Show that ab + 1 is a perfect square.

373 Problem What digits appear on the product

3 . . . 3︸ ︷︷ ︸666 3 ′s

· 6 . . . 6︸ ︷︷ ︸666 6 ′s

?

374 Problem Show that there exist no integers with the following property:if the initial digit is suppressed, the resulting integer is 1/35 of the originalnumber.

375 Problem Show that the sum of all the integers of n digits, n ≥ 3, is

494 99 . . . 9︸ ︷︷ ︸n−3 9 ′s

55 00 . . . 0︸ ︷︷ ︸n−2 0 ′s

.

Page 136: Discretemaths Book

72 Chapter 4

376 Problem Show that for any positive integer n,

11 . . . 1︸ ︷︷ ︸2n 1 ′s

− 22 . . . 2︸ ︷︷ ︸n 2 ′s

is a perfect square.

377 Problem A whole number is equal to the arithmetic mean of all thenumbers obtained from the given number with the aid of all possiblepermutation of its digits. Find all whole numbers with that property.

378 Problem The integer n is the smallest multiple of 15 such that everydigit of n is either 0 or 8. Compute

n

15.

379 Problem Show that Champernowne’s number

0.12345678910111213141516171819202122 . . .

, which is the sequence of natural numbers written after the decimal point,is irrational.

380 Problem Given that1

49= 0.020408163265306122448979591836734693877551, find the last

thousand digits of1 + 50 + 502 + · · · + 50999.

381 Problem Let t be a positive real number. Prove that there is a positiveinteger n such that the decimal expansion of nt contains a 7.

382 Problem (AIME 1989) Suppose that n is a positive integer and d is asingle digit in base-ten. Find n if

n

810= 0.d25d25d25d25d25 . . .

383 Problem (AIME 1988) Find the smallest positive integer whose cubeends in 888.

Page 137: Discretemaths Book

Non-decimal Scales 73

384 Problem (AIME 1986) In the parlor game, the “magician” asks one ofthe participants to think of a three-digit number abc, where a, b, c

represent the digits of the number in the order indicated. The magicianasks his victim to form the numbers

acb, bac, cab, cba,

to add these numbers and to reveal their sum N. If told the value of N,the magician can identify abc. Play the magician and determine abc ifN = 319.

385 Problem (AIME 1988) For any positive integer k, let f1(k) denote thesquare of the sums of the digits of k. For n ≥ 2, let fn(k) = f1(fn−1(k)).

Find f1988(11).

386 Problem (IMO 1969) Determine all three-digit numbers N that are

divisible by 11 and such thatN

11equals the sum of the squares of the

digits of N.

387 Problem (IMO 1962) Find the smallest natural number having the lastdigit 6 and if this 6 is erased and put in from of the other digits, theresulting number is four times as large as the original number.

4.3 Non-decimal Scales

The fact that most people have ten fingers has fixed our scale of notationto the decimal. Given any positive integer r > 1, we can, however,express any number x in base r.If n is a positive integer, and r > 1 is an integer, then n has the base-rrepresentation

n = a0 + a1r + a2r2 + · · · + akr

k, 0 ≤ at ≤ r − 1, ak 6= 0, rk ≤ n < rk+1.

We use the convention that we shall refer to a decimal number withoutreferring to its base, and to a base-r number by using the subindex r.

388 Example Express the decimal number 5213 in base-seven.

Page 138: Discretemaths Book

74 Chapter 4

Solution: Observe that 5213 < 75. We thus want to find0 ≤ a0, . . . , a4 ≤ 6, a4 6= 0 such that

5213 = a474 + a37

3 + a272 + a17 + a0.

Dividing by 74, we obtain 2+ proper fraction = a4+ proper fraction. Thismeans that a4 = 2. Thus 5213 = 2 · 74 + a37

3 + a272 + a17 + a0 or

411 = 5213 = a373 + a27

2 + a17 + a0. Dividing by 73 this last equality weobtain 1+ proper fraction = a3+ proper fraction, and so a3 = 1.

Continuing in this way we deduce that 5213 = 211257.

The method of successive divisions used in the preceding problem canbe conveniently displayed as

7 5212 57 744 27 106 17 15 17 2 2

The central column contains the successive quotients and the rightmostcolumn contains the corresponding remainders. Reading from the lastremainder up, we recover 5213 = 211257.

389 Example Write 5627 in base-five.

Solution: 5627 = 5 · 72 + 6 · 7 + 2 = in decimal scale, so the problemreduces to convert 289 to base-five. Doing successive divisions,

5 289 45 57 25 11 15 2 2

Thus 5627 = 289 = 21245.

390 Example Express the fraction13

16in base-six.

Solution: Write13

16=

a1

6+

a2

62+

a3

63+

a4

64+ · · ·

Page 139: Discretemaths Book

Non-decimal Scales 75

Multiplying by 6, we obtain 4+ proper fraction = a1+ proper fraction, soa1 = 4. Hence

13

16−

4

6=

7

48=

a2

62+

a3

63+

a4

64+ · · ·

Multiply by 62 we obtain 5+ proper fraction = a2+ proper fraction, and soa2 = 5. Continuing in this fashion

13

16=

4

6+

5

62+

1

63+

3

64= 0.45136.

We may simplify this procedure of successive multiplications by recurringto the following display:

6 1316

46 7

85

6 14

16 1

23

The third column contains the integral part of the products of the firstcolumn and the second column. Each term of the second column fromthe second on is the fractional part of the product obtained in thepreceding row. Thus 6 · 13

16− 4 = 7

8, 6 · 7

8− 5 = 1

4, etc..

391 Example Prove that 4.41r is a perfect square in any scale of notation.

Solution:

4.41r = 4 +4

r+

4

r2=

(

2 +1

r

)2

392 Example (AIME 1986) The increasing sequence

1, 3, 4, 9, 10, 12, 13, . . .

consists of all those positive integers which are powers of 3 or sums ofdistinct powers or 3. Find the hundredth term of the sequence.

Solution: If the terms of the sequence are written in base-three, theycomprise the positive integers which do not contain the digit 2. Thus theterms of the sequence in ascending order are

13, 103, 113, 1003, 1013, 1103, 1113, . . .

Page 140: Discretemaths Book

76 Chapter 4

In the binary scale these numbers are, of course, the ascending naturalnumbers 1, 2, 3, 4, . . .. Therefore to obtain the 100th term of the sequencewe write 100 in binary and then translate this into ternary: 100 = 11001002

and 11001003 = 36 + 35 + 32 = 981.

393 Example (AHSME 1993) Given 0 ≤ x0 < 1, let

xn =

{2xn−1 if 2xn−1 < 1,

2xn−1 − 1 if 2xn−1 ≥ 1.

for all integers n > 0. For how many x0 is it true that x0 = x5?

Solution: Write x0 in binary,

x0 =

∞∑

k=1

ak

2k, ak = 0 or 1.

The algorithm given moves the binary point one unit to the right. For x0 toequal x5 we need (0.a1a2a3a4a5a6a7 . . .)2 = (0.a6a7a8a9a10a11a12 . . .)2.

This will happen if and only if x0 has a repeating expansion witha1a2a3a4a5 as the repeating block. There are 25 = 32 such blocks. But ifa1 = a2 = · · · = a5 = 1 then x0 = 1, which lies outside ]0, 1[. The totalnumber of values for which x0 = x5 is therefore 32 − 1 = 31.

Problems

394 Problem Express the decimal number 12345 in every scale frombinary to base-nine.

395 Problem Distribute the 27 weights of 12, 22, 32, . . . , 272 lbs each intothree separate piles, each of equal weight.

396 Problem Let C denote the class of positive integers which, whenwritten in base-three, do not require the digit 2. Prove that no threeintegers in C are in arithmetic progression.

397 Problem What is the largest integer that I should be permitted tochoose so that you may determine my number in twenty “yes” or “no”questions?

Page 141: Discretemaths Book

Well-Ordering Principle 77

398 Problem Let bxc denote the greatest integer less than or equal to x.Does the equation

bxc + b2xc + b4xc + b8xc + b16xc + b32xc = 12345

have a solution?

4.4 Well-Ordering Principle

The set N = {1, 2, 3, 4, . . .} of natural numbers is endowed with twooperations, addition and multiplication, that satisfy the following propertiesfor natural number a, b, and c:

1. Closure: a + b and ab are also natural numbers,

2. Commutativity: a + b = b + a and ab = ba,

3. Associative Laws: (a + b) + c = a + (b + c) and (ab)c = a(bc),

4. Distributive Law: a(b + c) = ab + ac

5. Multiplicative Identity: 1a = a.

One further property of the natural numbers is the following.

Well-Ordering Axiom: Every non-empty subset S of the natural numbershas a least element.As an example of the use of the Well-Ordering Axiom let us prove thatthere is no integer between 0 and 1.

399 Example Prove that there is no integer in the open interval ]0, 1[.

Solution: Assume to the contrary that the set S of integers in ]0, 1[ isnon-empty. As a set of positive integers, by Well-Ordering it must containa least element, say m. Since 0 < m < 1, we have 0 < m2 < m < 1. Butthis last string of inequalities says that m2 is an integer in ]0, 1[ which issmaller than m, the smallest integer in ]0, 1[. This contradiction showsthat m cannot exist.Recall that an irrational number is one that cannot be represented as theratio of two integers.

Page 142: Discretemaths Book

78 Chapter 4

400 Example Prove that√

2 is irrational.

Solution: The proof is by contradiction. Suppose that√

2 were rational,i.e., that

√2 = a

bfor some integers a, b, b 6= 0. This implies that the set

A = {n√

2 : both n and n√

2 positive integers}

is non-empty since it contains a. By Well-Ordering, A has a smallestelement, say j = k

√2. As

√2 − 1 > 0,

j(√

2 − 1) = j√

2 − k√

2 =√

2(j − k), is a positive integer. Since 2 < 2√

2

implies 2 −√

2 <√

2 and also j√

2 = 2k, we see that

(j − k)√

2 = k(2 −√

2) < k√

2 = j.

Thus (j − k)√

2 is a positive integer in A which is smaller than j. Thiscontradicts the choice of j as the smallest integer in A and hence,finishes the proof.

401 Example Let a, b, c be integers such that a6 + 2b6 = 4c6. Show thata = b = c = 0.

Solution: Clearly we can restrict ourselves to non-negative numbers.Choose a triplet of non-negative integers a, b, c satisfying this equationand with

max(a, b, c) > 0

as small as possible. If a6 + 2b6 = 4c6, then a must be even, a = 2a1.

This leads to 32a61 + b6 = 2c6. This implies that b is even, b = 2b1 and so

16a61 + 32b6

1 = c6. This implies that c is even, c = 2c1 and soa6

1 + 2b61 = 4c6

1. But clearly max(a1, b1, c1) < max(a, b, c). We haveproduce a triplet of integers with a maximum smaller than the smallestpossible maximum, a contradiction.

402 Example (IMO 1988) If a, b are positive integers such thata2 + b2

1 + abis

an integer, then show thata2 + b2

1 + abmust be a square.

Page 143: Discretemaths Book

Well-Ordering Principle 79

Solution: Suppose thata2 + b2

1 + ab= k is a counterexample of an integer

which is not a perfect square, with max(a, b) as small as possible. Wemay assume without loss of generality that a < b for if a = b then

0 < k =2a2

a2 + 1= 2 −

2

a2 + 1< 2,

which forces k = 1, a square.Now, a2 + b2 − k(ab + 1) = 0 is a quadratic in b with sum of roots ka andproduct of roots a2 − k. Let b1, b be its roots, so b1 + b = ka, bb1 = a2 − k.

As a, k are positive integers, supposing b1 < 0 is incompatible witha2 + b2

1 = k(ab1 + 1). As k is not a perfect square, supposing b1 = 0 isincompatible with a2 + 02 = k(0 · a + 1). Also

b1 =a2 − k

b<

b2 − k

b= b −

k

b< b.

Thus we have shown b1 to be a positive integer witha2 + b2

1

1 + ab1

= k smaller

than b. This is a contradiction to the choice of b. Such a counterexample

k cannot exist, and soa2 + b2

1 + abmust be a perfect square. In fact, it can be

shown thata2 + b2

1 + abis the square of the greatest common divisor of a and

b.

Problems

403 Problem Find all integers solutions of a3 + 2b3 = 4c3.

404 Problem Prove that the equality x2 + y2 + z2 = 2xyz can hold forwhole numbers x, y, z only when x = y = z = 0.

405 Problem Show that the series of integral squares does not contain aninfinite arithmetic progression.

406 Problem Prove that x2 + y2 = 3(z2 + w2) does not have a positiveinteger solution.

Page 144: Discretemaths Book

80 Chapter 4

4.5 Mathematical Induction

The Principle of Mathematical Induction is based on the following fairlyintuitive observation. Suppose that we are to perform a task that involvesa certain finite number of steps. Suppose that these steps are sequential.Finally, suppose that we know how to perform the n-th step provided wehave accomplished the n − 1-th step. Thus if we are ever able to start thetask (that is, if we have a base case), then we should be able to finish it(because starting with the base we go to the next case, and then to thecase following that, etc.).We formulate the Principle of Mathematical Induction (PMI) as follows:

Principle of Mathematical Induction Suppose we have an assertionP(n) concerning natural numbers satisfying the following two properties:

(PMI I) P(k0) is true for some natural number k0,

(PMI II) If P(n − 1) is true then P(n) is true.

Then the assertion P(n) is true for every n ≥ k0.

407 Example Prove that the expression 33n+3 − 26n − 27 is a multiple of169 for all natural numbers n.

Let P(n) be the assertion “33n+3 − 26n − 27 is a multiple of 169.” Observethat 33(1)+3 − 26(1) − 27 = 676 = 4(169) so P(1) is true. Assume the truthof P(n − 1), that is, that there is an integer M such that

33(n−1)+3 − 26(n − 1) − 27 = 169M.

This entails33n − 26n − 1 = 169M.

Now33n+3 − 26n − 27 = 27 · 33n − 26n − 27

= 27(33n − 26n − 1) + 676n

= 27(169M) + 169 · 4n= 169(27M + 4n),

and so the truth of P(n − 1) implies the truth of P(n). The assertion thenfollows for all n ≥ 1 by PMI.

Page 145: Discretemaths Book

Mathematical Induction 81

408 Example Prove that

(1 +√

2)2n + (1 −√

2)2n

is an even integer and that

(1 +√

2)2n − (1 −√

2)2n = b√

2

for some positive integer b, for all integers n ≥ 1.

Solution: Let P(n) be the assertion: “

(1 +√

2)2n + (1 −√

2)2n

is an even integer and that

(1 +√

2)2n − (1 −√

2)2n = b√

2

for some positive integer b.” We see that P(1) is true since

(1 +√

2)2 + (1 −√

2)2 = 6,

and(1 +

√2)2 − (1 −

√2)2 = 4

√2.

Assume now that P(n − 1), i.e., assume that

(1 +√

2)2(n−1) + (1 −√

2)2(n−1) = 2N

for some integer N and that

(1 +√

2)2(n−1) − (1 −√

2)2(n−1) = a√

2

for some positive integer a. Consider now the quantity

(1 +√

2)2n + (1 −√

2)2n = (1 +√

2)2(1 +√

2)2n−2 + (1 −√

2)2(1 −√

2)2n−2

= (3 + 2√

2)(1 +√

2)2n−2 + (3 − 2√

2)(1 −√

2)2n−2

= 12N + 4a

= 2(6n + 2a),

Page 146: Discretemaths Book
Page 147: Discretemaths Book

Mathematical Induction 83

The Fibonacci sequence then goes like 0, 1, 1, 2, 3, 5, 8, 13, 21, . . . .

412 Example Prove that for integer n ≥ 1,

fn−1fn+1 = f2n + (−1)n+1.

Solution: If n = 1, then 2 = f0f2 = 12 + (−1)2 = f21 + (−1)1+1. If

fn−1fn+1 = f2n + (−1)n+1 then using the fact that fn+2 = fn + fn+1,

fnfn+2 = fn(fn + fn+1)

= f2n + fnfn+1

= fn−1fn+1 − (−1)n+1 + fnfn+1

= fn+1(fn−1 + fn) + (−1)n+2

= f2n+1 + (−1)n+2,

which establishes the assertion by induction.

413 Example Prove that a given square can be decomposed into n

squares, not necessarily of the same size, for all n = 4, 6, 7, 8, . . ..

Solution: A quartering of a subsquare increases the number of squaresby three (four new squares are gained but the original square is lost). Thefigure below shows that n = 4 is achievable.$.4in]

If n were achievable, a quartering would make {n,n + 3, n + 6, n + 9, . . .}

also achievable. We will show now that n = 6 and n = 8 are achievable.But this is easily seen from the figures below, and this finishes theproof.$.4in]

Sometimes it is useful to use the following version of PMI, known as thePrinciple of Strong Mathematical Induction (PSMI).

Principle of Strong Mathematical Induction Suppose we have anassertion P(n) concerning natural numbers satisfying the following twoproperties:

Page 148: Discretemaths Book

84 Chapter 4

(PSMI I) P(k0) is true for some natural number k0,

(PSMI II) If m < n and P(m), P(m + 1), . . . , P(n − 1) are true thenP(n) is true.

Then the assertion P(n) is true for every n ≥ k0.

414 Example In the country of SmallPesia coins only come in values of 3

and 5 pesos. Show that any quantity of pesos greater than or equal to 8

can be paid using the available coins.

Solution: We use PSMI. Observe that 8 = 3 + 5, 9 = 3 + 3 + 3, 10 = 5 + 5,so, we can pay 8, 9, or 10 pesos with the available coinage. Assume thatwe are able to pay n − 3, n − 2, and n − 1 pesos, that is, that 3x + 5y = k

has non-negative solutions for k = n − 3, n − 2 and n − 1. We will showthat we may also obtain solutions for 3x + 5y = k for k = n,n + 1 andn + 2. Now

3x + 5y = n − 3 =⇒ 3(x + 1) + 5y = n,

3x1 + 5y1 = n − 2 =⇒ 3(x1 + 1) + 5y1 = n + 1,

3x2 + 5y2 = n − 1 =⇒ 3(x2 + 1) + 5y2 = n + 2,

and so if the amounts n − 3, n − 2, n − 1 can be paid so cann,n + 1, n + 2. The statement of the problem now follows from PSMI.

415 Example (USAMO 1978) An integer n will be called good if we canwrite

n = a1 + a2 + · · · + ak,

where the integers a1, a2, . . . , ak are positive integers (not necessarilydistinct) satisfying

1

a1

+1

a2

+ · · · 1

ak

= 1.

Given the information that the integers 33 through 73 are good, prove thatevery integer ≥ 33 is good.

Solution: We first prove that if n is good, then 2n + 8 and 2n + 9 are alsogood. For assume that n = a1 + a2 + · · · + ak, and

1

a1

+1

a2

+ · · · 1

ak

= 1.

Page 149: Discretemaths Book
Page 150: Discretemaths Book
Page 151: Discretemaths Book
Page 152: Discretemaths Book

88 Chapter 4

The arrangement above shows that any integer comes in one of 5flavours: those leaving remainder 0 upon division by 5, those leavingremainder 1 upon division by 5, etc..Since n|(a − b) implies that ∃k ∈ Z such that nk = a − b, we deduce thata ≡ b mod n if and only if there is an integer k such that a = b + nk.

The following theorem is quite useful.

430 Theorem Let n ≥ 2 be an integer. If x ≡ y mod n and u ≡ v mod n

thenax + bu ≡ ay + bv mod n.

Proof As n|(x − y), n|(u − v) then there are integers s, t withns = x − y, nt = u − v. This implies that

a(x − y) + b(u − v) = n(as + bt),

which entails that,n|(ax + bu − ay − bv).

This last assertion is equivalent to saying

ax + bu ≡ ay + bv mod n.

This finishes the proof. q

431 Corollary Let n ≥ 2 be an integer. If x ≡ y mod n and u ≡ v mod n

then

xu ≡ yv mod n.

Proof Let a = u, b = y in Theorem 430. q

432 Corollary Let n > 1 be an integer, x ≡ y mod n and j a positiveinteger. Then xj ≡ yj mod n.

Proof Use repeteadly Corollary 431 with u = x, v = y. q

Page 153: Discretemaths Book

Congruences 89

433 Corollary Let n > 1 be an integer, x ≡ y mod n. If f is a polynomialwith integral coefficients then f(x) ≡ f(y) mod n.

434 Example Find the remainder when 61987 is divided by 37.

Solution: 62 ≡ −1 mod 37. Thus

61987 ≡ 6 · 61986 ≡ 6(62)993 ≡ 6(−1)996 ≡ −6 ≡ 31 mod 37

and the remainder sought is 31.

435 Example Find the remainder when

12233 · 455679 + 876533

is divided by 4.

Solution: 12233 = 12200 + 32 + 1 ≡ 1 mod 4. Similarly,455679 = 455600 + 76 + 3 ≡ 3, 87653 = 87600 + 52 + 1 ≡ 1 mod 4. Thus

12233 · 455679 + 876533 ≡ 1 · 3 + 13 ≡ 4 ≡ 0 mod 4.

This means that 12233 · 455679 + 876533 is divisible by 4.

436 Example Prove that 7 divides 32n+1 + 2n+2 for all natural numbers n.

Solution: Observe that

32n+1 ≡ 3 · 9n ≡ 3 · 2n mod 7

and2n+2 ≡ 4 · 2n mod 7

. Hence32n+1 + 2n+2 ≡ 7 · 2n ≡ 0 mod 7,

for all natural numbers n.

437 Example Prove the following result of Euler: 641|(232 + 1).

Page 154: Discretemaths Book
Page 155: Discretemaths Book

Congruences 91

441 Example Prove that 2k − 5, k = 0, 1, 2, . . . never leaves remainder 1

when divided by 7.

Solution: 21 ≡ 2, 22 ≡ 4, 23 ≡ 1 mod 7, and this cycle of three repeats.Thus 2k − 5 can leave only remainders 3, 4, or 6 upon division by 7.

442 Example (AIME 1994) The increasing sequence

3, 15, 24, 48, . . . ,

consists of those positive multiples of 3 that are one less than a perfectsquare. What is the remainder when the 1994-th term of the sequence isdivided by 1000?

Solution: We want 3|n2 − 1 = (n − 1)(n + 1). Since 3 is prime, thisrequires n = 3k + 1 or n = 3k − 1, k = 1, 2, 3, . . .. The sequence3k + 1, k = 1, 2, . . . produces the terms n2 − 1 = (3k + 1)2 − 1 which arethe terms at even places of the sequence of 3, 15, 24, 48, . . .. Thesequence 3k − 1, k = 1, 2, . . . produces the terms n2 − 1 = (3k − 1)2 − 1

which are the terms at odd places of the sequence 3, 15, 24, 48, . . .. Wemust find the 997th term of the sequence 3k + 1, k = 1, 2, . . .. Finally, theterm sought is (3(997) + 1)2 − 1 ≡ (3(−3) + 1)2 − 1 ≡ 82 − 1 ≡ 63

mod 1000. The remainder sought is 63.

Ad Pleniorem Scientiam

443 Problem Prove that 0, 1, 3, 4, 9, 10, and 12 are the only perfect squaresmod 13.

(Hint: It is enough to consider 02, 12, 22, . . . , 122. In fact, by observing thatr2 ≡ (13 − r)2 mod n, you only have to go half way.)

444 Problem Prove that there are no integers with x2 − 5y2 = 2.

(Hint: Find all the perfect squares mod 5.)

445 Problem Which digits must we substitute for a and b in 30a0b03 sothat the resulting integer be divisible by 13?

Page 156: Discretemaths Book
Page 157: Discretemaths Book

Congruences 93

458 Problem Find the last two digits of 3100.

459 Problem (USAMO 1986) What is the smallest integer n > 1, for whichthe root-mean-square of the first n positive integers is an integer?

Note. The root mean square of n numbers a1, a2, . . . , an is defined to be

(

a21 + a2

2 + · · · + a2n

n

)1/2

.

460 Problem If 62ab427 is a multiple of 99, find the digits a and b.

461 Problem Show that an integer is divisible by 2n, n = 1, 2, 3, . . . if thenumber formed by its last n digits is divisible by 2n.

462 Problem Find the last digit of

2333333334 · 9987737 + 12 · 21327 + 12123 · 99987.

463 Problem (AIME 1994) The increasing sequence

3, 15, 24, 48, . . . ,

consists of all those multiples of 3 which are one less than a square. Findthe remainder when the 1994th term is divided by 1000.

Answer: 63

464 Problem (AIME 1983) Let an = 6n + 8n. Find the remainder when a83

is divided by 49.

465 Problem Show that if 9|(a3 + b3 + c3), then 3|abc, for the integersa, b, c.

Page 158: Discretemaths Book
Page 159: Discretemaths Book

Miscellaneous Problems with Integers 95

This inequality in turn will follow from the inequality

bαc + bβc ≤ bα + βc(4.7)

which we will show valid for all real numbers α,β.Adding the inequalities bαc ≤ α, bβc ≤ β, we obtain bαc + bβc ≤ α + β.

Since bαc + bβc is an integer less than or equal to α + β, it must be lessthan or equal than the integral part of α + β, that is bαc + bβc ≤ bα + βc,as we wanted to show.Observe that (m + n)! = m!(m + 1)(m + 2) · · · (m + n). Thus cancelling afactor of m!,

(m + n)!

m!n!=

(m + 1)(m + 2) · · · (m + n)

n!

we see that the product of n consecutive positive integers is divisible byn!. If all the integers are negative, we may factor out a (−1)n, or if theyinclude 0, their product is 0. This gives the following theorem.

469 Theorem The product of n consecutive integers is divisible by n!.

470 Example Prove that n5 − 5n3 + 4n is always divisible by 120 for allintegers n.

Solution: We have

n5 − 5n3 + 4n = n(n2 − 4)(n2 − 1) = (n − 2)(n − 1)(n)(n + 1)(n + 2),

the product of 5 consecutive integers and hence divisible by 5! = 120.

471 Example Let A be a positive integer and let A ′ be the resultinginteger after a specific permutation of the digits of A. Show that ifA + A ′ = 1010 then A s divisible by 10.

Solution: Clearly, A and A ′ must have 10 digits each. Put

A = a10a9a8 . . . a1

andA ′ = b10b9b8 . . . b1,

Page 160: Discretemaths Book

96 Chapter 4

where ak, bk, k = 1, 2, . . . , 10 are the digits of A and A ′ respectively. AsA + A ′ = 10000000000, we must havea1 + b1 = a2 + b2 = · · · = ai + bi = 0 and

ai+1 + bi+1 = 10, ai+2 + bi+2 = · · · = a10 + b10 = 9,

for some subindex i, 0 ≤ i ≤ 9. Notice that if i = 9 there are no sumsai+2 + bi+2, ai+3 + bi+3, . . . and if i = 0 there are no sumsa1 + b1, . . . , ai + bi.

Adding,

a1 +b1 +a2 +b2 + · · ·+ai +bi +ai+1 +bi+1 + · · ·+a10 +b10 = 10+9(9− i).

If i is even, 10 + 9(9 − i) is odd and if i is odd 10 + 9(9 − i) is even. As

a1 + a2 + · · · + a10 = b1 + b2 + · · · + b10,

we have

a1+b1+a2+b2+· · ·+ai+bi+ai+1+bi+1+· · ·+a10+b10 = 2(a1+a2+· · ·+a10),

an even integer. We gather that i is odd, which entails that a1 = b1 = 0,

that is , A and A ′ are both divisible by 10.

472 Example (PUTNAM 1956) Prove that every positive integer has amultiple whose decimal representation involves all 10 digits.

Solution: Let n be an arbitrary positive integer with k digits. Letm = 1234567890 · 10k+1. Then all of the n consecutive integers

m + 1,m + 2, . . . ,m + n

begin with 1234567890 and one of them is divisible by n.

473 Example (PUTNAM 1966) Let 0 < a1 < a2 < . . . < amn+1 be mn + 1

integers. Prove that you can find either m + 1 of them no one of whichdivides any other, or n + 1 of them, each dividing the following.

Solution: Let, for each 1 ≤ k ≤ mn + 1, nk denote the length of thelongest chain, starting with ak and each dividing the following one, that

Page 161: Discretemaths Book

Miscellaneous Problems with Integers 97

can be selected from ak, ak+1, . . . , amn+1. If no nk is greater than n, thenthe are at least m + 1 nk’s that are the same. However, the integers ak

corresponding to these nk’s cannot divide each other, because ak|al

implies that nk ≥ nl + 1.

474 Theorem If k|n then fk|fn.

Proof Letting s = kn, t = n in the identity fs+t = fs−1ft + fsft+1 we obtain

f(k+1)n = fkn+n = fn−1fkn + fnfkn+1.

It is clear that if fn|fkn then fn|f(k+1)n. Since fn|fn·1, the assertion follows.

475 Example Prove that if p is an odd prime and ifa

b= 1 + 1/2 + · · · + 1/(p − 1),

then p divides a.

Solution: Arrange the sum as

1 +1

p − 1+

1

2+

1

p − 2+ · · · + 1

(p − 1)/2+

1

(p + 1)/2.

After summing consecutive pairs, the numerator of the resulting fractionsis p. Each term in the denominator is < p. Since p is a prime, the p onthe numerator will not be thus cancelled out.

476 Example The sum of some positive integers is 1996. What is theirmaximum product?

Solution: We are given some positive integers a1, a2, . . . , an witha1 + a2 + · · · + an = 1996. To maximise a1a2 · · ·an, none of the ak’s canbe 1. Let us show that to maximise this product, we make as manypossible ak = 3 and at most two aj = 2.

Suppose that aj > 4. Substituting aj by the two terms aj − 3 and 3 thesum is not changed, but the product increases since aj < 3(aj − 3). Thusthe ak’s must equal 2, 3 or 4. But 2 + 2 + 2 = 3 + 3 and 2 × 2 × 2 < 3 × 3,

thus if there are more than two 2’s we may substitute them by 3’s. As1996 = 3(665) + 1 = 3(664) + 4, the maximum product sought is 3664 × 4.

Page 162: Discretemaths Book

98 Chapter 4

477 Example Find all the positive integers of the form

r +1

r,

where r is a rational number.

Solution: We will show that the expression r + 1/r is a positive integeronly if r = 1, in which case r + 1/r = 2. Let

r +1

r= k,

k a positive integer. Then

r =k ±

√k2 − 4

2.

Since k is an integer, r will be an integer if and only k2 − 4 is a square ofthe same parity as k. Now, if k ≥ 3,

(k − 1)2 < k2 − 4 < k2,

that is, k2 − 4 is strictly between two consecutive squares and so it cannotbe itself a square. If k = 1,

√k2 − 4 is not a real number. If k = 2,

k2 − 4 = 0. Therefore, r + 1/r = 2, that is, r = 1. This finishes the proof.

478 Example For how many integers n in {1, 2, 3, . . . , 100} is the tens digitof n2 odd?

Solution: In the subset {1, 2, . . . 10} there are only two values of n (4 and6) for which the digits of the tens of n2 is odd. Now, the tens digit of(n + 10)2 = n2 + 20n + 100 has the same parity as the tens digit of n2.Thus there are only 20 n for which the prescribed condition is verified.

Problems

479 Problem Find the sum

5 + 55 + 555 + · · · + 5 . . . 5︸ ︷︷ ︸n 5 ′s

.

Page 163: Discretemaths Book

Miscellaneous Problems with Integers 99

480 Problem Show that for all numbers a 6= 0, a 6= ±i√

3 the followingformula of Reyley (1825) holds.

a =

(

a6 + 45a5 − 81a2 + 27

6a(a2 + 3)2

)3

+

(

−a2 + 30a2 − 9

6a(a2 + 3)

)3

+

(

−6a3 + 18a

(a2 + 3)2

)3

.

If a is rational this shows that every rational number is expressable as thesum of the cubes of three rational numbers.

481 Problem What is the largest power of 7 that divides 1000!?

482 Problem Demonstrate that for all integer values n,

n9 − 6n7 + 9n5 − 4n3

is divisible by 8640.

483 Problem Prove that if n > 4 is composite, then n divides (n − 1)!.(Hint: Consider, separately, the cases when n is and is not a perfectsquare.)

484 Problem Find all real numbers satisfying the equation

bx2 − x − 2c = bxc.

485 Problem Solve the equation

b x

1999c = b x

2000c

486 Problem (PUTNAM 1948) Let n be a positive integer. Prove that

b√

n +√

n + 1c = b√

4n + 2c

(Hint: Prove that√

4n + 1 <√

n +√

n + 1 <√

4n + 3. Argue that neither4n + 2 nor 4n + 3 are perfect squares.)

487 Problem Prove that 6|n3 − n, for all integers n.

Page 164: Discretemaths Book

100 Chapter 4

488 Problem (POLISH MATHEMATICAL OLYMPIAD) Prove that if n is aneven natural number, then the number 13n + 6 is divisible by 7.

489 Problem Find, with proof, the unique square which is the product offour consecutive odd numbers.

490 Problem (PUTNAM 1989) How many primes amongst the positiveintegers, written as usual in base-ten are such that their digits arealternating 1’s and 0’s, beginning and ending in 1?

491 Problem Let a, b, c be the lengths of the sides of a triangle. Show that

3(ab + bc + ca) ≤ (a + b + c)2 ≤ 4(ab + bc + ca).

492 Problem Let k ≥ 2 be an integer. Show that if n is a positive integer,then nk can be represented as the sum of n successive odd numbers.

493 Problem (IMO 1979) If a, b are natural numbers such that

a

b= 1 −

1

2+

1

3−

1

4+ · · · − 1

1318+

1

1319,

prove that 1979|a.

494 Problem (POLISH MATHEMATICAL OLYMPIAD) A triangular number isone of the form 1 + 2 + . . . + n,n ∈ N. Prove that none of the digits2, 4, 7, 9 can be the last digit of a triangular number.

495 Problem Demonstrate that there are infinitely many square triangularnumbers.

496 Problem (PUTNAM 1975) Supposing that an integer n is the sum oftwo triangular numbers,

n =a2 + a

2+

b2 + b

2,

write 4n + 1 as the sum of two squares, 4n + 1 = x2 + y2 where x and y

are expressed in terms of a and b.

Page 165: Discretemaths Book

Miscellaneous Problems with Integers 101

Conversely, show that if 4n + 1 = x2 + y2, then n is the sum of twotriangular numbers.

497 Problem (POLISH MATHEMATICAL OLYMPIAD) Prove thatamongst ten successive natural numbers, there are always at least oneand at most four numbers that are not divisible by any of the numbers2, 3, 5, 7.

498 Problem Are there five consecutive positive integers such that thesum of the first four, each raised to the fourth power, equals the fifthraised to the fourth power?

499 Problem Prove that(2m)!(3n)!

(m!)2(n!)3

is always an integer.

500 Problem Prove that for n ∈ N, (n!)! is divisible by n!(n−1)!

501 Problem (OLIMPIADA MATEMATICA ESPANOLA, 1985)If n is a positive integer, prove that (n+ 1)(n+ 2) · · · (2n) is divisible by 2n.

Page 166: Discretemaths Book

102 Chapter 4

Page 167: Discretemaths Book

Chapter 5Sums, Products, and Recursions

5.1 Telescopic cancellation

We could sum the series

a1 + a2 + a3 + · · · + an

if we were able to find {vk} satisfying ak = vk − vk−1. For

a1+a2+a3+· · ·+an = v1−v0+v2−v1+· · ·+vn−1−vn−2+vn−vn−1 = vn−v0.

If such sequence vn exists, we say that a1 + a2 + · · · + an is a telescopicseries.

502 Example Simplify(

1 +1

2

)

·(

1 +1

3

)

·(

1 +1

4

)

· · ·(

1 +1

99

)

.

Solution: Adding each fraction:

3

2· 4

3· 5

4· · · 100

99,

which simplifies to 100/2 = 50.

103

Page 168: Discretemaths Book
Page 169: Discretemaths Book

Telescopic cancellation 105

505 Example Show that

1

2· 3

4· 5

6· · · 9999

10000<

1

100.

Solution: LetA =

1

2· 3

4· 5

6· · · 9999

10000

andB =

2

3· 4

5· 6

7· · · 10000

10001.

Clearly, x2 − 1 < x2 for all real numbers x. This implies that

x − 1

x<

x

x + 1

whenever these four quantities are positive. Hence

1/2 < 2/3

3/4 < 4/5

5/6 < 6/7...

......

9999/10000 < 10000/10001

As all the numbers involved are positive, we multiply both columns toobtain

1

2· 3

4· 5

6· · · 9999

10000<

2

3· 4

5· 6

7· · · 10000

10001,

or A < B. This yields A2 = A · A < A · B. Now

A · B =1

2· 2

3· 3

4· 4

5· 5

6· 6

7· 7

8· · · 9999

10000· 10000

10001=

1

10001,

and consequently, A2 < A · B = 1/10001. We deduce thatA < 1/

√10001 < 1/100.

For the next example we recall that n! (n factorial) means

n! = 1 · 2 · 3 · · ·n.

For example, 1! = 1, 2! = 1 · 2 = 2, 3! = 1 · 2 · 3 = 6, 4! = 1 · 2 · 3 · 4 = 24.

Observe that (k + 1)! = (k + 1)k!. We make the convention 0! = 1.

Page 170: Discretemaths Book
Page 171: Discretemaths Book

Arithmetic Sums 107

5.2 Arithmetic Sums

An arithmetic progression is one of the form

a, a + d, a + 2d, a + 3d, . . . , a + (n − 1)d, . . .

One important arithmetic sum is

1 + 2 + · · · + n =n(n + 1)

2.

To obtain a closed form, we utilise Gauss’ trick:If

An = 1 + 2 + 3 + · · · + n

thenAn = n + (n − 1) + · · · + 1.

Adding these two quantities,

An = 1 + 2 + · · · + n

An = n + (n − 1) + · · · + 1

2An = (n + 1) + (n + 1) + · · · + (n + 1)

= n(n + 1),

since there are n summands. This gives An =n(n + 1)

2.

We summarise this as

1 + 2 + · · · + n =n(n + 1)

2.(5.1)

For example,

1 + 2 + 3 + · · · + 100 =100(101)

2= 5050.

Applying Gauss’s trick to the general arithmetic sum

(a) + (a + d) + (a + 2d) + · · · + (a + (n − 1)d)

we obtain

(a) + (a + d) + (a + 2d) + · · · + (a + (n − 1)d) =n(2a + (n − 1)d)

2

(5.2)

Page 172: Discretemaths Book

108 Chapter 5

511 Example Find the sum of all the integers from 1 to 1000 inclusive,which are not multiples of 3 or 5.

Solution: One computes the sum of all integers from 1 to 1000 and weedsout the sum of the multiples of 3 and the sum of the multiples of 5, butputs back the multiples of 15, which one has counted twice. The desiredsum is

(1 + 2 + 3 + · · · + 1000) − (3 + 6 + 9 + · · · + 999)

−(5 + 10 + 15 + · · · + 1000)

+(15 + 30 + 45 + · · · + 990)

= (1 + 2 + 3 + · · · + 1000) − 3(1 + 2 + 3 + · · · + 333)

−5(1 + 2 + 3 + · · · + 200)

+15(1 + 2 + 3 + · · · + 66)

= 500500 − 3 · 55611 − 5 · 20100 + 15 · 2211= 266332

512 Example Each element of the set {10, 11, 12, . . . , 19, 20} is multipliedby each element of the set {21, 22, 23, . . . , 29, 30}. If all these products areadded, what is the resulting sum?

Solution: This is asking for the product(10+ 11+ · · ·+ 20)(21+ 22+ · · ·+ 30) after all the terms are multiplied. But

10 + 11 + · · · + 20 =(20 + 10)(11)

2= 165

and

21 + 22 + · · · + 30 =(30 + 21)(10)

2= 255.

The required total is (165)(255) = 42075.

513 Example The sum of a certain number of consecutive positiveintegers is 1000. Find these integers.

Solution: Let the the sum of integers be S = (l + 1) + (l + 2) + (l + n).

Using Gauss’ trick we obtain S =n(2l + n + 1)

2. As S = 1000,

Page 173: Discretemaths Book

Arithmetic Sums 109

2000 = n(2l + n + 1). Now 2000 = n2 + 2ln + n > n2, whencen ≤ b

√2000c = 44. Moreover, n and 2l + n + 1 divisors of 2000 and are of

opposite parity. Since 2000 = 2453, the odd factors of 2000 are 1, 5, 25,and 125. We then see that the problem has te following solutions:

n = 1, l = 999,

n = 5, l = 197,

n = 16, l = 54,

n = 25, l = 27.

514 Example Find the sum of all integers between 1 and 100 that leaveremainder 2 upon division by 6.

Solution: We want the sum of the integers of the form6r + 2, r = 0, 1, . . . , 16. But this is

16∑

r=0

(6r + 2) = 6

16∑

r=0

r +

16∑

r=0

2 = 616(17)

2+ 2(17) = 850.

Ad Pleniorem Scientiam

515 Problem Show that

1 + 2 + 3 + · · · + (n2 − 1) + n2 =n2(n2 + 1)

2.

516 Problem Show that

1 + 3 + 5 + · · · + 2n − 1 = n2.

517 Problem (AHSME 1994) Sum the series

20 + 201

5+ 20

2

5+ · · · + 40.

Answer: 3030

Page 174: Discretemaths Book

110 Chapter 5

518 Problem Show that

1

1996+

2

1996+

3

1996+ · · · + 1995

1996

is an integer.

519 Problem (AHSME 1991) Let Tn = 1 + 2 + 3 + · · · + n and

Pn =T2

T2 − 1· T3

T3 − 1· T4

T4 − 1· · · Tn

Tn − 1.

Find P1991.

Answer: 59731993

520 Problem Given that

1

a + b,

1

b + c,

1

c + a

are consecutive terms in an arithmetic progression, prove that

b2, a2, c2

are also consecutive terms in an arithmetic progression.

521 Problem Consider the following table:

1 = 1

2 + 3 + 4 = 1 + 8

5 + 6 + 7 + 8 + 9 = 8 + 27

10 + 11 + 12 + 13 + 14 + 15 + 16 = 27 + 64

Conjecture the law of formation and prove your answer.

522 Problem The odd natural numbers are arranged as follows:

(1)

Page 175: Discretemaths Book

Geometric Sums 111

(3, 5)

(7, 9, 11)

(13, 15, 17, 19)

(21, 23, 25, 27, 29)

...............................

Find the sum of the nth row.

523 Problem Sum

10002 − 9992 + 9982 − 9972 + · · · + 44 − 32 + 22 − 11.

524 Problem The first term of an arithmetic progression is 14 and its 100thterm is −16. Find (i) its 30th term and (ii) the sum of all the terms from thefirst to the 100th.

5.3 Geometric Sums

A geometric progression is one of the form

a, ar, ar2, ar3, . . . , arn−1, . . . ,

525 Example Find the following geometric sum:

1 + 2 + 4 + · · · + 1024.

Solution: LetS = 1 + 2 + 4 + · · · + 1024.

Then2S = 2 + 4 + 8 + · · · + 1024 + 2048.

Hence

S = 2S−S = (2+4+8 · · ·+2048)−(1+2+4+ · · ·+1024) = 2048−1 = 2047.

Page 176: Discretemaths Book
Page 177: Discretemaths Book

Geometric Sums 113

Solution: We have

Sn−1

2Sn = (1+1/2+1/4+· · ·+1/2n)−(1/2+1/4+· · ·+1/2n+1/2n+1) = 1−1/2n.

WhenceSn = 2 − 1/2n.

So as n varies, we have:

S1 = 2 − 1/20 = 1

S2 = 2 − 1/2 = 1.5

S3 = 2 − 1/22 = 1.875

S4 = 2 − 1/23 = 1.875

S5 = 2 − 1/24 = 1.9375

S6 = 2 − 1/25 = 1.96875

S10 = 2 − 1/29 = 1.998046875

Thus the farther we go in the series, the closer we get to 2.Let us sum now the geometric series

S = a + ar + ar2 + · · · + arn−1.

Plainly, if r = 1 then S = na, so we may assume that r 6= 1. We have

rS = ar + ar2 + · · · + arn.

Hence

S − rS = a + ar + ar2 + · · · + arn−1 − ar − ar2 − · · · − arn = a − arn.

From this we deduce thatS =

a − arn

1 − r,

that is,

a + ar + · · · + arn−1 =a − arn

1 − r(5.3)

If |r| < 1 then rn → 0 as n → ∞.For |r| < 1, we obtain the sum of the infinite geometric series

a + ar + ar2 + · · · =a

1 − r(5.4)

Page 178: Discretemaths Book

114 Chapter 5

529 Example A fly starts at the origin and goes 1 unit up, 1/2 unit right,1/4 unit down, 1/8 unit left, 1/16 unit up, etc., ad infinitum. In whatcoordinates does it end up?

Solution: Its x coordinate is

1

2−

1

8+

1

32− · · · =

12

1 − −14

=2

5.

Its y coordinate is

1 −1

4+

1

16− · · · =

1

1 − −14

=4

5.

Therefore, the fly ends up in (25, 4

5).

Ad Pleniorem Scientiam

530 Problem The 6th term of a geometric progression is 20 and the 10this 320. Find (i) its 15th term, (ii) the sum of its first 30 terms.

5.4 Fundamental Sums

In this section we compute several sums using telescoping cancellation.We start with the sum of the first n positive integers, which we havealready computed using Gauss’ trick.

531 Example Find a closed formula for

An = 1 + 2 + · · · + n.

Solution: Observe that

k2 − (k − 1)2 = 2k − 1.

From this12 − 02 = 2 · 1 − 1

22 − 12 = 2 · 2 − 1

32 − 22 = 2 · 3 − 1...

......

n2 − (n − 1)2 = 2 · n − 1

Page 179: Discretemaths Book

Fundamental Sums 115

Adding both columns,

n2 − 02 = 2(1 + 2 + 3 + · · · + n) − n.

Solving for the sum,

1 + 2 + 3 + · · · + n = ·n2/2 + n/2 =n(n + 1)

2.

532 Example Find the sum

12 + 22 + 32 + · · · + n2.

Solution: Observe that

k3 − (k − 1)3 = 3k2 − 3k + 1.

Hence13 − 03 = 3 · 12 − 3 · 1 + 1

23 − 13 = 3 · 22 − 3 · 2 + 1

33 − 23 = 3 · 32 − 3 · 3 + 1...

......

n3 − (n − 1)3 = 3 · n2 − 3 · n + 1

Adding both columns,

n3 − 03 = 3(12 + 22 + 32 + · · · + n2) − 3(1 + 2 + 3 + · · · + n) + n.

From the preceding example 1 + 2 + 3 + · · ·+ n = ·n2/2 + n/2 =n(n+1)

2so

n3 − 03 = 3(12 + 22 + 32 + · · · + n2) −3

2· n(n + 1) + n.

Solving for the sum,

12 + 22 + 32 + · · · + n2 =n3

3+

1

2· n(n + 1) −

n

3.

After simplifying we obtain

12 + 22 + 32 + · · · + n2 =n(n + 1)(2n + 1)

6(5.5)

Page 180: Discretemaths Book

116 Chapter 5

533 Example Add the series

1

1 · 2 +1

2 · 3 +1

3 · 4 + · · · + 1

99 · 100.

Solution: Observe that

1

k(k + 1)=

1

k−

1

k + 1.

Thus1

1·2= 1

1− 1

21

2·3= 1

2− 1

31

3·4= 1

3− 1

4...

......

199·100

= 199

− 1100

Adding both columns,

1

1 · 2 +1

2 · 3 +1

3 · 4 + · · · + 1

99 · 100 = 1 −1

100=

99

100.

534 Example Add

1

1 · 4 +1

4 · 7 +1

7 · 10 + · · · + 1

31 · 34.

Solution: Observe that

1

(3n + 1) · (3n + 4)=

1

3· 1

3n + 1−

1

3· 1

3n + 4.

Thus1

1·4= 1

3− 1

121

4·7= 1

12− 1

211

7·10= 1

21− 1

301

10·13= 1

30− 1

39...

......

134·37

= 1102

− 1111

Page 181: Discretemaths Book

Fundamental Sums 117

Summing both columns,

1

1 · 4 +1

4 · 7 +1

7 · 10 + · · · + 1

31 · 34 =1

3−

1

111=

12

37.

535 Example Sum

1

1 · 4 · 7 +1

4 · 7 · 10 +1

7 · 10 · 13 + · · · + 1

25 · 28 · 31.

Solution: Observe that

1

(3n + 1) · (3n + 4) · (3n + 7)=

1

6· 1

(3n + 1)(3n + 4)−

1

6· 1

(3n + 4)(3n + 7).

Therefore1

1·4·7= 1

6·1·4− 1

6·4·71

4·7·10= 1

6·4·7− 1

6·7·101

7·10·13= 1

6·7·10− 1

6·10·13...

......

125·28·31

= 16·25·28

− 16·28·31

Adding each column,

1

1 · 4 · 7+1

4 · 7 · 10+1

7 · 10 · 13+· · ·+ 1

25 · 28 · 31 =1

6 · 1 · 4−1

6 · 28 · 31 =9

217.

536 Example Find the sum

1 · 2 + 2 · 3 + 3 · 4 + · · · + 99 · 100.

Solution: Observe that

k(k + 1) =1

3(k)(k + 1)(k + 2) −

1

3(k − 1)(k)(k + 1).

Therefore

Page 182: Discretemaths Book
Page 183: Discretemaths Book
Page 184: Discretemaths Book
Page 185: Discretemaths Book

First Order Recursions 121

2. Test a solution of the form xn = Aan + g(n), where g is a polynomialof the same degree as f.

546 Example Let x0 = 7 and xn = 2xn−1, n ≥ 1. Find a closed form for xn.

Solution: Raising subscripts we have the characteristic equationxn = 2xn−1. Cancelling, x = 2. Thus we try a solution of the formxn = A2n, were A is a constant. But 7 = x0 = A20 and so A = 7. Thesolution is thus xn = 7(2)n.Aliter: We have

x0 = 7

x1 = 2x0

x2 = 2x1

x3 = 2x2

......

...xn = 2xn−1

Multiplying both columns,

x0x1 · · · xn = 7 · 2nx0x1x2 · · · xn−1.

Cancelling the common factors on both sides of the equality,

xn = 7 · 2n.

547 Example Let x0 = 7 and xn = 2xn−1 + 1, n ≥ 1. Find a closed form forxn.

Solution: By raising the subscripts in the homogeneous equation weobtain xn = 2xn−1 or x = 2. A solution to the homogeneous equation willbe of the form xn = A(2)n. Now f(n) = 1 is a polynomial of degree 0 (aconstant) and so we test a particular constant solution C. The generalsolution will have the form xn = A2n + B. Now, 7 = x0 = A20 + B = A + B.Also, x1 = 2x0 + 7 = 15 and so 15 = x1 = 2A + B. Solving thesimultaneous equations

A + B = 7,

2A + B = 15,

we find A = 8, B = −1. So the solution is xn = 8(2n) − 1 = 2n+3 − 1.

Page 186: Discretemaths Book

122 Chapter 5

Aliter: We have:x0 = 7

x1 = 2x0 + 1

x2 = 2x1 + 1

x3 = 2x2 + 1...

......

xn−1 = 2xn−2 + 1

xn = 2xn−1 + 1

Multiply the kth row by 2n−k. We obtain

2nx0 = 2n · 72n−1x1 = 2nx0 + 2n−1

2n−2x2 = 2n−1x1 + 2n−2

2n−3x3 = 2n−2x2 + 2n−3

......

...22xn−2 = 23xn−3 + 22

2xn−1 = 22xn−2 + 2

xn = 2xn−1 + 1

Adding both columns, cancelling, and adding the geometric sum,

xn = 7 · 2n + (1 + 2 + 22 + · · · + 2n−1) = 7 · 2n + 2n − 1 = 2n+3 − 1.

Aliter: Let un = xn + 1 = 2xn−1 + 2 = 2(xn−1 + 1) = 2un−1. We solve therecursion un = 2un−1 as we did on our first example:un = 2nu0 = 2n(x0 + 1) = 2n · 8 = 2n+3. Finally, xn = un − 1 = 2n+3 − 1.

548 Example Let x0 = 2, xn = 9xn−1 − 56n + 63. Find a closed form forthis recursion.

Solution: By raising the subscripts in the homogeneous equation weobtain the characteristic equation xn = 9xn−1 or x = 9. A solution to thehomogeneous equation will be of the form xn = A(9)n. Nowf(n) = −56n + 63 is a polynomial of degree 1 and so we test a particularsolution of the form Bn + C. The general solution will have the formxn = A9n + Bn + C. Nowx0 = 2, x1 = 9(2) − 56 + 63 = 25, x2 = 9(25) − 56(2) + 63 = 176. We thussolve the system

2 = A + C,

Page 187: Discretemaths Book

First Order Recursions 123

25 = 9A + B + C,

176 = 81A + 2B + C.

We find A = 2, B = 7, C = 0. The general solution is xn = 2(9n) + 7n.

549 Example Let x0 = 1, xn = 3xn−1 − 2n2 + 6n − 3. Find a closed form forthis recursion.

Solution: By raising the subscripts in the homogeneous equation weobtain the characteristic equation xn = 3xn−1 or x = 9. A solution to thehomogeneous equation will be of the form xn = A(3)n. Nowf(n) = −2n2 + 6n − 3 is a polynomial of degree 2 and so we test aparticular solution of the form Bn2 + Cn + D. The general solution willhave the form xn = A3n + Bn2 + Cn + D. Nowx0 = 1, x1 = 3(1) − 2 + 6 − 3 = 4, x2 = 3(4) − 2(2)2 + 6(2) − 3 = 13, x3 =

3(13) − 2(3)2 + 6(3) − 3 = 36. We thus solve the system

1 = A + D,

4 = 3A + B + C + D,

13 = 9A + 4B + 2C + D,

36 = 27A + 9B + 3C + D.

We find A = B = 1, C = D = 0. The general solution is xn = 3n + n2.

550 Example Find a closed form for xn = 2xn−1 + 3n−1, x0 = 2.

Solution: We test a solution of the form xn = A2n + B3n. Thenx0 = 2, x1 = 2(2) + 30 = 5. We solve the system

2 = A + B,

7 = 2A + 3B.

We find A = 1, B = 1. The general solution is xn = 2n + 3n.

We now tackle the case when a = 1. In this case, we simply consider apolynomial g of degree 1 higher than the degree of f.

551 Example Let x0 = 7 and xn = xn−1 + n,n ≥ 1. Find a closed formulafor xn.

Page 188: Discretemaths Book

124 Chapter 5

Solution: By raising the subscripts in the homogeneous equation weobtain the characteristic equation xn = xn−1 or x = 1. A solution to thehomogeneous equation will be of the form xn = A(1)n = A, a constant.Now f(n) = n is a polynomial of degree 1 and so we test a particularsolution of the form Bn2 + Cn + D, one more degree than that of f. Thegeneral solution will have the form xn = A + Bn2 + Cn + D. Since A andD are constants, we may combine them to obtain xn = Bn2 + Cn + E.

Now, x0 = 7, x1 = 7 + 1 = 8, x2 = 8 + 2 = 10. So we solve the system

7 = E,

8 = B + C + E,

10 = 4B + 2C + E.

We find B = C =1

2, E = 7. The general solution is xn =

n2

2+

n

2+ 7.

Aliter: We havex0 = 7

x1 = x0 + 1

x2 = x1 + 2

x3 = x2 + 3...

......

xn = xn−1 + n

Adding both columns,

x0 + x1 + x2 + · · · + xn = 7 + x0 + x2 + · · · + xn−1 + (1 + 2 + 3 + · · · + n).

Cancelling and using the fact that 1 + 2 + · · · + n =n(n + 1)

2,

xn = 7 +n(n + 1)

2.

Some non-linear first order recursions maybe reduced to a linear firstorder recursion by a suitable transformation.

552 Example A recursion satisfies u0 = 3, u2n+1 = un, n ≥ 1. Find a closed

form for this recursion.

Page 189: Discretemaths Book
Page 190: Discretemaths Book

126 Chapter 5

560 Problem If u0 = 1/3 and un+1 = 2u2n − 1, find a closed form for un.

Hint: Let un = cos vn.

561 Problem Let x1 = 1, xn+1 = x2n − xn + 1, n > 0. Show that

∞∑

n=1

1

xn

= 1.

5.6 Second Order Recursions

All the recursions that we have so far examined are first order recursions,that is, we find the next term of the sequence given the preceding one.Let us now briefly examine how to solve some second order recursions.We now outline a method for solving second order homogeneous linearrecurrence relations of the form

xn = axn−1 + bxn−2.

1. Find the characteristic equation by “raising the subscripts” in theform xn = axn−1 + bxn−2. Cancelling this gives x2 − ax − b = 0. Thisequation has two roots r1 and r2.

2. If the roots are different, the solution will be of the formxn = A(r1)

n + B(r2)n, where A,B are constants.

3. If the roots are identical, the solution will be of the formxn = A(r1)

n + Bn(r1)n.

562 Example Let x0 = 1, x1 = −1, xn+2 + 5xn+1 + 6xn = 0.

Solution: The characteristic equation is x2 + 5x + 6 = (x + 3)(x + 2) = 0.Thus we test a solution of the form xn = A(−2)n + B(−3)n. Since1 = x0 = A + B,−1 = −2A − 3B, we quickly find A = 2, B = −1. Thus thesolution is xn = 2(−2)n − (−3)n.

563 Example Find a closed form for the Fibonacci recursionf0 = 0, f1 = 1, fn = fn−1 + fn−2.

Page 191: Discretemaths Book

Second Order Recursions 127

Solution: The characteristic equation is f2 − f − 1 = 0, whence a solution

will have the form fn = A

(

1 +√

5

2

)n

+ B

(

1 −√

5

2

)n

. The initial

conditions give0 = A + B,

1 = A

(

1 +√

5

2

)

+ B

(

1 −√

5

2

)

=1

2(A + B) +

√5

2(A − B) =

√5

2(A − B)

This gives A =1√5, B = −

1√5

. We thus have the Cauchy-Binet Formula:

fn =1√5

(

1 +√

5

2

)n

−1√5

(

1 −√

5

2

)n

(5.7)

564 Example Solve the recursion x0 = 1, x1 = 4, xn = 4xn−1 − 4xn−2 = 0.

Solution: The characteristic equation is x2 − 4x + 4 = (x − 2)2 = 0. Thereis a multiple root and so we must test a solution of the formxn = A2n + Bn2n. The initial conditions give

1 = A,

4 = 2A + 2B.

This solves to A = 1, B = 1. The solution is thus xn = 2n + n2n.

Ad Pleniorem Scientiam

565 Problem Solve the recursion x0 = 0, x1 = 1, xn = 10xn−1 − 21xn−2.

566 Problem Solve the recursion x0 = 0, x1 = 1, xn = 10xn−1 − 25xn−2.

567 Problem Solve the recursion x0 = 0, x1 = 1, xn = 10xn−1 − 21xn−2 + n.

568 Problem Solve the recursionx0 = 0, x1 = 1, xn = 10xn−1 − 21xn−2 + 2n.

Page 192: Discretemaths Book

128 Chapter 5

5.7 Applications of Recursions

569 Example Find the recurrence relation for the number of n digit binarysequences with no pair of consecutive 1’s.

Solution: It is quite easy to see that a1 = 2, a2 = 3. To form an, n ≥ 3, wecondition on the last digit. If it is 0, the number of sequences sought isan−1. If it is 1, the penultimate digit must be 0, and the number ofsequences sought is an−2. Thus

an = an−1 + an−2, a1 = 2, a2 = 3.

570 Example Let there be drawn n ovals on the plane. If an ovalintersects each of the other ovals at exactly two points and no three ovalsintersect at the same point, find a recurrence relation for the number ofregions into which the plane is divided.

Solution: Let this number be an. Plainly a1 = 2. After the n − 1th stage,the nth oval intersects the previous ovals at 2(n − 1) points, i.e. the nthoval is divided into 2(n − 1) arcs. This adds 2(n − 1) regions to the an−1

previously existing. Thus

an = an−1 + 2(n − 1), a1 = 2.

571 Example Find a recurrence relation for the number of regions intowhich the plane is divided by n straight lines if every pair of linesintersect, but no three lines intersect.

Solution: Let an be this number. Clearly a1 = 2. The nth line is cut by heprevious n − 1 lines at n − 1 points, adding n new regions to thepreviously existing an−1. Hence

an = an−1 + n, a1 = 2.

Page 193: Discretemaths Book

Applications of Recursions 129

572 Example (Derangements) An absent-minded secretary is filling n

envelopes with n letters. Find a recursion for the number Dn of ways inwhich she never stuffs the right letter into the right envelope.

Solution: Number the envelopes 1, 2, 3, · · · , n. We condition on the lastenvelope. Two events might happen. Either n and r(1 ≤ r ≤ n − 1) tradeplaces or they do not.

In the first case, the two letters r and n are misplaced. Our task is just tomisplace the other n − 2 letters, (1, 2, · · · , r − 1, r + 1, · · · , n − 1) in theslots (1, 2, · · · , r − 1, r + 1, · · · , n − 1). This can be done in Dn−2 ways.Since r can be chosen in n − 1 ways, the first case can happen in(n − 1)Dn−2 ways.In the second case, let us say that letter r, (1 ≤ r ≤ n − 1) moves to then-th position but n moves not to the r-th position. Since r has beenmisplaced, we can just ignore it. Since n is not going to the r-th position,we may relabel n as r. We now have n − 1 numbers to misplace, and thiscan be done in Dn−1 ways.As r can be chosen in n − 1 ways, the total number of ways for thesecond case is (n − 1)Dn−1. Thus Dn = (n − 1)Dn−2 + (n − 1)Dn−1.

573 Example There are two urns, one is full of water and the other isempty. On the first stage, half of the contains of urn I is passed into urn II.On the second stage 1/3 of the contains of urn II is passed into urn I. Onstage three, 1/4 of the contains of urn I is passed into urn II. On stagefour 1/5 of the contains of urn II is passed into urn I, and so on. Whatfraction of water remains in urn I after the 1978th stage?

Solution: Let xn, yn, n = 0, 1, 2, . . . denote the fraction of water in urns Iand II respectively at stage n. Observe that xn + yn = 1 and that

Page 194: Discretemaths Book

130 Chapter 5

x0 = 1;y0 = 0

x1 = x0 − 12x0 = 1

2;y1 = y1 + 1

2x0 = 1

2

x2 = x1 + 13y1 = 2

3;y2 = y1 − 1

3y1 = 1

3

x3 = x2 − 14x2 = 1

2;y1 = y1 + 1

4x2 = 1

2

x4 = x3 + 15y3 = 3

5;y1 = y1 − 1

5y3 = 2

5

x5 = x4 − 16x4 = 1

2;y1 = y1 + 1

6x4 = 1

2

x6 = x5 + 17y5 = 4

7;y1 = y1 − 1

7y5 = 3

7

x7 = x6 − 18x6 = 1

2;y1 = y1 + 1

8x6 = 1

2

x8 = x7 + 19y7 = 5

9;y1 = y1 − 1

9y7 = 4

9

A pattern emerges (which may be proved by induction) that at each oddstage n we have xn = yn = 1

2and that at each even stage we have (if

n = 2k) x2k = k+12k+1

, y2k = k2k+1

. Since 1978 ÷ 2 = 989 we have x1978 = 9901979

.

Ad Pleniorem Scientiam

574 Problem At the Golem Gambling Casino Research Institute anexperiment is performed by rolling a die until two odd numbers haveappeared (and then the experiment stops). The tireless researcherswanted to find a recurrence relation for the number of ways to do this.Help them!

Answer: a0 = 0, an = an−1 + (n − 1)3n.

575 Problem Mrs. Rosenberg has $8 000 000 in one of her five savingsaccounts. In this one, she earns 15% interest per year. Find a recurrencerelation for the amount of money after n years.

Page 195: Discretemaths Book

Applications of Recursions 131

576 Problem Find a recurrence relation for the number of ternary n-digitsequences with no consecutive 2’s.