You are on page 1of 78

MATH1179

Mathematics for
Computer Science
Part 1

Dr Yvonne Fryer
September 2021
MATH1179

Mathematics for Computer Science


This handbook contains the basic material you will need for the first half of the module. Worked
examples and further explanations will be given in the lectures and tutorials so it is very important
that you attend lectures and tutorials and work through these notes. Only the most basic information
about each topic is contained in this handbook. Accompanying these notes will also be a workbook
with corresponding questions on a particular topic that are to be completed during the tutorials. Bring
the workbook to the tutorials.

What is this course about?


We will look at some of the fundamental ideas of mathematics that are useful and applied in aa
computing setting. You will gain far more from this course if you engage fully, work hard, contribute,
and have fun!

Course topics
A variety of topics will be introduced. Some may be familiar to you, others possibly less so. You will
be encouraged to read around and find out more about the topics.
Topics will include sets, number theory, linear algebra, calculus, graph theory… and more!

How will the course run?


There is a two-hour lecture, that includes recorded and live sessions, plus there is a tutorial session
each week for the module.

Tutorials
The tutorials allow you the opportunity to really make use of the tutors help and guidance. The tutors
will be monitoring your completion of the tutorial exercises in the workbook.
There will be tutorial exercises accompanying every week of lectures - you must try to complete these
exercises - the most important reason is to develop your understanding. If you cannot do an exercise
then ask for help!
Working together on tutorial exercises is encouraged and strongly recommended – you can learn from
each other!
You should work through a selection of exercises and ask for help on ones you don’t understand how
to do.
Solutions will appear on Moodle a little while after the tutorial, after you have had time to try all the
exercises for yourself.

Attendance
It is expected that you attend all lectures and tutorials. Records of attendance are kept. There is always
a very strong correlation between attendance and marks.

Mathematics for Computer Science i


MATH1179
If you cannot make a class, make sure you notify your absence online, let your personal tutor know
there is a long term problem.
Assessment
This course is assessed by two tests each worth 50% of the marks available for the module. There
will be formative assessment prior to the summative tests. This formative assessment will be used as
a practice for you. More information will be given on this in the lectures.

Books
We do not “lecture from a book” and are therefore not expecting you to go out and buy a book.
There are some books and sites listed that may be useful for you, but it is not compulsory to buy
anything.
If you find any really good books or sites, let me know, so that I can recommend them to other
students!
www.mathcentre.ac.uk has useful resources and practice material, often referred to on the course.

Finally …
Finally, we want you to enjoy this course! We want to get you thinking in different ways.
Put the work in and contribute to lectures, and you will learn more and hopefully enjoy it while you
are doing it!
And ask me anything you want to ask!

ii Term 1
MATH1179

Contents

Page No. Bouncing balls .................................. 31


Convergence and divergence ............ 32
1: Sets ............................................................ 1
Arithmetic and geometric progressions
1.1 Subsets ................................................ 2
- definitions and formulae ................ 32
1.2 Venn diagrams .................................... 3
1.3 Laws of Operations of Sets ................ 6 5: Logic........................................................ 33
Product Set .......................................... 7 5.1 Propositional Calculus ..................... 33
Propositions ...................................... 33
2: Numbers.................................................... 9
Compound Propositions ................... 34
2.1 Numeric systems ................................ 9
Truth values and tables ..................... 34
2.2 Number systems ................................. 9
Laws of Propositional Calculus ........ 36
Types of numbers in programming... 10
Implication ........................................ 36
Exponents and Scientific Notation ... 10
Valid and Invalid Arguments ........... 37
Naming conventions ......................... 11
Equivalent systems ........................... 40
Arithmetic and units ......................... 12
Propositions and Computer Logic .... 40
Decimal places, significant figures and
Electronic Gates................................ 40
estimates. .......................................... 12
5.2 Predicate Calculus ............................ 42
2.3 Number bases ................................... 12
Quantification ................................... 43
Binary, Octal and Hexadecimal ........ 14
Two-place predicates ........................ 44
Modular arithmetic ........................... 16
Applications to Programming........... 45
3: Algebra.................................................... 19
6: Relations, Mappings and Functions..... 46
4: Sequences and Series ............................. 24 6.1 Relations........................................... 46
4.1 Sequences ......................................... 24 6.2 Functions .......................................... 48
Golden ratio ...................................... 25
7: Complex Numbers ................................. 64
Sequence notation ............................. 25
4.2 Series ................................................ 25 8: Pattern Matching and Unification ....... 71
Arithmetic progressions ......................... 27 Pattern matching..................................... 71
Geometric progressions .......................... 28 Unification.............................................. 71
Infinite geometric progressions .............. 30 Differences between pattern matching
Zeno’s paradox ................................. 30 and unification .................................. 72
Cutting wood .................................... 31

Mathematics for Computer Science iii


MATH1179

iv Term 1
MATH1179

1: Sets
The first lines of a computer program often contain type declarations so that we know what sort of
storage we are going to need to fit our data.

Example
String name
Dim sum as Single
double x1,x2
If we try to use a different set of values to those we have declared we get a ‘type mismatch’ error
and our program will not run. In constructing and using databases we can also have sets of permitted
data as inputs or outputs. Names, addresses, dates and telephone numbers are often specified in a
certain style and trying to use the wrong format will cause an ‘inadmissible input’ error.

These are examples where a certain set or type is defined with clear rules so that it can be seen whether
a given value is in or out of the set.

As a formal structure a set is a collection of objects with some criteria of membership. The objects of
the set are called elements or members of the set.

Example
We could define D = the set of students on Computing courses
L = the set of letters in the alphabet
N = the set of natural numbers

To specify a set we must describe its members. This is usually done in one of two ways.
(i) Listing all the members of the set, e.g., L = {a, b, c, … y, z}.
(ii) Giving a property which distinguishes the members of the set from objects not in the set.
e.g., P = {x: x is a whole number, 4 ≤ x ≤ 7}
(We read this as: P is the set of elements x such that x is a whole number greater than or equal
to 4 and less than or equal to 7.)

Example
T = {x: 3x = 6}
K = {x: x is a whole number, 2 < x < 6}
H = the set of even numbers
F = the set of colours in the rainbow
E = {x: x is a letter in the alphabet, x is a vowel}
Note that sets are normally denoted by capital letters and the list of elements is enclosed in ‘curly
brackets’

Mathematics for Computer Science Page 1


MATH1179

A set is called finite if it contains only a limited number of elements. Otherwise it is called infinite.
If a set A is finite then the number of elements in A is called the order of A.
We denote the order of A by A.
In the last example T = 1, K = 3, H is infinite.

We will use the symbol ∈ to mean 'is an element of' or ' belongs to'
and ∉ to mean 'is not an element of' or 'does not belong to'.

Example
W = {1 ,2,4,5,8}. Then 4 ∈ W, 6 ∉ W.

Two sets A and B are equal if they contain exactly the same elements.

Example
If D = set of letters in the word 'members', E = {m, e, m, b, e, r, s} and
F = {b, e, m, r, s},
then D = E = F. Note sets do not normally contain duplicates.

1.1 Subsets
Let A and S be sets. If every element in A is also in S then A is called a subset of S.
We write: A ⊆ S (or S ⊇ A).

If every element in A is also in S, but there are elements of S which are NOT in A then A is a proper
subset of S.
We then write: A ⊂S (or S ⊃A).

Example
(i) B = {2}, C = {2, 4, 6}, D = {2, 3, 4, 5, 6}. Then B ⊂C, B ⊂D, C ⊂D, B ⊆ B, etc.

(ii) P = set of students on Computing programmes, S = set of students in the Faculty of


Architecture, Computing & Humanities. P ⊂S.
(iii) P is the set of positive whole numbers less than 5. E = {2, 4} F = {1,2,3,4} G = (1,2,3,4,5}
then E ⊂F⊆ P⊂ G

The empty set ∅ is the set containing no elements. For any set A, ∅ ⊆ A.

Example
D = the set of students taking a course in Computing, aged under 10; D = ∅

Page 2 Sets
MATH1179
For any problem we define a universal set U as the boundary of all things under consideration and
assume all sets to be subsets of U. For example if we are dealing with numbers the universal set
might be R ; if we were dealing with sets of students the universal set might be the set of all students
in the University of Greenwich.

Let A and B be sets with universal set U.


The complement of A is the set of elements in U which are not in A, written A or A'.
The intersection of A and B is the set of elements which are in A and also in B, written A ∩ B.
The union of A and B is the set of elements which are in A or in B or in both, written A ∪ B.
The difference of A and B is the set of elements which are in A but not in B, written A \ B,

Example
Let U = {a,b,c,d,e,f,g,h,i}, P = {b, d, f}, Q = {a, b, c, d}, S = {a, c, e}

P ∩ Q = { b,d } P ∪ Q = { a, b, c, d, f}
Q ∩ S = {a,c} Q ∪ S = {a,b,c,d,e}
P ∪ (Q ∩ S) = {a,b,c,d,f} (P ∪ Q) ∩ (P ∪ S) = {a,b,c,d,f}
P ∪ (Q ∪ S) = {a,b,c,d,e,f} (P ∪ Q) ∪ S = {a,b,c,d,e,f}
P' = {a,c,e,g,h,i} Q' = {e,f,g,h,i}
P' ∪ Q' = {a,c,e,f,g,h,i} (P ∩ Q)' = {a,c,e,f,g,h,i}
P \ Q = {f} Q \ P = {a,c}

Two sets A and B are disjoint if they have no elements in common.


In the example above, P and S are disjoint.

1.2 Venn diagrams


A useful way of illustrating sets is to use a Venn diagram.
The interior of the rectangle represents the universal set U. A set is represented by the inside of a
closed loop.

A∩B A∪B A or A'

Example
Let U, = {a,b,c,d,e,f,g,h,i}, P = {b, d, f}, Q = {a, b, c, d}, S = {a, c, e}.
On a Venn diagram we can see their relationship and where each element is placed:

Mathematics for Computer Science Page 3


MATH1179

Q
P S
b a
e
f d c i

h
g

Calculations with sets usually involve understanding when elements in an intersection may have been
counted more than once.

Example
Out of 100 people questioned, 54 read the Morning News and 67 the Evening News. If 40 read both,
how many read neither?

Note in the diagram below 40 represents |P ∩ Q| whereas in the previous diagram we show b and d
as elements of P ∩ Q.
U

|P|= 54 |Q| = 67

40

19

Let P be the set of people who read the Morning News and Q the set of people who read the Evening
News.

40 people are in P and Q.

This leaves 54 – 40 = 14 in P but not Q and 67 – 40 = 27 in Q but not P.

So 40 + 14 + 27 = 81 people are in P ∪ Q. The number who are not in P ∪ Q is 100 – 81 = 19.

Page 4 Sets
MATH1179
Formula for A ∪ B

A B
Let A = x, B = y, and A ∩ B = r.
From the Venn diagram, it can be seen that x-r r y-r

A ∪ B = (x - r) + (y - r) + r
= x + y -r.

A ∪ B = A + B - A ∩ B

Example
• 112 students sat a certain examination.
• Question 1 was attempted by 70 students, question 2 was attempted by 66 students
and question 3 was attempted by 38 students.
• 32 students attempted questions 1 and 2, and 20 attempted questions 1 and 3.
• Only 8 students attempted all three questions.
Assuming all students attempted at least one of these three questions find the number who attempted
only two questions.

Let Q1 be the set of students who attempted question 1, let Q2 be the set of students who attempted
question 2 and let Q3 be the set of students who attempted question 3.

When these are placed on a Venn diagram we use the most general form of three intersecting circles.

U
Q2
Q1

24
26 34-x

12 x

18-x
Q3

• 8 students answered questions 1, 2 and 3.


• 32 answered 1 and 2 but we have already accounted for 8 of these.
o This gives 32-8=24 to put on the diagram.
• Similarly we put 20-8=12 on the diagram.
• We do not know how many attempted 2 and 3, so mark this as x.
• Fill in the rest of the information.
• All students attempted at least one question so adding all the numbers gives 112 as a total for
all students.

Mathematics for Computer Science Page 5


MATH1179
o i.e. 26+24+12+8+(34-x)+x+(18-x) = 112

This gives 122-x = 112 and so


x = 10.

The number of students attempting two questions is 24+12+10 = 46.

1.3 Laws of Operations of Sets


In previous examples we saw that P ∪ (Q ∩ S) = (P ∪ Q) ∩ (P ∪ S) and P' ∪ Q' = (P ∩ Q)'
These hold for any sets P, Q and S and are two of the many laws of sets.

1 Commutative Law A∩B=B∩A A∪B=B∪A


2 Associative Law A ∩ (B ∩ C) = (A ∩ B) ∩ C A ∪ (B ∪ C) = (A ∪ B) ∪ C
3 Distributive Law A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
4 Identity Law A∩U=A A∪∅=A
5 Domination Law A∩∅=∅ A∪U=U
6 Idempotent Law A∩A=A A∪A=A
7 Complement Law A ∩ A'= ∅ A ∪ A'= U U'= ∅ ∅'=U
8 De Morgan's Law (A ∩ B)'= A'∪ B' (A ∪ B)'= A'∩ B'
9 Involution Law (A')' = A

We can demonstrate the truth of statements about sets by using Venn diagrams and/or prove it with
laws.

Consider A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C) We will show this by shading these 2 diagrams

A A B
B

C
C

A ∪ (B ∩ C) (A ∪ B) ∩ (A ∪ C)

Shading a UNION you collect ALL shaded areas together


Shading an INTERSECTION you want areas shaded in both ways where they OVERLAP

Page 6 Sets
MATH1179
Example
Show that S ∩ (S ∩ T')' = S ∩ T

S ∩ (S ∩ T')' = S ∩ (S' ∪ T'') De Morgan


= S ∩ (S' ∪ T) Involution
= (S ∩ S') ∪ (S ∩ T) Distributive
= ∅ ∪ (S ∩ T) Complement
=S∩T Domination
Alternatively, use a Venn diagram.

S T S T S T S T

S ∩ T' (S ∩ T')' S ∩ (S ∩ T')' S∩T


(S and not T) NOT(S and not T) overlap of S and NOT(S and not T)

Product Set
Let A and B be sets.
The Cartesian product set A × B is the set of all ordered pairs (a, b) where a ∈ A and b ∈ B.

Example
Let A = {1, 2}, B = {s, t, u}
A × B = {(1,s), (1,t), (1,u), (2,s), (2,t), (2,u)}

Mathematics for Computer Science Page 7


MATH1179
Example
Consider the following information about 5 people and the illnesses they suffered as children
Mary, Tom and Albie had measles;
Tom, John and Leroy had chicken pox;
John and Albie had mumps.

If C = {Mary, Tom, Albie, John, Leroy } and I = {measles, chicken pox, mumps}
then we can express the information as a subset of the cartesian product set C × I,
i.e. as only the following pairs from the 15 possible in the product set
Product Set
{(Mary, measles), (Tom, measles), (Albie, measles), (Tom, chicken-pox),
(John, chicken-pox), (Leroy,chicken pox), (John,mumps), (Albie,mumps)} Name Disease

Mary Measles
We can see the application of this idea in queries in databases.
Tom Measles
Illness Query
People Albie Measles
Name Disease
Disease John Measles
Name
Mary Measles
Disease
Mary Leroy Measles
Tom Measles
Measles
Tom Mary Chickenpox
Albie Measles
Chicken Pox
Albie Tom Chickenpox
Tom Chicken Pox
Mumps
John Albie Chickenpox
John Chicken Pox
Leroy John Chickenpox
Leroy Chicken Pox
Leroy Chickenpox
This structure can be extended to 3 or more sets or John Mumps
use the same set more than once Mary Mumps
Albie Mumps
Tom Mumps

Albie Mumps

John Mumps

Leroy Mumps

Example
If you are trying to describe the position of a 3D shape in space then its coordinates are from the set
RxRxR

e.g. a cube could be (0,0,0), (0,1,0), (0,0,1), (0,1,1), (1,0,0), (0,1,0), (1,0,1), (1,1,1),

Page 8 Sets
MATH1179

2: Numbers

2.1 Numeric systems


Different methods to represent and write numbers have been formulated over many years. For
example roman numerals originated from ancient Rome as a way of writing numbers and are still
used occasionally today. For example a television programme may have at the end of the credits
MMXVIII.
Symbol I V X L C D M
Value 1 5 10 50 100 500 1000
Using the conversion table would mean the programme was made in the year 2018.
For counting, roman numerals can be a bit clumsy and not the best approach. A simple counting
technique we sometimes use is that of tally marks. A single line is added when adding one to the tally.
Symbol | || ||| |||| ||||
Value 1 2 3 4 5
For example the number 46 would be seen as: |||| |||| |||| |||| |||| |||| |||| |||| |||| |
For our number representations and arithmetic we use the decimal system with digits 0 to 9.

2.2 Number systems


Since primitive man wanted to count objects we have used various forms of number systems. The
methods for representing a number vary too. These have got more complex as the need has arisen.
Each set is an extension of the previous one with some extra features added.

N Z Q R

the set of natural numbers N = {1, 2, 3,...} (note 0 is included in some books)
the set of integers Z = { ..., -2, -1, 0, 1, 2,...} extended to include negatives
the set of rational numbers Q = {p/q : p,q ∈ Ζ, q ≠ 0} extended to include fractions
the set of real numbers R extended to include √2 e.t.c.

In computing it is important when declaring numerical variables to know what type of number they
can be.

Mathematics for Computer Science Page 9


MATH1179
We need to be confident at manipulating all these types of numbers as the underlying rules become
significant when we deal with non- numerical expressions. At the end of the chapter are some practise
questions to remind you of the basics. If you have trouble with these please sort it out in your tutorial
time.

Remember for Addition and Subtraction you need to have ‘like with like’

Example
To add 2/5 and 3/8 you must change them both to a common denominator

2 3 16 15 31
+ = + =
5 8 40 40 40

Types of numbers in programming


In Java there are six different types of number used in declarations, which can be thought of in two
groups

byte
short float
‘integers’ int double ‘reals’

The type you use depends on the purpose of your program and the precision you require.

Exponents and Scientific Notation


It is common on calculators and in programming to find that 5 000 000 is written in a different format
e.g. 5 EXP 6 or 5 6 or 5 e +6 because
5 000 000 is 5 million and a million is 10×10×10×10×10×10, 10 to the power of 6.

In dealing with computer storage, data transfer speeds, bandwidth and many other variables we often
need to write very large and very small numbers so we make use of this shorter form using powers of
10.

Examples The exponent (or index) on top of the 10 is


2
123.7 = 1.237 x 10 determined by how much you have moved the
72.893 = 7.2893 x 101 decimal point.
0.000345 = 3.45 x 10-4

Remember: Addition and Subtraction need ‘like with like’

Page 10 Numbers
MATH1179
Example
2.3 x 102 + 4.1x103 = 4.33 x 103 (0.23 x 103 + 4.1x103 )

We can describe the range of some of the Java number types using this notation

NAME min (or smallest) max (or largest) storage


Byte -128 127 1 byte ( 8 bits)
Short -32768 32767 2 bytes
Int -2147483648 2147483647 4 bytes
Long -9223372036854775808 9223372036854775807 8 bytes
float ± 1.4 x10-45 ± 3.4 x 1038 4 bytes
Double ± 4.9 x 10-324 ± 1.8 x 10308 8 bytes

Remember: 1
10 − n =
10 n
Because using indices you are dealing with powers of 10, they allow some short cuts in the arithmetic.

Remember:
102 × 103 = (10×10) × (10×10×10) = 105 so we add the indices ( or exponents)
when multiplying

10 × 10 × 10 × 10 × 10 × 10
106 ÷ 102 = = 104 here we subtract the indices when
10 × 10
dividing

Naming conventions
We are used to a centimetre being one hundredth of a metre. In general there are several words which
indicate a quantity has been multiplied or divided by a power of 10.

Smallest largest
×10-12 ×10-9 ×10-6 ×10-3 ×10-2 ×10-1 1=100 ×101 ×102 ×103 ×106 ×109 ×1012
Pico nano micro milli centi deci deca hecto kilo mega giga terra
one thousandth of a

a million millionth
One millionth of a

one thousandth

one hundredth
one millionth
thousandth

a thousand

a thousand
a hundred

a million
one tenth
millionth

thousand
Original

ten

Mathematics for Computer Science Page 11


MATH1179
Arithmetic and units
Care must be taken when using units of measurement in calculations.

Example
A snail moves at a rate of 5 millimetres a second. How many hours will it take the snail to finish the
Olympic 100 metres race? (answers to 2dp)

100m 1x10 2 m
= = 0.2 × 105 seconds = 2.0 × 104 = seconds.
5mm / sec 5x10 −3 m / sec
2.0×104
There are 360 seconds in an hour, therefore in hours the time is = 0.5555 × 102 = 55.56
3.6×102
hours or over 2 days!

Note the possibilities for errors.


1. not expressing 100 and 5 both in the same units – metres
2. dividing by 10-3 becomes multiplying by 103
3. metres divided by metres per second becomes seconds
4. if you have a given degree of accuracy for the answer – carry more places
the answer is a recurring decimal and must be truncated carefully.

Decimal places, significant figures and estimates.


Often when programming expressions into a calculator, a spreadsheet or a computer it is useful to
have an estimate of the likely output. We often do this by approximating the actual number using
fewer decimal places or significant figures.

e.g. 3.2478 = 3 or 485 = 500 to 1 significant figure


so 485 × 3.2478 ≈ 500 × 3 = 15 000

2.3 Number bases


We have looked at the types of number we can use in programming now we will look at different
internal representations of number used in computer arithmetic.

In computing it is common to have to work in binary (base 2) arithmetic or even in hexadecimal (base
16). To be confident with these you need to go back to the basics of the decimal system (base 10) we
normally use and recognise some of the other bases in common use.

You should understand the value of the 4 in each of the following numbers is different:
number 2431 3.04 543.29 9.0045 4
value of 4 4 hundred 4 hundredths forty or 4 tens four thousandths 4 or 4 units

Page 12 Numbers
MATH1179
Understand the importance of place values and ‘carrying’ and ‘borrowing’ in addition and subtraction
when a whole 10 is made from 10 in one column.

14 31 4 234 2 31 4 3204 2 . 3 47 82 . 32
8+ 8- 718+ 118- 1348 - 0 . 015+ 9 . 70 -
22 26 952 116 1856 2 . 362 76 . 62
1 1

Th H T U The column headings in the decimal system reflect the powers of 10


so the two 3’s represent different values. One is 3 the other is 3000
1000’s 100’s 10’s 1’s
3 7 4 3
We can translate these ideas into any number base

Example
A teams’ total times for the relay are obtained by adding up each 48.6
individual leg given in seconds.
54.3
52.7

However the total is given as 3 hours 24.9 minutes as we gather 49.3


up the complete 60’s into minutes 204.9 sec

Example
A boy of 5ft 11 inches grows 3 inches in a year making him 5 ft 14 inches????
No, we collect whole 12’s to make feet so 11 inches is the most we can count and his new height as
6ft 2 inches.

So if we were in base 5 the largest number possible in any place is 4 and the arithmetic would look
like this

5 + 2 = 7,
14 4+2 = 6 32
2+ 4- => 7 - 4 = 3to
21 23

So in base 2 (binary) the largest number possible in any place is 1 and a typical binary number looks
like

8’s 4’s 2’s 1’s


1 0 1 1 here the value of each of the 1’s is different
the arithmetic would look like this

Mathematics for Computer Science Page 13


MATH1179

11 10 101 100 1 . 111 11 . 01

1+ 1- 11+ 11 - 0 . 011+ 1 . 10-

100 1 1000 1 10 . 010 1 . 11

11

Binary, Octal and Hexadecimal


To represent numbers in the decimal system you have to have symbols for the digits 0-9 larger
numbers are a combination of these in different columns.

Binary, octal and hexadecimal are number systems based on 2, 8 and 16

Binary numbers are widely used in electronics and computing because they only consist of two
values, 0 and 1, which can be represented in terms of electrical current.

Converting between decimal and other bases:


Binary
BINARY 16’s 8’s 4’s 2’s 1’s 10011 base 2 (binary) becomes 16+2+1 = 19 in base 10
(decimal) we can write 100112 = 1910
1 0 0 1 1

2 )55 1
2 )27 1 5510 is converted to binary by successive divisions by 2 so becoming 1101112
2 )13 1
2 )6 0
2 )3 1
2 )1 1
0

Octal
This is another name for base 8 so that the digits used are 0-7.

OCTAL 512’s 64’s 8’s 1’s 1063 base 8 (binary) becomes 512+48+3 = 563 in base 10
(decimal)
1 0 6 3
we can write 1063 8 = 56310
8 )135 7
8 )16 0 13510 is converted to octal successive divisions by 8 becoming 2078
8 )2 2
0

Page 14 Numbers
MATH1179
Hexadecimal
This is another name for base 16 and now we have difficulty with our notation for digits
In hexadecimal we need to represent the digits 0-15 before we change columns

Example
In this addition using base 16, the 9 and 3 in the units column have not made a whole 16 to carry but
now the answer looks like 11216

1 9
3 + We overcome this by using letters for the digits between 10 and 15. So the digits used are
1 1 2 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F and the answer to our previous sum becomes 1C16

HEXADECIMAL 256’s 16’s 1’s 2AF base 16 (hexadecimal) becomes 512+160+15 =


2 A F 687 in base 10 we can write 2AF16 = 68710

16 )570 A
16 )35 3 57010 is converted to hexadecimal by successive divisions by 16 becoming 23A16
16 )2 2
0

Changing between Binary, Octal and Hexadecimal


If we compare these three number bases we can see they are all representing powers of 2
System Place value in each system
2n 29 28 27 26 25 24 23 22 21 20
Binary 512 256 128 64 32 16 8 4 2 1
Octal 512 64 8 1
Hex 256 16 1
It is clear from the table that Octal takes every third column from binary and hexadecimal every fourth
column. This gives us an easy way of converting between these three.

Binary to Octal or hexadecimal

Example
Given 110011011001102 we can convert it to octal by grouping the figures in threes from the right
and converting them individually as 3 digit binary numbers.

1 1 0 0 1 1 0 1 1 0 0 1 1 0
3 1 5 4 6

110011011001102 = 315468

Mathematics for Computer Science Page 15


MATH1179

Similarly we can convert 110011011001102 to hexadecimal by grouping the figures in four from the
right and converting them individually as 4 digit binary numbers.

1 1 0 0 1 1 0 1 1 0 0 1 1 0
3 3 6 6

110011011001102 = 336616
This same procedure can be reversed by converting each of the digits to a binary group of three or
four

Example
60358

6 0 3 5
1 1 0 0 0 0 0 1 1 1 0 1
so 60358 = 1100000111012 (you must put in all the three 0’s for the 0 in base 8)

2D516

2 D =13 5
0 0 1 1 1 1 0 1 0 1 0 1

so 2D516 = 11110101012 (you can omit the first two 0’s but not any in the middle)

Modular arithmetic
In programming languages we often use several types of division, Java has two operators for dividing
2 numbers, these are written as p/q and p%q.

Example
In Java if p = 13 and q = 5
then p/q = 2 ( the number of 5’s in 13) and p%q = 3 ( the remainder when all whole 5’s are taken out)

The % operator is called Modulo Division and we can use this idea in general with the set of integers.

Modular arithmetic is an idea closely linked to number bases and is arithmetic only concerned with
‘remainders’.

Page 16 Numbers
MATH1179
Example
A dial can be turned to any one of four positions, the diagram shows that once you have done 4 turns
you are repeating the sequence again, so in this sense 5 = 1 and 6 = 2 and 7= 3 and 8= 4 = 0

This example illustrates arithmetic MODULO 4 i.e. where any whole 4’s are discounted. We write
6 = 2 mod 4 or

11 = 3 mod 4 (if you take all the whole 4’s from 11 you are left with a remainder of 3)

Example
We can complete tables for arithmetic modulo 4 for addition and multiplication, the sins for these are
sometimes written ⊕4 and ⊗4 to avoid confusion with ordinary + and ×. We note that the only digits
we need are {0,1,2,3} (you can see the link with number bases)
⊗4 0 1 2 3
⊕4 0 1 2 3 0 0 0 0 0
3×3 =9
0 0 1 2 3 3+3 = 6 1 0 1 2 3 9 = 1 mod 4
1 1 2 3 0 6 = 2 mod 4 2 0 2 0 2
2 2 3 0 1 3 0 3 2 1
3 3 0 1 2
The set {0,1,2,3} is known as Z4 and in general Zn = {0,1,2,3,……n-1}

Now Backwards!
,

We can see from the dials that 3 positive turns are


equivqlent to one negative turn

This gives us the idea that 3 = -1 mod 4 so we could replace a negative number by a positive one and
so do addition instead of subtraction modulo 4
(3 -1) mod4 = (3 +3) mod4 = (6) mod4 = 2 mod 4 as expected

Mathematics for Computer Science Page 17


MATH1179

This idea uses addition by the complement of 1. Instead of subtraction by 1, we add 3.3 is the
complement of 1 as together they make 4.
(We have already used this ideas when dividing fractions by multiplying by its multiplicative inverse
3 4 3 4
e.g. ÷ = × where × = 1 )
4 3 4 3

2’s Complement arithmetic


If we translate our last example into binary we can begin to see how we could use the same ‘trick’.

00 00 00 00

11 01 11 01 11 01
11 01

10 10 10
10

So 3-1 = 3 +3 in binary becomes 112 – 012 = 112+112 = 1102 We seem to have got an extra digit as
we are no longer using modular arithmetic and ignoring complete 4’s so we have 6 (1102 ) instead of
2 (102)

Using binary 2’s complement we normally fix the length of the numbers stored so that this leading 1
is automatically lost.

Finding the Complement


In our simple example above we could see that 3 was the complement of 1 as they made 4 together.
In the binary we have 11 and 01 making 100 together

For larger binary numbers the complement is found by reversing the digits and adding 1.

Example
The 2’s complement of 100011102 is 011100012+1 = 011100102

11110000 11110000
so that 10001110 − is equivalent to 01110010 +
01100010 1 01100010
which gives the same answer ignoring the first 1.
Remember if you use 2’s complement you must work to the same number of digits

Page 18 Numbers
MATH1179

3: Algebra
As software developers and system builders the crucial first step is in understanding the clients’ needs
and expressing them correctly.

By now you will be familiar with the concept of a variable in programming. You will try to use
suitable names for quantities that may change in value during the running of the program.

Example
Bill ← Bill + Vat

You often need to identify these variables from a large amount of information and work out the
relationships between them. This is a skill known as modelling and will be developed in semester 2.
The algebra that you learnt at school makes this modelling more abstract by using letters rather than
names to represent variables.

Instead of writing Bill ← Bill + Vat


Let 𝒃𝒃 be the original Bill and 𝒗𝒗 the VAT due, then 𝒏𝒏 the new Bill is given by:
𝒏𝒏 = 𝒃𝒃 + 𝒗𝒗

Note that this is now an equation not an assignment. An equation by signifying that we have two
equal sides allows us to manipulate it.

Example
Given the relationship 𝒏𝒏 = 𝒃𝒃 + 𝒗𝒗 can you make b the subject? (derive an expression for b on its
own)
𝒏𝒏 = 𝒃𝒃 + 𝒗𝒗
𝒏𝒏 − 𝒗𝒗 = 𝒃𝒃 (subtracting v from both sides)
as both sides are equal this can also be read 𝒃𝒃 = 𝒏𝒏 − 𝒗𝒗

In terms of our model we can interpret this as saying the original Bill can be found by taking off the
VAT from the new Bill.

Linear Equations
One of the simplest relationships between variables is an equation which only involves simple terms
and an equality, like the previous example. The modeller’s skill is in trying to get that relationship
from the client’s specification or from the information presented to them. The final job puts this skill
in reverse as results from the relationships back into meaningful words.

Mathematics for Computer Science Page 19


MATH1179
Modelling Examples
A shop has a sale where all goods are reduced by 10%. If the article is £63 in the sale what was its
original price?

The two unknowns or variables here are the sale price and the original. We need to connect them
using the information given and so try and get an equation.

Let 𝑠𝑠: sale price and 𝑝𝑝: original price then sale is the original less 10% or sale is 90% of the original.
So,
90
𝑠𝑠 = × 𝑝𝑝
100
We can simplify this as 𝑠𝑠 = 0.9 × 𝑝𝑝
Now we can solve the problem where we know the sale price is £63. We substitute this in for s in the
general equation and rearrange it to solve for 𝑝𝑝.
𝑠𝑠 = 0.9 × 𝑝𝑝
So
63 = 0.9 × 𝑝𝑝
63
0.9
= 𝑝𝑝 (dividing both sides by 0.9)
Therefore
70 = 𝑝𝑝 (or more correctly: 𝑝𝑝 = 70)
The final part of the modelling is to interpret the result in terms of the real life situation i.e.
The original price of the article was £70

Example
The capacity of a big green Arriva bus is 70 people whereas a small single decker London Transport
red bus holds 40. If I have to transport 280 people on an outing to Broadstairs next month how many
buses will I need if I must hire them all from the same firm and use only one type?

This text has a lot of information which is not needed to solve the problem. We need to find the
variables (like Bill and Vat). One way to start modelling the variables is to work out what your
answer will be first!

It should be something like


I will need **** Arriva buses or &&& London Transport buses (where ***** and &&& will
be numbers) to take the 280 people to Broadstairs next month.

This gives us our variables:


Let v be the number of Arriva buses and t the number of London Transport buses.

Page 20 Algebra
MATH1179
Now we have to express the information we know in terms of our variables.

I have to transport 280 people on an outing to Broadstairs next month this problem is about
numbers of people

The capacity of a big green Arriva bus is 70 If I have v buses I can take 70 x v people written
70v people

A small single decker London Transport red bus holds 40. If I have t buses I can take 40t
people

Now we are ready to set up equations:

If I use v Arriva buses then equating the people 70v = 280 so (dividing both sides by 70) v = 4
If I use t London Transport buses then equating the people 40 t = 280 so (dividing both sides by 40)
t=7

Answers v = 4 or t = 7
This is NOT the answer to the problem it is only the answer to the equations!

Answer
I will need 4 Arriva buses or 7 London Transport buses to take the 280 people to Broadstairs next
month.

One of the main problems is sorting out which information is vital to the solution and which is only
included for background. With experience you can see only the bold type information is essential.

The capacity of a big green Arriva bus is 70 people whereas a small single decker London
Transport red bus holds 40. If I have to transport 280 people on an outing to Broadstairs next month
how many buses will I need if I must hire them all from the same firm and use only one type?

Solving equations
We have used the balancing approach to solving the equations we have so far come across (do the
same to both sides). This can be because we want to solve the equation or sometimes just to rearrange
it into a different form.

Example
The total pay, p, of a worker is given in terms of the number of hours, n, he works as a formula

p = 4.50n

Mathematics for Computer Science Page 21


MATH1179
rearrange this to make n the subject and then use your new equation to work out how many hours he
works if his pay is 87.75

p = 4.50n = 4.50 x n (dividing both sides by 4.50 )

p p 87.75
=n or n = so to solve our problem n = = 19.50
4.50 4.50 4.50

Answer NOT n = 19.50 but, if his pay is 87.75 then he works 19.5 hours

Example
The total pay, p, of a second worker is given in terms of the number of hours, n, he works and a
weekly bonus, b, he receives as a formula
p = 4.50n +b

rearrange this to make b the subject and then use your new equation to work out how much his bonus
is if his pay is 181.25 for a 38 hour week

p = 4.50n +b
p - 4.50n = b (subtracting 4.50n from both sides)
b = p - 4.50n Now we know the values of p and n so this becomes
b = 181.25 – 4.50x38 = 181.25 – 171.00 = 10.25
Answer: The bonus is 10.25

Example
The temperature in Fahrenheit, F, is found from the temperature in Celsius, C, by using the formula
9C
F= + 32
5
What formula should we use to get the temperature in Celsius from the temperature in Fahrenheit?
9C
F= + 32
5
9C
F-32 = (subtracting 32 from both sides)
5
5(F-32) = 9C (multiplying both sides by 5)
5
(F − 32) = C (dividing both sides by 9)
9
5
C= (F − 32)
9

Page 22 Algebra
MATH1179
Example
The annual amount of pension due to an employee is given first by working out how many
pensionable years service an employee has (how many years they have worked). This is then divided
by 80 and the result used to give the fraction of their final salary they will receive as pension. Find
out what pension an employee gets if they leave at 58 with a final salary of £35 000 and started work
when they were 25

The answer will be of the form: pension will be **** . So for the variable let p = pension

To calculate this we need to know pensionable years = y and final salary = f

Set up the equation by looking at the information on how it is calculated

y
p= × f If you want more detail y = l-s (last age – start age)
80

l−s
p= × f This is the sort of formula you can enter into a spreadsheet
80

58 − 25
For this actual case p = × 35000 = 14 437.50
80
The annual pension will be £14 437.50

If the final salary stays the same how many more years must they work so that the pension is over
£16 000?

Here the unknown variable is l so we must rearrange to make l the subject


l−s
p= ×f
80

80p
80 p = (l-s)f then = l−s
f

80p 80p
+s = l so l= +s
f f
putting our p = 16000 as required and putting f = 35000 and s = 25 gives

80 × 16000
l= + 25 = 61.57 so, they must work until they are 62 to be sure of a pension of £16 000
35000

Mathematics for Computer Science Page 23


MATH1179

4: Sequences and Series

4.1 Sequences
A sequence is essentially an ordered list of numbers, following some pattern. In most cases they are
infinite but finite sequences can also exist.
You might be able to define a sequence directly with a formula, or via previous elements in the
sequence.
We normally use notation such as 𝑥𝑥𝑛𝑛 to mean the n’th term of a sequence. Normally they follow some
pattern.
What is the next term in the following sequences?

1, 4, 9, 16, ….?
This is the sequence of square numbers (starting from 1). We can just describe this directly. The n’th
term is just given by 𝑛𝑛2 . We might write this slightly more mathematically as 𝑓𝑓𝑛𝑛 = 𝑛𝑛2

2, 5, 10, 17, ….?


This one is similar to the previous one. This is the sequence of square numbers (starting from 1) but
then adding 1. We can again just describe this directly. The n’th term is just given by 𝑛𝑛2 + 1
Again, we might write this slightly more mathematically as 𝑓𝑓𝑛𝑛 = 𝑛𝑛2 + 1.

2, 3, 5, 7, 11, ….?
This one is the sequence of prime numbers. Note that we don’t count 1 as a prime number – there are
good reasons for this! So the next term is the next prime number, which is 13. Hence 𝑓𝑓(𝑛𝑛) = n’th
prime number. Is there a formula for this? As far as we know, there is no formula for the n’th prime
number.
In some sense, the prime numbers are the “building blocks” of all the natural numbers. Yet we know
so little about them.
This illustrates the fact that in maths, we are constantly learning and there is so much more for us to
discover

1, 1, 2, 3, 5, 8, 13, ….?
This is a special type of sequence.
Each term (after the first two 1’s) is obtained by
adding the two previous terms together.
So the next term is 8 + 13 = 21
This is called the Fibonnaci sequence
You might define it by saying that 𝑓𝑓1 = 𝑓𝑓2 = 1 and
then 𝑓𝑓𝑛𝑛 = 𝑓𝑓𝑛𝑛−1 + 𝑓𝑓𝑛𝑛−2
This is a surprisingly useful series.
It is used for example in banking, where the expected share price, say, is obtained by using the
previous two values.

Page 24 Sequences and Series


MATH1179
It also appears a lot in nature – the number of bristles on a cone, or petals on a flower, tend to be one
of the numbers in the Fibonacci sequence (called a Fibonacci number).
Also appears a lot in music – tuning often uses a Fibonacci scale.
There is actually a formula for the n’th Fibonacci number. It is not easy, and not at all obvious why
it gives an integer solution! Don’t worry, you don’t have to learn this!
𝑛𝑛 𝑛𝑛
1 1 + √5 1 − √5
𝑓𝑓𝑛𝑛 = �� � −� � �
√5 2 2

Golden ratio
1+√5
The term that appears here is often referred to as the golden ratio. It is considered one of the
2
most aesthetically pleasing numbers. Many buildings, pictures, cars etc are designed according to the
golden ratio. Have a look around the campus.
Many of the buildings are built roughly to the golden ratio.
That is, the height and width are in the golden ratio (approximately 1: 1.618)
Why is this so aesthetically pleasing? I don’t know the answer – we can discuss in the class!

6, 7, 9, 8, 6, …..?
This one is trickier. There is no obvious pattern. You might be able to come up with lots of
possibilities depending on how you interpret the sequence
The number of letters in the days of the week (Monday = 6, Tuesday = 7 etc). So the next number
would be 8 (Saturday)
Note that by my definition, this could be a finite or infinite sequence – repeating the seven days to
make an infinite series. You may have thought of something else. So, we need to define sequences
carefully and unambiguously.

Sequence notation
Generally we use notation like 𝑠𝑠𝑛𝑛 to represent the n’th term of a sequence.
So, for example, with the sequence we had before, 1, 4, 9, 16, …. we have 𝑠𝑠𝑛𝑛 = 𝑛𝑛2
For the Fibonacci sequence we have 𝑠𝑠𝑛𝑛 = 𝑠𝑠𝑛𝑛−1 + 𝑠𝑠𝑛𝑛−2 . Note that we need to specify 𝑠𝑠1 = 𝑠𝑠2 = 1

4.2 Series
Sequences are useful in recognising patterns in numbers.
Things get more interesting, though, when you try to add up the numbers in a sequence.
If you add up (or sum) the terms then it is called a series.
The notation is called sigma notation – you may have seen this before.

Just for something for you to do first:


What is 1 + 2 + ⋯ + 100 ?

Mathematics for Computer Science Page 25


MATH1179
A historical tale tells the story of a maths teacher who had not prepared a class and basically wanted
to have a nap.
He decided to set this problem to his group of 8 and 9 year olds, assuming that it would take them at
least the full hour to complete.
A young boy called Carl Gauss solved it in around a minute. What is the answer?

Gauss noticed that 1 + 100 = 101


Then 2 + 99 = 101
Then 3 + 98 = 101
And it continues in this vein
There are 50 such pairs
So the answer is 50 x 101 = 5050
Easy when you see it!

Pattern spotting
Gauss essentially solved this by spotting patterns in the numbers. A lot of maths is pattern-spotting!
When you see the answer, it’s obvious, but it takes intuition, practice and experience to be able to
actually spot a pattern. Which is why most mathematical researchers spend half their life being stuck!

Sigma notation
Sigma notation uses a capital Greek letter ∑ (sigma) to denote summation.
You need to specify the start and end points of your sum (it is possible for these to be infinite).
Then you need to put a formula for each term of the sum after the sigma.
You work out the formula for the start point 𝒔𝒔, then the next term 𝒔𝒔 + 𝟏𝟏, and so on until you reach
the end point.
For example, our sum from 1 to 100 could be written in sigma notation as:

100

∑i
End point

Formula
Start point
i =1
This gives 1 + 2 + ⋯ + 100 as required

As another equation, consider the sum of the first ten squares 1 + 4 + 9 + 16 + ⋯ + 100
This could be written in sigma notation as
10

� 𝑖𝑖 2
𝑖𝑖=1
Note that the use of the letter 𝒊𝒊 is entirely arbitrary.
It is sometimes referred to as a dummy variable, we could use any symbol we like.
Page 26 Sequences and Series
MATH1179

Arithmetic progressions
An arithmetic progression is a sequence where each term differs from the previous term by a
common amount.

Example
1, 4, 7, 10, 13, 16, 19, 22
Infinite arithmetic progressions are not particularly useful as they will always sum to infinity.
Hence we will generally assume arithmetic progressions to be finite.
An arithmetic progression can be written by giving the starting term (usually denoted by 𝒂𝒂), the
common difference (usually denoted by 𝒅𝒅) and the number of terms (usually denoted by 𝒏𝒏)
An arithmetic progression is therefore of the form
𝑎𝑎, 𝑎𝑎 + 𝑑𝑑, 𝑎𝑎 + 2𝑑𝑑, … , 𝑎𝑎 + (𝑛𝑛– 1)𝑑𝑑
Check this has 𝑛𝑛 terms!
Hence the 𝑘𝑘’th term is given by:
𝑎𝑎 + (𝑘𝑘– 1)𝑑𝑑
It is possible to derive a formula for the sum of an arithmetic progression.
The common proof technique relates strongly to the idea that the young Gauss used in the example
we did previously. Hence the reason for mentioning it.
For simplicity, let’s define the last term in the sequence as 𝒍𝒍 (remember, we are assuming arithmetic
progressions are finite so there must be a last term)
Hence our arithmetic progression is of the form
𝑎𝑎, 𝑎𝑎 + 𝑑𝑑, 𝑎𝑎 + 2𝑑𝑑, … , 𝑙𝑙– 2𝑑𝑑, 𝑙𝑙– 𝑑𝑑, 𝑙𝑙
Let’s call the sum of this 𝑆𝑆𝑛𝑛 . So we have
𝑆𝑆𝑛𝑛 = 𝑎𝑎 + (𝑎𝑎 + 𝑑𝑑) + (𝑎𝑎 + 2𝑑𝑑) + ⋯ + (𝑙𝑙– 2𝑑𝑑) + (𝑙𝑙– 𝑑𝑑) + 𝑙𝑙
Now let’s use a little trick. Writing this in reverse gives
𝑆𝑆𝑛𝑛 = 𝑙𝑙 + (𝑙𝑙– 𝑑𝑑) + (𝑙𝑙– 2𝑑𝑑) + ⋯ + (𝑎𝑎 + 2𝑑𝑑) + (𝑎𝑎 + 𝑑𝑑) + 𝑎𝑎
Now, what happens if we add together these two equations?
In particular, what happens to all the 𝒅𝒅?
All the 𝒅𝒅 cancel and we get
2𝑆𝑆𝑛𝑛 = (𝑎𝑎 + 𝑙𝑙) + (𝑎𝑎 + 𝑙𝑙) + ⋯ + (𝑎𝑎 + 𝑙𝑙) + (𝑎𝑎 + 𝑙𝑙)
There are 𝒏𝒏 copies of 𝒂𝒂 + 𝒍𝒍 on the right hand side, and then dividing by 2 gives us:
The sum of the arithmetic progression with n terms, given first term a and last term l is:
1
𝑆𝑆𝑛𝑛 = 𝑛𝑛(𝑎𝑎 + 𝑙𝑙)
2
We often don’t know the last term, just the number of terms and the common difference
Substituting in for 𝑙𝑙 = 𝑎𝑎 + (𝑛𝑛– 1)𝑑𝑑 gives us:
The sum of the arithmetic progression with n terms, given first term a and common difference
d is:
1
𝑆𝑆𝑛𝑛 = 𝑛𝑛(2𝑎𝑎 + (𝑛𝑛 − 1)𝑑𝑑)
2
Mathematics for Computer Science Page 27
MATH1179
Note that if the terms are integers then this will always give an integer solution.
Note that we can use this to check Gauss’ answer for 1 + 2 + ⋯ + 100. We have first term 𝑎𝑎 = 1,
last term 𝑙𝑙 = 100, common difference 𝑑𝑑 = 1 and number of terms 𝑛𝑛 = 100
Use both versions of the formula to check you do indeed get the answer 5050!

We could of course write the sum of an arithmetic progression using sigma notation
In general the sum of an arithmetic progression can be given by
𝑛𝑛

� 𝑎𝑎 + (𝑖𝑖 − 1) 𝑑𝑑
𝑖𝑖=1
Note that you could choose to write this in other ways, for example
𝑛𝑛−1

� 𝑎𝑎 + 𝑖𝑖𝑖𝑖
𝑖𝑖=0
Choose the way that you feel is easiest for the problem you are dealing with!

Example
So, for example, the sum of the arithmetic progression 1, 4, 7, 10, 13, 16, 19, 22 could be written as
8 8

� 1 + 3(𝑖𝑖 − 1) = � 3𝑖𝑖 − 2
𝑖𝑖=1 𝑖𝑖=1
Calculate this using both versions of the formula

Example
What the arithmetic progression using sigma notation, 1, 5, 9, 13, 17, 21
6 6

� 1 + 4(𝑖𝑖 − 1) = � 4𝑖𝑖 − 3
𝑖𝑖=1 𝑖𝑖=1

Note that sigma notation can be used more generally.


For example, what is the formula for the sum of the first m integers 1 + 2 + 3 + ⋯ + 𝑚𝑚?
This is just an arithmetic progression, which can be written as
𝑚𝑚

� 𝑖𝑖
𝑖𝑖=1
Using either version of the formula, we have
𝑚𝑚
𝑚𝑚(𝑚𝑚 + 1)
� 𝑖𝑖 =
2
𝑖𝑖=1

Geometric progressions
In an arithmetic progression, each term differs by a common amount (the common difference).
In a geometric progression, each term differs by a multiple of a common amount.

Page 28 Sequences and Series


MATH1179
These can be more interesting, as it is possible to have their sum being finite even if there are infinitely
many terms
The definition of a geometric progression means that its terms are of the form
𝑎𝑎, 𝑎𝑎𝑎𝑎, 𝑎𝑎𝑟𝑟 2 , 𝑎𝑎𝑟𝑟 3 , ….
The 𝑛𝑛’th term is given by 𝑎𝑎𝑟𝑟 𝑛𝑛–1 where 𝑎𝑎 is the first term, and 𝑟𝑟 is the common ratio (the multiple by
which each term differs from the previous one)
For example, the sequence 1, 2, 4, 8, 16, 32 is a geometric progression with 𝑎𝑎 = 1 and 𝑟𝑟 = 2

What is the sum of a geometric progression?


For a simple example like 1 + 2 + 4 + 8 + 16 + 32 we can easily just do it manually (what is it?)
But in general we need a formula!
Especially as the series may be infinite and we just want the sum of the first 𝑛𝑛 terms, say
We can derive this formula, using a little trick again to help us
Let’s suppose we want the sum of the first 𝑛𝑛 terms
So we are looking for
𝑆𝑆𝑛𝑛 = 𝑎𝑎 + 𝑎𝑎𝑎𝑎 + 𝑎𝑎𝑟𝑟 2 + ⋯ + 𝑎𝑎𝑟𝑟 𝑛𝑛–1
The trick here is to multiply both sides by 𝑟𝑟
This give
𝑟𝑟𝑆𝑆𝑛𝑛 = 𝑎𝑎𝑎𝑎 + 𝑎𝑎𝑟𝑟 2 + ⋯ + 𝑎𝑎𝑟𝑟 𝑛𝑛–1 + 𝑎𝑎𝑟𝑟 𝑛𝑛
What happens if you subtract these expressions?
Almost all of the terms cancel out
You are just left with
(1– 𝑟𝑟)𝑆𝑆𝑛𝑛 = 𝑎𝑎– 𝑎𝑎𝑟𝑟 𝑛𝑛
Hence we have the following:
The sum of the first n terms of the geometric progression with first term a and common ratio r
is given by
𝑎𝑎(1 − 𝑟𝑟 𝑛𝑛 )
𝑆𝑆𝑛𝑛 =
1 − 𝑟𝑟
Note that this result does require that 𝑟𝑟 ≠ 1, otherwise we would be dividing by zero, which is not
allowed! This is a trivial case though, since the sequence is just 𝑎𝑎, 𝑎𝑎, 𝑎𝑎, … . , 𝑎𝑎, this sum is just 𝑛𝑛𝑛𝑛.

To check this formula works, can you use it to work out (you can use a calculator!) the sum of the
geometric progression we had before?
This was 1 + 2 + 4 + 8 + 16 + 32
Write down 𝑎𝑎, 𝑟𝑟 and 𝑛𝑛 and use the formula!
Hopefully you will get the answer 63

What would it be for similar geometric progressions with a different number of terms?

Mathematics for Computer Science Page 29


MATH1179
– 1 term gives 1
– 2 terms gives 1 + 2 = 3
– 3 terms gives 1 + 2 + 4 = 7
– 4 terms gives 1 + 2 + 4 + 8 = 15
– 5 terms gives 1+ 2 + 4 + 8 + 16 = 31
– 6 terms gives 1 + 2 + 4 + 8 + 16 + 32 = 63
– 7 terms gives 1 + 2 + 4 + 8 + 16 + 32 + 64 = 127

Example
Find the sum of the first 5 terms of the geometric progression where the first term 𝑎𝑎 = 3 and the
common ratio 𝑟𝑟 = 4
𝑎𝑎(1 − 𝑟𝑟 𝑛𝑛 )
𝑆𝑆𝑛𝑛 =
1 − 𝑟𝑟
5)
3(1 − 4 3(1 − 1024)
𝑆𝑆5 = = = 1023
1−4 1−4

Infinite geometric progressions


As previously mentioned, geometric progressions have the curious property that they can be infinite,
but still have a finite sum!
How can this be? Surely if you add up infinitely many things, the sum must also be infinite?
This issue perplexed mathematicians for centuries and it was only really with the introduction of
calculus that people started to understand the concept.
Let’s look at a couple of simple illustrations

Zeno’s paradox
Zeno’s paradox is from ancient Greece
Suppose a hare was to run against a tortoise in, let’s say, a 100m race
The tortoise is obviously slower, so the hare agrees to let the tortoise have a head start, say 10m
Who is going to win this race?
The tortoise claims that it will win. In order for the hare to catch the tortoise, it firstly has to catch up
to the point where the tortoise started. But by then, the tortoise will have moved on a bit (say 1m)
So the hare still has to catch up.
So it has to catch up to the point where the tortoise is now
But in that time, the tortoise will have moved on a bit further
So the hare has to catch up that bit more
When catching up, the tortoise will move on a little bit more
And so on…
So the hare can never catch the tortoise
So the tortoise will win
Is this true?

Page 30 Sequences and Series


MATH1179
Clearly not, but why?
We have infinitely many catch-ups to do, but in reality, the hare will overtake the tortoise
The only way this can happen is that these infinitely many catch-ups give a finite answer as their sum
This bothered mathematicians for centuries!
The “paradox” here is the assumption that infinitely many things must add up to “infinity”
But this is clearly untrue as the hare will win the race
It was only once limits were defined that we started to understand this!

Cutting wood
As another example, suppose you have a 2m long piece of wood. You cut it in half and keep 1m,
leaving you with 1m left over

1m 1m

Now from the 1m you have left, you cut in half again, keeping 1/2m and having 1/2m left over
And keep going like this, constantly cutting the remainder in half.

1m ½m ½m

The total amount of wood you keep is given by 1 + ½ + ¼ + …..


This is just the sum of a geometric progression with 𝑎𝑎 = 1 and 𝑟𝑟 = ½
It’s an infinite series, but clearly can never exceed 2 as the original length was only 2m
So we have this concept of a “sum to infinity” actually taking a numeric value (in this case, 2)

Bouncing balls
A similar example is to think of dropping a ball. At each bounce it bounces to half of its previous
heights, so it (theoretically at least) bounces infinitely many times.
Yet it will stop bouncing after a finite amount of time.

Infinite geometric progressions


The formula for the sum of an infinite geometric progression is not difficult to work out.
Let us assume that |r| < 1 (if not, the series will just go off to infinity)
Recall the formula for the sum of a geometric progression
𝑎𝑎(1 − 𝑟𝑟 𝑛𝑛 )
𝑆𝑆𝑛𝑛 =
1 − 𝑟𝑟
𝑛𝑛
Given that |𝑟𝑟| < 1 , what happens to 𝑟𝑟 as 𝑛𝑛 becomes very large? (we say as 𝑛𝑛 tends towards infinity,
or 𝑛𝑛 → ∞)
This tends towards 0, and so 1– 𝑟𝑟 𝑛𝑛 tends towards 1
Hence we can deduce a formula for the “sum to infinity” for such a progression
We have the following:

Mathematics for Computer Science Page 31


MATH1179
If |𝒓𝒓| < 𝟏𝟏, the sum to infinity of the geometric progression with first term a and common ratio
𝒓𝒓 is given by
𝑎𝑎
𝑆𝑆∞ =
1 − 𝑟𝑟
Check this works with the cutting wood example – do you get the answer of 2?

Conceptualisation
Conceptually, this is quite a difficult thing to comprehend
Adding up infinitely many things to get towards a finite limit is a difficult thing to fully grasp.

Convergence and divergence


Remember, this only works if |𝑟𝑟| < 1
If not, it just goes off to infinity
If |𝑟𝑟| < 1 and so we have a finite solution, we can call the series convergent (it “converges”, “gets
closer and closer” to a limit point)
If not, then it is called divergent (it just goes off to infinity)

Arithmetic and geometric progressions - definitions and formulae

Arithmetic Progressions
An arithmetic progression is a sequence of the form
𝑎𝑎, 𝑎𝑎 + 𝑑𝑑, 𝑎𝑎 + 2𝑑𝑑, … , 𝑎𝑎 + (𝑛𝑛 − 1)𝑑𝑑
Where 𝑎𝑎 is the first term, 𝑑𝑑 is the common difference and 𝑛𝑛 is the number of terms.
The j’th term is given by 𝑎𝑎 + (𝑗𝑗 − 1)𝑑𝑑
If 𝑎𝑎, 𝑑𝑑 and 𝑛𝑛 are defined as above, and 𝑙𝑙 is the last term, the sum of an arithmetic progression can be
given by either of the formulae:
1 1
𝑆𝑆𝑛𝑛 = 2 𝑛𝑛(𝑎𝑎 + 𝑙𝑙), or 𝑆𝑆𝑛𝑛 = 2 𝑛𝑛(2𝑎𝑎 + (𝑛𝑛 − 1)𝑑𝑑)

Geometric progressions
A geometric progression is a sequence 𝑎𝑎, 𝑎𝑎𝑎𝑎 𝑎𝑎𝑟𝑟 2 , 𝑎𝑎𝑟𝑟 3 , …
where 𝑎𝑎 is the first term and 𝑟𝑟 is the common ratio
The 𝑗𝑗’th term is given by 𝑎𝑎𝑟𝑟 𝑗𝑗–1
If 𝑎𝑎 and 𝑟𝑟 are defined as above, the sum of the first 𝑛𝑛 terms of a geometric progression is given by
𝑎𝑎(1 − 𝑟𝑟 𝑛𝑛 )
𝑆𝑆𝑛𝑛 =
1 − 𝑟𝑟

Infinite progression
An infinite arithmetic progression will always diverge.
If |𝑟𝑟| ≥ 1 an infinite geometric progression will always diverge.
𝑎𝑎
If |𝑟𝑟| < 1 an infinite geometric progression will converge to 1−𝑟𝑟

Page 32 Sequences and Series


MATH1179

5: Logic
Logic is the area of mathematics used for reasoning and constructing proofs. It has applications in
computing science to networking, reasoning about and writing programs and to program
specification. It is also necessary in the wider context of data modelling and system building.

5.1 Propositional Calculus

Propositions
A proposition is a statement which can be given the value true or false

Example
'two plus two = five', 'it is Tuesday today' are propositions

'x+y=3', 'she is happy' are not propositions as they


contain undetermined elements
(x,y and she are not specified)

Example
Consider the following argument:
If the program syntax is faulty or if program execution results in division by zero, then the
computer will generate an error message. Therefore if the computer does not generate an
error message, then the program syntax is correct and program execution does not result in
division by zero.

This involves three propositions. We shall call them p, q and r as follows:

p : the program syntax is faulty,


q : program execution results in division by zero
r : the computer generates an error message.

We also need 'the program syntax is correct'. This is the same as 'the program syntax is not faulty'
and we shall call this ‘not p’. Similarly we have not q, not r.

In terms of p, q and r we can translate the argument to: If p or q, then r


Therefore if not r, then not p and not q.

Note We shall use lower case letters to represent propositions.


p : 'it is raining ' is read p is the proposition 'it is raining '

Mathematics for Computer Science Page 33


MATH1179
Compound Propositions
Simple propositions can be linked by the logical connectives AND and OR to give a compound
proposition.

If p is the proposition 'I eat chips' and q the proposition 'I eat salad'
then the conjunction of the propositions is 'I eat chips AND I eat salad' ( I eat chips and salad)
In symbols we write this p ∧ q.
The new proposition is true precisely when p and q are both true.

The disjunction of the propositions is 'I eat chips OR I eat salad' ( I eat chips or salad)
In symbols we write this p ∨ q.

Note In English, p or q offers two interpretations -


(i) p or q but not both - the exclusive or (Do you want red wine or white wine )
(ii) p or q or both - the inclusive or (Do you want milk or sugar )

In symbolic logic p ∨ q will always mean p or q or both. The proposition p ∨ q is true when either
p or q or both p and q are true. (Otherwise we have to use the exclusive or.)

The negation of a proposition p, written ¬p,( or sometimes ~ p) is the proposition which is false
whenever p is true and true whenever p is false.

Example
p : it is raining q : it is cold r : it is Monday

p∧q it is raining and it is cold, p∨q it is raining or it is cold


¬p it is not raining, p∧q∧r it is raining, cold and Monday

Truth values and tables


The truth value of a proposition is its truth or falsity. A proposition which is true has truth value T
and one that is false has truth value F. Truth tables are used to determine the truth or falsehood of
compound statements.

P q p∧q p q p∨q p ¬p
T T T T T T T F
T F F T F T F T
F T F F T T
F F F F F F

Two compound propositions are logically equivalent if they always have the same truth value.

Page 34 Logic
MATH1179
Example
p : the programming team is happy;
¬p : it is not true that the programming team is happy
¬¬p : it is not true that it is not true that the programming team is happy
which is equivalent to the programming team is happy.
So, ¬¬p and p are logically equivalent. We write p ≡ ¬¬p.

Example
Using a truth table show that ¬(p ∧ q) ≡ ¬p ∨ ¬q.

We have to show that for all the different truth values for p and q, the two compound propositions
have the same truth value.
We construct a table as shown below. In the columns headed p and q we put all the different
combinations there are for truth values for p and q. In the next column we write down the resulting
truth values for p ∧ q; in the next column the truth values for ¬(p ∧ q). Similarly we find the truth
values for ¬p ∨ ¬q.
To show the compound propositions ¬(p ∧ q) and ¬p ∨ ¬q are equivalent we compare the truth
values.

In this case they are identical and so the compound propositions are equivalent.

p q p∧q ¬(p ∧ q) ¬p ¬q ¬p ∨ ¬q
T T T F F F F
T F F T F T T
F T F T T F T
F F F T T T T

This means that 'it is not true that I got over 40% and failed' is equivalent to 'I did not get over 40%
or I passed'.

Example
Show that p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)
p Q R q∧r p ∨ (q ∧ r) p∨q p∨r (p ∨ q) ∧ (p ∨ r)
T T T T T T T T
T T F F T T T T
T F T F T T T T
T F F F T T T T
F T T T T T T T
F T F F F T F F
F F T F F F T F
F F F F F F F F
A tautology - t - is a proposition which is always true eg p ∨ ¬p; it is wet or dry.
A contradiction - f - is a proposition which is always false eg p ∧ ¬p; it is wet and dry.

Mathematics for Computer Science Page 35


MATH1179
Laws of Propositional Calculus
Given any propositions p, q and r, tautology t and contradiction f, the following hold:

1 idempotent laws p∧p≡p p∨p≡p


2 associative laws (p ∧ q) ∧ r ≡ p ∧ (q ∧ r), (p ∨ q) ∨ r ≡ p ∨ (q ∨ r)
3 commutative laws p ∧ q ≡ q ∧ p, p∨q≡q∨p
4 distributive laws p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r), p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)
5 De Morgan's laws ¬(p ∧ q) ≡ ¬p ∨ ¬q, ¬(p ∨ q) ≡ ¬p ∧ ¬q
6 identity laws p ∧ f ≡ f, p ∧ t ≡ p, p ∨ t ≡ t, p ∨ f ≡ p
7 complement laws p ∧ ¬p ≡ f, p ∨ ¬p ≡ t, ¬t ≡ f, ¬f ≡ t
8 involution laws ¬¬p ≡ p,

Note that this table is similar to the table of laws of sets.


All we do is replace ∩ by ∧, ∪ by ∨, ' by ¬, U by t and ∅ by f.

Example
Show that ¬a ∨ (a ∧ b) ≡ ¬a ∨ b

Using the above: ¬a ∨ (a ∧ b) ≡ (¬a ∨ a) ∧ ( ¬a ∨ b) distributive Law 4


≡ t ∧ ( ¬a ∨ b) complement Law 7
≡ ¬a ∨ b identity Law 6
Using truth tables

a B ¬a a∧b ¬a ∨ (a ∧ b) ¬a ∨ b
T T F T T T
T F F F F F
F T T F T T
F F T F T T

Implication
This logical connective is of the form ' p implies q ', or ' if p then q '.
p is called the premise and q the conclusion. We write this p → q.

To construct the truth table for the implication consider the example
'if I want some bread then I shall go to the supermarket'

Page 36 Logic
MATH1179

The only time this statement is false is when the conclusion (I go to the supermarket) is false but the
premise (I want some bread) is true.

So the truth table for this structure is


p q p→q
T T T
T F F
F T T
F F T

The last but one line of this table may seem confusing (the premise is false but the conclusion is true),
but it fits in with our example. An alternative equivalent formula is sometimes easier to work with.

We can show that ¬p ∨ q gives the same truth table

p q ¬p ¬p ∨ q
T T F T
T F F F
F T T T
F F T T

Valid and Invalid Arguments


One use of logic is to analyse the validity of an argument. An argument is a relationship between a
set of propositions p1, p2, ... , pn called premises and another proposition q called the conclusion.

An argument has the form p1, p2,... pn therefore q.

An argument is valid if whenever all the premises p1, p2, ..., pn are true then the
conclusion q is true.

An argument is a fallacy if there is a case when all the premises p1, p2, ... , pn are
true and the conclusion q is false.

Mathematics for Computer Science Page 37


MATH1179
Example
If I watch TV then I fall asleep. I watch TV or I fall asleep. Therefore I fall asleep
Identify the propositions:
w: I watch TV s: I fall asleep

Now translate each premise and conclusion into logic notation.


premise 1 If I watch TV then I fall asleep w→s
premise 2 I watch TV or I fall asleep w∨s
conclusion Therefore I fall asleep s

Notation We replace the word therefore by the symbol ├ , so the argument becomes

w→s, w∨s s

To test the validity of the argument we construct a truth table showing the original propositions, each
of the premises and the conclusion

premises conclusion Argument


w s w→s w∨s s
T T T T T VALID
T F F T F
F T T T T VALID
F F T F F

We can see from the table that whenever the premises are true the conclusion is true.
Here we have a VALID argument

Example
If Harry doesn't practice the piano he will fail Grade 8. Harry fails Grade 8 therefore he didn't practise.

Identify the propositions: p: Harry practises the piano and g: Harry fails grade 8

Now translate each premise and conclusion into logic notation.


premise 1 If Harry doesn't practise the piano he will fail Grade 8 ¬p→g
premise 2 Harry fails Grade 8 g
conclusion therefore he didn't practise. ¬p

The argument becomes ¬ p→g, g ¬p


Page 38 Logic
MATH1179
We construct a truth table showing the original propositions, each of the premises and the conclusion

premises conclusion Argument


p g ¬p→g g ¬p
T T T T F FALLACY
T F T F F
F T T T T
F F F F T

We can see from the table that although the conclusion is true when the premises are true in the third
line, in the first line the conclusion is false. An argument is only valid if whenever the premises are
true the conclusion is true so this argument is a FALLACY.

Example
If I study then I will not fail Mathematics. If I do not play football then I study.
But I failed Mathematics. Therefore I played football
Take as the propositions
s : I study; m : I fail Mathematics; b : I play football
premise 1 If I study then I will not fail Mathematics. s → ¬m
premise 2 If I do not play football then I study. ¬b → s
premise 3 But I failed Mathematics. m
Conclusion Therefore I played football. b

Then the argument becomes


s → ¬m, ¬b → s, m b
To test the validity of the argument we construct the truth table which has eight rows because we
have three propositions in this example.
premises conclusion Argument
s m b ¬m ¬b s→¬m ¬b→s m b
T T T F F F T T T
T T F F T F T T F
T F T T F T T F T
T F F T T T T F F
F T T F F T T T T VALID
F T F F T T F T F
F F T T F T T F T
F F F T T T F F F
Here we see that the only case where all the premises are true, the conclusion is also true so we have
a VALID argument

Mathematics for Computer Science Page 39


MATH1179
Equivalent systems
In general many aspects of logic can be re-expressed using Boolean forms, Set theory or Logic gates.

and or complement

p∧q p∨q ¬p ~p f t

P∩Q P∪Q P P´ ∅ U

P.Q P+Q P 0 1

Statements and equations can be written in equivalent forms

i.e. We can see De Morgan’s Laws in three different forms

(P∩Q)' = P′ ∪ Q'

¬(p∧q) = ¬ p ∨ ¬ q

(P.Q)´ = P´+Q´

Sometimes the laws are easier to remember in one of the forms

p∧t = p P.1 = P P∩U = P

p∧f = f P.0 = 0 P∩∅ = ∅

p∨t = t P+1 = 1 P∪U = U

p∨f = p P+0 = P P∪∅ = P

Propositions and Computer Logic


There is a relationship between the laws of propositions and the wiring diagrams for electronic circuits
in computers, control devices, etc.

Electronic Gates
Electronic switches and gates are designed to operate the same way as logical connectives. These
gates have inputs from wires carrying voltages; the convention is that a low voltage is treated as 0
and a high voltage as 1. Truth tables for propositions are written in terms of T and F however when
dealing with circuits we shall replace T by 1 and F by 0.

Page 40 Logic
MATH1179
The OR gate receives inputs from two wires carrying voltages and works likes the logical connective
∨. If the inputs are p and q, we write the output as p+q rather than p∨q.

OR Input Output Truth Table


p q r = p+q p q p∨q
p 1 1 1
r T T T
q 1 0 1 T F T
0 1 1 F T T
0 0 0 F F F

The AND gate works like the logical connective ∧. The output is written p.q instead of p∧q.

AND Input Output Truth Table


p p q r = p.q p q p∧q
r 1 1 1 T T T
q 1 0 0 T F F
0 1 0 F T F
0 0 0 F F F

The NOT gate has one input and behaves like ¬. If the input is p, the output is p .

NOT Input Output Truth Table


p r= p p ¬p
p r
1 0 T F
0 1 F T

We can use the laws of logic to simplify circuits.

Example
p r
q

The Boolean expression for this circuit is p .q .


When p = 0 and q = 0, the output is p .q = 0.0 = 1.0 = 0 .

When p = 0 and q = 1, the output is p .q = 0.1 = 11


. =1

Mathematics for Computer Science Page 41


MATH1179
Example
Draw the circuit for the Boolean expression p + (p . q) and show that it is equivalent to p + q
p + (p . q) = ( p + p ).(p + q ) = 1.( p + q ) = ( p + q )

p p
q q

Circuits for Binary Addition


Suppose p and q are binary numbers containing one bit (i.e. they are either 0 or 1).
Design a logic circuit to find the sum of p and q
Now 02 + 02 = 002 12 + 02 = 012 02 + 12 = 012 12 + 12 = 102
This means that when we add p and q, there are two inputs and two outputs. In the output, the right
digit is called the sum s and the left digit is called the carry c.

The following circuit finds the sum and carry p q carry sum
0 0 0 0
s = ( p + q ) .( p .q ) 1 0 0 1
c = p.q 0 1 0 1
1 1 1 0

5.2 Predicate Calculus


Predicates can be considered as 'generalised propositions'.

x + 3 = 8 is a one place predicate, it may or may not be true depending on the value of one object x. If
we give x a specific value then the statement becomes a proposition.
For example, 2 + 3 = 8 F or 5 + 3 = 8 T

We can see that when x has the value 5 the proposition is true and we say 5 satisfies the predicate. The
predicate can also be thought of as a propositional function using the notation p(x).

Example
q(x) : x is an even number r(x) : x is less than 8

In a similar way to propositions we can use our connectives ∧,∨ and ¬.


¬ q(x) = ¬ (x is an even number) = x is an odd number,
r(x) ∧ ¬ q(x) = x is less than 8 ∧ x is an odd number.

Page 42 Logic
MATH1179
Quantification
Universal quantifiers are used to express the concept that a predicate is being stated for all possible
values of the object x.

Example
Everyone loves maths, is the extension of x loves maths to include all possible people and can be
expressed
For every x, x loves maths.

We use the symbol ∀ to denote for every or for all. There is an implied universal set called the
universe of discourse underlying the use of this quantifier. For instance when we say everyone we
are implying a universe of discourse that might be all the people in the world, everyone we know or
all the students in a class.

Example
p(x) : x has red hair. ∀ x p(x) would mean everyone has red hair, this is more likely if the universe
of discourse underlying this statement was not the world population but rather a particular family.

It is not always easy to translate a sentence into predicates.

Example
All mathematicians are highly intelligent has to use 2 predicates

p(x) : x is a mathematician q(x) : x is highly intelligent


so that our sentence becomes ∀ x (p(x) → q(x))

Existential quantifiers are used to state that at least one object exists that satisfies the predicate.

It is used to translate sentences such as ' there is a number that is greater than 8 '. Note than unless the
universe of discourse here were to be natural numbers less than 10 there would be more than one value
of x satisfying the predicate. The symbol used to state existential quantification is ∃ for there exists or
at least one.

Example
If s(x) : x has a sports car , then ∃ x s(x) could be translated as
there exists an x such that x has a sports car

or equivalently if we are considering x from a set of people – there is someone with a sports car, or at
least one person has a sports car or even some people have sports cars

Mathematics for Computer Science Page 43


MATH1179
Examples
Everyone likes chips becomes ∀ x(x likes chips)

There are no odd numbers greater than 3 dividing 9


becomes ¬ ∃ x((x is odd) ∧ (x>3) ∧ (x divides 9))

Nobody likes the dentist becomes ¬ ∃ x(x likes the dentist)


or equivalently ∀ x(x does not like the dentist)

Two-place predicates
The same ideas can be used to generate predicates with more than one variable. These are written in the
same way using an ordered pair.
Consider the proposition 'Programming team A is happy with specification 2.' Suppose we want to
change this so that it deals with a variety of programming teams and a variety of specifications.
We create a function h(p,s) : Is Happy with, which takes two values, 'p' from the set of programming
teams and 's' from the set of specifications and gives truth values T and F dependent on both these
variables It can be thought of in a similar way to a programming function

IsHappyWith(p,s) returns the boolean value true or false (i.e has truth values T or F).

So if team A is happy with spec 2 but unhappy with spec 1 we would write
IsHappyWith(teamA, spec2) = true IsHappyWith(teamA, spec1) = false

Examples
Let S be a set of programs under discussion. We can define the predicate Compiles(s) which takes
the value T, true
if program s ∈ S compiles.

Let E be a set of employees and P a set of programming languages.


We can define the predicate ExpertIn(p,e) takes the value T, true if employee e ∈ E is an expert in
the programming language p ∈ P.
We can use our previous quantifiers with 2-place predicates

Example
We define k(x,y) : x knows y. Take the universe of discourse as a village, we could say

Everyone knows everyone else, and write it ∀ x ∀ y(x knows y) or ∀ x ∀ y(k(x,y))

Everyone knows someone, written ∀ x ∃ y(x knows y) or ∀ x ∃ y(k(x,y))

Sentences written like this will have truth values dependent on the values each variable takes.

Page 44 Logic
MATH1179
Example
Given the universe of discourse is N the natural numbers, ∀ x ∀ y(x = y) is clearly false i.e ≡ f
but ∀ x ∃ y(y>x)≡ t.

Applications to Programming
Prolog (PROgramming in LOGic) is a logic programming language which uses Horn clauses and
resolution calculus and may be covered in detail later in the AI course in the final year of the degree. The
definitions given here are just for interest.

Definition A Horn clause is a formula in clausal form restricted to having just one non negative
literal.

Definition Resolution inference rule. For any clauses A,B and C


A ∨ B
¬B ∨ C
A ∨ C

Mathematics for Computer Science Page 45


MATH1179

6: Relations, Mappings and Functions

6.1 Relations
Consider the set of students S = {Simina, Helen, Jawed, Stephen, Aysha, Jo } on the second year of
a programme of study.
They choose up to two electives from the set of courses, C = {G23, G67, N45, H43} as follows:

Simina, Helen Jawed Stephen Aysha Jo


G67 G23 H43 G23, G67

This is an example of a relation between the sets S and C.

SIMINA
HELEN G23 The relation is many-many as each
student may take many courses and
JAWED G67
each course may be taken by many
STEPHEN N45 students.
AYSHA H43
JO

We can also express this relation, R, as the set of ordered pairs:


R = {(Simina, G67), (Helen, G67), (Jawed, G23), (Stephen, H43), (Aysha, G23), (Aysha, G67)}

In general terms, a relation between sets A and B is a rule which associates some of the elements in
the set A with some of those in B. It is a subset of the product set A × B (as discussed in semester
one).
If you are familiar with database concepts you will see a one-many relation illustrated here in Access

with one customer owning several second hand cars.

Page 46 Functions
MATH1179
There are problems in Access when modelling many-many relations.
Given a relation we can define two mappings, called the projection mappings or projection
functions. They are designed to pick out one half of the ordered pair so that

p1
A
A×B p1 : A × B → A ,
p2 : A × B → B ,
p 1 (a, b) = a,
p 2 (a, b) = b
p2 B
In the example above, p 1 (Simina, G67) = Simina, p 2 (Stephen, H43) = H43

• The range of p 1 = { Simina, Helen, Jawed, Stephen, Aysha } .


o This is the subset of students taking one or more of the given courses.
• The range of p 2 = {G23, G67, H43} .
o This is the subset of courses taken by the students.

Now p 2 (Simina, G67) = G67, p 2 (Helen, G67) = G67 and p 2(Aysha, G67) = G67
Therefore
p 2 –1(G67) = {(Simina,G67),(Helen, G67), (Aysha, G67)}
Using the ideas of composition of functions which we shall deal with next,

p 1 ο p 2 –1 (G67) = p 1{(Simina,G67),(Helen, G67), (Aysha, G67)}


= {Simina, Helen, Aysha }

So, p 1 ο p 2 –1 (G67) is the subset of students who are taking G67.

pair
p1-1 p2-1
s

p1 p2

S C
student course
s relation

Similarly p2 ο p1-1 (Aysha) finds the subset of courses that Aysha has chosen.
So p2 ο p1-1 (Aysha) = p 2 { (Aysha, G23), (Aysha, G67) }
= {G23, G67}

This sort of operation is accomplished in a database with queries to pick out the fields you want using
the fields you know, e.g. From the above Access tables we can see that we could start with a customer
and find all the second hand cars they have owned.
Mathematics for Computer Science Page 47
MATH1179

6.2 Functions
A function is a rule which associates with each element of a set A, called the domain of the function,
exactly one element of another set B.

Example
A = subset of lecturers in the School of Computing and Mathematical Sciences, B = subset of rooms
at Greenwich Maritime Site. Each of the lecturers in the set A has a room which is in the set B and
so we have a rule linking the elements of A with the elements of B. We can see this clearly in a
diagram.

Finney 237
Fryer 305
Domain A Taft Co-domain B
523
Petridis
100

This is an example of a function, since every lecturer has a room, so every element in A is related to
an element in B - and no lecturer can have two rooms.

We usually give the function a name, say f and write f : A → B


The set A is called the domain of the function and the set B the co-domain.

Suppose f associates a ∈ A with b ∈ B.


We say that the image of a is b and write f: a → b or f(a) = b.

In the example above if r is the function from the set A to the set B,
then
the image of Fryer is 305
or r : Fryer → 305,
or r(Fryer) = 305

Example k u
1
v
Let A = { 1, 2, 3, 4}, B = { u, v, w, x, y, z}. w
2
3 x
A function k : A → B is defined by y
4
k(1) = w, k(2) = x, k(3) = x, k(4) = z. z

This example demonstrates the following facts about functions:


1 Every element of the domain must be sent to an element in the co-domain.
2 Some elements of the co-domain may fail to have any element of the domain sent to them
3 No element of the domain is sent to more than one element of the co-domain.
4 Elements of the co-domain may have more than one element of the domain sent to them e.g. x.

Page 48 Functions
MATH1179
The set of values in the co-domain B which are images of f is called the range or the image set of
the function f.

In the previous example, the range of k is {w, x, z}. In the example of the lecturers’ rooms, 100 is in
the co-domain but not in the range of r as none of the lecturers have that room.

The use of the co-domain and range allow you to declare types (e.g. currency) for the output of your
functions in programming before the computer produces actual values for the output (e.g. £3.45).

Classification of Functions
Let f be a function from the set A to the set B.

f is one-one (or injective) if no two elements in the domain are sent to the same element in the
co-domain. If this is not the case f is called many-one.

f is one to one f is many to one

Example
A function which maps every student to their address is many to one (assuming there are at least 2
students sharing accommodation.)

Example
A function which maps every student to their registration number is one to one.

f is onto (or surjective) if every element in B is the image of an element in A. In this case the range
of f and the co-domain B are the same sets. If the range of the function is not the same as the co-
domain then f is said to be into.
range

A f B co-domain
A f B

f is onto f is into
range = co-domain range ⊂ co-domain

Example
A function from UoG students to the set of home addresses which maps every student to their home
address is onto (assuming an address is held when a student lives there.)

Mathematics for Computer Science Page 49


MATH1179
Example
A function from UoG students to the Natural numbers which maps every student to their registration
number is into (think of the order of each set).

We note that to decide whether a function is onto or into, the domain and co-domain have to be stated
clearly.

From a programming point of view we can think of the function as a ‘black box’ turning inputs into
outputs.

inputs outputs
A function ran f

Composition of Functions
When programming or using formulae in databases or spreadsheets we often need to nest our
functions.
e.g. E1 =AVERAGE(MAX(C1:D1),MAX(A1:B1))

We have already considered the use of brackets to ensure that arithmetic operations are carried out in
the desired order, now we look at ways of describing putting 2 or more functions together.

Let A = { 1, 2, 3, 4}, B = { w, x, y, z}, C = { p, q, r}


Function f : A → B is defined by f(1) = w, f(2) = y, f(3) = y, f(4) = z;
Function g : B → C is defined by g(w) = p, g(x) = q, g(y) = r, g(z) = q.

This can be illustrated in the following diagram:

A f B g C
1
w p
2
x q
3
y r
4
z

We can combine the functions : first f then g. So 1 → w → p, 2 → y → r etc


We call this mapping from A → C, g ο f . So g ο f acting on x is g ο f (x) = g(f(x)).
So g ο f (1) = g(f(1)) = g(w) = p. Similarly g ο f (2) = r; g ο f (3) = r; g ο f (4) = q.

inputs output domain g inputs outputs


domain f f ran f g ran g

Note the order for g ο f, f first then g. Also the range of f must be a subset of the domain of g.

Page 50 Functions
MATH1179
Inverse of a function
Suppose that f is a 1-1 function from A onto B, we can define a function from B to A which "undoes"
f. We call this function the inverse of f and write it f -1
Dom -Domain
inputs outputs
f Ran - Range

dom f ran f =
-
= ran f dom f -

f -1
inputs
outputs

Example
A = { 1, 2, 3}, B = { x, y, z}. Define the function f : A → B, f(1) = z, f(2) = x, f(3) = y
Then
1 z
f -1 : B → A, f -1(x) = 2, f -1(y) = 3, f -1(z) = 1 2 x
3 y

Note for a function f to have an inverse it must be:


1-1 otherwise if f is many to one the inverse would be one-many which is not allowed for a
function.
onto otherwise there will be elements in the co-domain of f (and therefore the domain of f –1 ) which
are not images of f. In this case f –1 will have nowhere to ‘send them back’ so it will break
another function rule that every member of the domain of f-1 must be sent to an element in its
co-domain.

Functions defined by formulae


Suppose f is a function which acts on the elements of the set N as follows :
f(1) = 3, f(2) = 5, f(3) = 7, f(4) = 9.
i.e. f doubles each element of N and then adds 1 to the result.

We could describe this function by the formula : f(n) = 2n + 1, n ∈ N.

Examples
1 f(n) = 5n - 4, n ∈ N. So f(1) = 5 × 1 - 4 = 5 - 4 = 1
f(2) = 5 × 2 - 4 = 10 - 4 = 6
f(5) = 5 × 5 - 4 = 25 - 4 = 21

2 g(n) = 4n 2 + 1, n ∈ N. So g(1) = 4 × 1 2 + 1 = 4 + 1 = 5
g(3) = 4 × 3 2 + 1 = 36 + 1 = 37
g(6) = 4 × 6 2 + 1 = 144 + 1 = 145

Mathematics for Computer Science Page 51


MATH1179
3 g ο f (n) = g(5n-4) = 4(5n-4)2 + 1 = 100n2 –160n +64
g ο f (2) = g(6) = 145

4 f ο g (n) = f(4n 2 + 1) = 5(4n 2 + 1) – 4 = 20n2 +1 Dom -Domain


f ο g (1) = f(5) = 2 Ran - Range

inputs outputs
×2 +1
f

dom f ran f =
= ran f -1 dom f -1

f -1 ÷2 -1
outputs inputs

To find the inverse function, think of the inverse operations in reverse order.
So here we have f: x→ 2x + 1

i.e. f(x) = (x ×2) +1 so that f- -1 (x) = x − 1


2

f:R→R f(x) = 3x -4 then f –1 (x) = x + 4


3

f:R→R f(x) = 1 (2 x − 1) then f –1 (x) = 3  x + 1 


3  2 

Graphs of Functions
co-domain
We can illustrate a function by using a graph.
u
We put the elements of the domain on the
horizontal axis and the elements of the co- v
domain on the vertical axis.
w
Let A = { 1, 2, 3, 4}, B = { u, v, w, x, y, z}.
The function k : A → B is defined by x
k(1) = w, k(2) = x, k(3) = x, k(4) = z.
y
The graph of this function is shown.
Note We cannot ‘join the dots’ as we only know z
the values of the function for 1,2,3 & 4 (we have
a discrete set of data points) 1 2 3 4 domain

With numerical data you can use a spreadsheet chart facility to draw the graph.
The one shown is f : A→B where A = {1,2,3,4} and B = {1,2,3,4,5,6,7,8,9,10}.

Page 52 Functions
MATH1179
Here f : x→2x or f(x) = 2x. As the domain of f only contains four members there are only four points
on the graph.
Note do not use a chart type which gives a continuous line when you have discrete data.

Functions whose domain is R, the set of real numbers


We have already looked at R, the set of real numbers, which can be represented as a continuous
number line from -∞ to +∞. With this as the domain we can have functions which may be represented
by graphs with unbroken lines and curves.

Linear Functions: The straight line

Example
f : R → R, f(x) = 2x + 1.
Using y to represent the image of x, this is usually written
y = 2x + 1, x ∈ R

To draw the graph, first find some points on the line by choosing values of x and working out the
corresponding values of y.
y

x 0 1 2 5

y 1 3 5 4

2
1

0 1 2 x

Note These points can be joined because the function is defined for all real values of x, i.e. the whole
axis, so we can calculate values like f(1.73) = 4.46

Mathematics for Computer Science Page 53


MATH1179
When drawing the graphs by hand the line segment obtained by joining the three given points can be
extended in either direction. When using a package the graph will be joined into a continuous line but
you must ensure other points are included to demonstrate the rest of the line. Wherever possible x=0
should be included to show the point where the line crosses the y axis, this is also known as the
intercept.

The graph of any function of the form f: R → R where f(x) = mx + c where m and c are given
numbers and R is the set of real numbers, is a straight line with m as the measure of the slope or
gradient and c the intercept where the line crosses the y axis.

y f(x) = 2x+1
or y = 2x+1
slope = 2/1 = 2

5
4 intercept = 1

2
1

0 1 2 x

Page 54 Functions
MATH1179
Prediction and inversion
Graphs can be used to predict values outside those from the given domain, and also to find values
from the inverse function f -1

Using the graph to


find f (4)

Using the graph to find


f –1 (-7)

Quadratic functions: the U shaped curve

y = x2 – 3x + 2

These are of the form f(x) = ax 2 + bx + c, where a, b and c are given numbers.
Note: There might not be an x term or a constant, c, but there must be an x2 term in a quadratic
function.
So, f(x) = x2 + 3, f(x) = 5x2 + 7x, f(x) = 2x2 +5x - 7.5 are all quadratic functions
But, f(x) = x3-2 and f(x) =2x +7 are not.

Mathematics for Computer Science Page 55


MATH1179
Important points are where the curve crosses the x axis as y = 0 and they can be used to solve a
quadratic equation. In the example shown, plotting f(x) as y, we see y= x2 -3x +2 and that the solutions
to x2 -3x +2 = 0 (i.e. when y = 0) are x = 1 and x = 2, these are also known as the roots of the
quadratic. The second graph shows how to obtain the solutions to x2 -3x +2 = 4.

Exponential Functions and Logarithms


Exponential functions have the form y = a x where a is a given number.
They have a characteristic ‘tail’ on one part of the graph and an accelerated ascent or descent on the
other part.

y = 2x

y = 2-x

The exponential function


y = ex
e is a special number whose value is approximately 2.71828......between 2 and 3
The graph of y = e x lies between y = 2 x and y = 3 x .

Page 56 Functions
MATH1179

Logarithms
(You will occasionally need logs in other parts of the programme)
8 = 23 We say that the logarithm of 8 to base 2 is 3 log 2 8 = 3
32 = 2 5 So logarithm of 32 to base 2 is 5 log 2 32 = 5
81 = 3 4 So logarithm of 81 to base 3 is 4 log 3 81 = 4
1 1 1 1
= 4 = 2 − 4 So logarithm of to base 2 is – 4 log 2 = -4
16 2 16 16

Logarithms to base 10 are on your calculator usually near 10x under log. Logarithms to base e,
sometimes known as natural logarithms, are on your calculator usually near ex under ln.
log b x
To find logs to any other base you need the rule: log a x =
log b a
Using a calculator,
log 10 3 = 0.4771 and log 10 2 = 0.3010. So log 2 3 = log10 3 = 0.4771 = 1.585
log10 2 0.3010

Relation between exponentials and logarithms


As we can see from the previous section and a glance at our calculator, there is a connection between
exponents and logarithms.
y = ex then logey = x (‘taking logs’ both sides) or x = lny

This enables us to draw the graph of y = ln x simply using reflection of y = ex in the line y = x.

Mathematics for Computer Science Page 57


MATH1179

Trigonometric Functions

360 2π
degrees radians

These are used in Computer Systems and Networking in the discussion of data transmission.

Radian Measure
Angles can be measured in degrees or radians. There are 360 degrees in a circle and 2π radians in a
circle.
So 2π radians = 360 degrees
∴ π radians = 180 degrees
180 π
So 1 radian = degrees = 57.3 degrees and 1 degree = radians = 0.017 radians.
π 180
You will find both these systems on your calculator so make sure you know which one you should
be using.

Example
(i) Convert to degrees 3.2 radians : π radians = 180 degrees
180
∴ 1 radian = degrees
π
180
So 3.2 radians = 3.2 × degrees = 183.3 degrees
π

(ii) Convert to degrees radians : π radians = 180 degrees
4

Page 58 Functions
MATH1179
180
∴ 1 radian = degrees
π
3π 3π 180
So radians = × degrees = 125 degrees
4 4 π
(iii) Convert to radians 30 degrees 180 degrees = π radians
π
1 degree = radians
180
π π
So 30 degrees = 30 × radians = radians = 0.52 radians
180 6

The following is a table of conversions for some common angles


Degrees 0 30 45 60 90 120 135 150 180 270 360
Radians 0 π π π π 2π 3π 5π π 3π 2π
6 4 3 2 3 4 6 2

Note:
arc length 1:2π
Suppose we have a circle of radius 1. The
circumference of the circle is 2πr in this case
this is the same as 2π and the angle at the 1
angle 1: 2π radians
centre is 2π radians. So if we have an arc of
length 1, using ratios then the angle at the 1
centre is 1 radian.

Graphs of Trigonometric Functions


r
Let A be the point (x,y) and let OA = r. t
Suppose the angle between the x-axis and the line OA is t.
y x
Then sin t = , cos t =
r r

Now suppose the angle t is greater than 90 degrees.


y x
We still define sin t = , cos t = r
r r
t
but since x is negative, cos t is also negative.
In this way we can find sin t and cos t for all values of t.

Using a calculator we find


t (radians) 0 π π π π 3π π 5π 3π 7π 2π 5π 3π
6 4 3 2 4 4 2 4 2
sin t 0 0.5 0.71 0.87 1 0.71 0 -0.71 -1 -0.71 0 1 0

Mathematics for Computer Science Page 59


MATH1179

Graph of y = sin t

-2π -π 0 π 2π 3π 4π t

Graph of y = cos t

-2π 0 π 2π 3π 4π t

The maximum value is +1 and the minimum value–1. The amplitude of the sine function is half the
difference between its maximum and minimum values. The amplitude of sin t is 1.

These functions are periodic since they repeat themselves over and over again. The period is the
length of one cycle (or time interval if t is time), that is, 2π.

Given the basic sine curve we can manipulate it to represent different functions.

y = sint
0 2π 4π
-1

Amplitude = 1 period = 2π ( 1 cycle) frequency = (number of cycles in unit t) note

Putting a scaling factor, A, in front of the function alters the amplitude i.e. the peaks and troughs go
between ±A.

Page 60 Functions
MATH1179
Adding a factor, m, in front of the variable t scales the function along the x axis, compressing the sine
curve so that it oscillates more if m>1 and elongates if m<1

y = A sin(mt)
0
2π 4π

-A

Amplitude = A period = 2π frequency = =

If we take m = 2π then the values of t, which for signals is usually time in seconds, will be in exact
units at the critical points of the curve.

y = sin(2πt)
0 1 2
t in sec

-1
Amplitude = 1 period = 1 (1 cycle) frequency = =1

(now number of cycles in 1 second so measured in Hertz)

Combining the transformations from the last two examples, the usual way of writing a signal
mathematically is to put in the form s(t) = A sin(2πft), where s(t) is the signal at time t.

A
t in sec
s = Asin2πft
0

-A

Amplitude = A period = (1 cycle) frequency = f (now number of cycles in 1 second so is

measured in Hertz)

Mathematics for Computer Science Page 61


MATH1179
Example
Sketch the graphs of s = 3sin 2πt, s = 3sin πt and s = 3sin 4πt for values of t between 0 and 2
t 0 0.25 0.5 0.75 1 1.25 1.5 1.75 2
s = 3sin 2πt 0 3 0 -3 0 3 0 -3 0

t 0 0.25 0.5 0.75 1 1.25 1.5 1.75 2


s = 3sin πt 0 3 0 -3 0

t 0 0.125 0.25 0.325 0.5 0.6251 0.75 0.825 1


s = 3sin 4πt 0 3 0 -3 0 3 0 -3 0

3
t in sec

1 2 s = 3sin2πt
0

-3
Amplitude = 3 period = 1 frequency =1

3
t in sec
s = 3sinπt
0 2 4

-3

Amplitude = 3 period = 2 frequency =

3
t in sec

1 s = 3sin4πt
0 0.5

-3
Amplitude = 3 period = 1/2 frequency =2

Page 62 Functions
MATH1179
Examples
As the signal can be written as s = A sin (2πft) where A is the amplitude and f the frequency.
So s = 6 sin (8πt) has amplitude 6 and frequency 4,
s = 4 sin (πt) has amplitude 4 and frequency 0.5.

Phase angle
Sometimes the curve is shifted along the axis by a constant amount, θ, which moves all the points
along.

The graph shows the comparison of s = sin 2πt and a second curve s = sin 2π(t +0.25)
which has been moved back along the axis by 0.25

Phase angles sin2*PI*t


sin2*PI*(t+0.25)
1.5

0.5

s 0
0 0.5 1 1.5 2 2.5 3
-0.5

-1

-1.5 t

Mathematics for Computer Science Page 63


MATH1179

7: Complex Numbers
We have already covered the solving of simple quadratic equations by factorisation and using the
formula. Plus we have also looked a number systems: integers, real, etc.

Examples
x2 +7x +10 = 0 2x2 -7x -4 = 0 2x2 +5x +1 = 0 Formula for solving a
(x+2 )(x+5) = 0 (2x+1 )(x-4) = 0 a= 2, b= 5, c=1 quadratic
either either −5± 25 − 8 𝑎𝑎𝑥𝑥 2 + 𝑏𝑏𝑏𝑏 + 𝑐𝑐 = 0
x=
x+2 = 0 2x+1 = 0 4 Therefore
x = -2 x = -1/2 − 5 ± 17 −𝑏𝑏 ± √𝑏𝑏 2 − 4𝑎𝑎𝑎𝑎
x=
4 𝑥𝑥 =
or or 2𝑎𝑎
− 5 + 4.12 = -0.22
x+5 = 0 x-4 = 0 x=
4
x = -5 x=4 or
− 5 − 4.12 =-2.28
x=
4

These values of x represent the roots of the equation 50


i.e. the points where the curve crosses the x axis. In
the first example we can see this as the values of 40
𝑥𝑥 = −5 and 𝑥𝑥 = −2 as shown in the graph.
30

In some cases it seems impossible to factorise and 20


when we use the formula we are left trying to find
the square root of a negative quantity 10

Example 0
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4
x2 + x + 1 = 0 so a = 1, b = 1, c = 1 -10

−1± 1− 4 so x =
−1± − 3 50
x=
2 2
40
Here we see b2 – 4ac (known as the discriminant)
is -3 which cannot have a real square root.
30

If we plot this function of x we can see why there 20


are no real solutions to the equation as the curve
never cuts the x axis. 10

0
It was to deal with situations like this that a new
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
extension to the number system was defined.

Page 64 Complex Numbers


MATH1179
Definition
We define the imaginary number i to be such that 𝑖𝑖 2 = −1
Now we can also find − 3 = 3i 2 = 3 i = 1.732i.

Definition
A complex number is an expression of the form 𝑧𝑧 = 𝑎𝑎 + 𝑏𝑏 𝑖𝑖 where 𝑎𝑎 and 𝑏𝑏 are real numbers and
𝑖𝑖 = √−1. The number 𝑎𝑎 is called the real part, denoted Re(z) and 𝑏𝑏 is called the imaginary part,
denoted Im(z). Note: 𝑖𝑖 2 = −1, 𝑖𝑖 is generally used for √−1 by mathematicians and 𝑗𝑗 commonly used
by engineers. For example, simplify the following:
√(−1) = 𝑖𝑖, √4 = ±2, √25 = ±5
and
√−36 = √36 × −1 = √36 × √−1 = ±6𝑖𝑖 .
Complex numbers are added, subtracted, multiplied and divided in the manner that you would think
from elementary algebra.

Adding and Subtracting


You deal with the real and imaginary parts separately.

Example

(3 + 4i) + (7 – 6i) = (3 + 7) + (4i – 6i) = 10 + (–2i) = 10 – 3i

(3 – 2i) – (–6 + i) = (3 – (–6)) + (–2i – (+i)) = (3 +6) + (–2i - i) = (9) + (–3i) = 9 – 3i

Multiplication
Here you must remember to multiply all parts of the two complex numbers together.

Example

(3 – i)(5 + 2i) = (3)(5) + (3)(2i) + (–i)(5) + (–i)(2i)

= 15 + 6i – 5i – 2i2 = 15 + i – 2(–1) = 15 + i +2 = 17 + i

Division
As we have already seen with fractions and matrices sometimes a different technique is used to deal
with division turning it into a multiplication.

When two complex numbers are divided we multiply top and bottom of the division by the complex
conjugate of the divisor. The complex conjugate of a + bi is a – bi. This has the effect of making the
denominator real.

Mathematics for Computer Science Page 65


MATH1179
Example 2 + 3i 2 + 3i 7 + 4i
= ×
7 − 4i 7 − 4i 7 + 4i

= 14 + 8i + 21i + 12i = 14 + 29i − 12 = 2 + 29i = 2 + 29 i


2

49 + 28i − 28i − 16i 2


49 − 16( −1) 65 65 65

Example 5 − 2i 5 − 2i 3 − 6i
= ×
3 + 6i 3 + 6i 3 − 6i

= 15 + −30i − 6i + 12i = 15 − 36i − 12 = 3 + 36i = 3 + 36 i = 1 + 4 i


2

9 − 18i + 18i − 36i


2
9 − 36( −1) 45 45 45 15 5

Argand Diagram
Imaginary Axis
Given a complex number 𝑧𝑧 in its Cartesian form,
𝑧𝑧 = 𝑥𝑥 + 𝑖𝑖𝑖𝑖, the Real part of z is x, and the Imaginary
y z
part of z is y. This point can be plotted on an Argand
diagram
θ is called the argument of z and is written as:
−1
θ = arg(z) = tan ( y / x)
r is called the modulus of z and is written as:
r = z = ( x2 + y 2 )
x Real Axis

6
-3 + 5i 5

4 2 + 3i 4 + 3i
3
r
2

1
0 θ
-6 -5 -4 -3 -2 -1 1 2 3 4 5 6
-1

-2

-3
4 - 2i
-5 - 3i -4

-5

-6
We can see 5 complex numbers marked here are −3 + 5𝑖𝑖, 2 + 3𝑖𝑖, 4 + 3𝑖𝑖, 4 − 2𝑖𝑖 and −5 − 3𝑖𝑖.

Page 66 Complex Numbers


MATH1179
Alternative formats for a complex number
On the Argand diagram for 4+3i we have marked its distance from the origin and the angle it makes
with the positive x axis. These are known as the modulus, r, and argument, θ , of a complex number
and can be found using Pythagoras a trigonometry.

Definition
Given a complex number 𝑧𝑧 = 𝑎𝑎 + 𝑖𝑖𝑖𝑖 the modulus, 𝑟𝑟, is given by a 2 + b2
𝑏𝑏
the tangent of the argument, 𝑡𝑡𝑡𝑡𝑡𝑡 𝜃𝜃 = 𝑎𝑎
𝑎𝑎 𝑏𝑏
𝑧𝑧 can then be rewritten using the fact that cos 𝜃𝜃 = 𝑟𝑟
and sin 𝜃𝜃 = 𝑟𝑟
Leading to
𝑧𝑧 = 𝑟𝑟 𝑐𝑐𝑐𝑐𝑐𝑐 𝜃𝜃 + 𝑟𝑟 𝑖𝑖 𝑠𝑠𝑠𝑠𝑠𝑠 𝜃𝜃 = 𝑟𝑟 (𝑐𝑐𝑐𝑐𝑐𝑐 𝜃𝜃 + 𝑖𝑖 𝑠𝑠𝑠𝑠𝑠𝑠 𝜃𝜃)
this is called the polar form of a complex number sometimes written rcisθ for short or r∠θ o
The exponential (Euler’s) Form of z is 𝑧𝑧 = 𝑟𝑟𝑒𝑒 𝑖𝑖𝑖𝑖 Use radions for 𝜃𝜃.

Example
We have marked z = 4 + 3i,
3
so r = 4 2 + 32 = 16 + 9 = 25 = 5 , tan θ = so, θ = tan-1 (0.75) = 36.9o
4
z = 5(cos36.9 + isin36.9)

Complex Conjugate
Let𝑧𝑧 = 𝑥𝑥 + 𝑖𝑖𝑖𝑖, then the conjugate of z is denoted as 𝑧𝑧̅ and is written as 𝑧𝑧̅ = 𝑥𝑥 − 𝑖𝑖𝑖𝑖

Example
If z = 3 − 2i , the conjugate of z, z is given as z = 3 + 2i y
Here r = z = (3) 2 + (−2) 2 = 13 3
x
y −2 o
θ = arg( z ) = tan ( ) = tan −1 ( ) = 326.3
−1

x 3
-2
z is sometimes written in the polar form r∠θ o
o
In polar form, we can write, 13 ∠ 326.3 [ θ o in Degrees]
2
Note: 1) zz = ( x + iy )( x − iy ) = x 2 + y 2 = r 2 = z 2) z is always positive

This can also be written using the exponential form 𝒛𝒛 = 𝒓𝒓𝒆𝒆𝒊𝒊𝒊𝒊

Example Given z = 4 + 3i , find z and derive the polar form of z.


3
z = 4 − 3i , r = z = (4) 2 + (3) 2 = 5 , θ = arg( z ) = tan −1 ( ) = 36.7 o
4
o
z in polar form is 5 ∠ 36.7

Mathematics for Computer Science Page 67


MATH1179
More Addition / Subtraction in Complex Numbers
When two or more complex numbers are added or subtracted, the Re(z) and the Im(z) are dealt with
separately.

Example z1-z2 y
If z1 = 3 + 2i and z2 = 5 − 9i , find
(i) z1 + z2 , (ii) z1 − z2 , (iii) z2 − z1 -z2

Solution
(i) z1 + z2 = (3 + 2i ) + (5 − 9i )
z1
= 3 + 5 + 2i − 9i
x
= 8 − 7i
(ii) z1 − z2 = (3 + 2i ) − (5 − 9i ) -z1
= 3 − 5 + 2i − −9i
= − 2 + 11i
(iii) z2 − z1 = (5 − 9i ) − (3 + 2i ) z1+z2
z2
= 5 − 3 − 9i − 2i
= 2 − 11i z2-z1
Now find − z2 − z1 and roughly plot on the
Argand diagram.

More Multiplication in Complex Numbers


In General, ( x + iy )( p + iq ) = x( p + iq ) + iy ( p + iq )
2
= xp + ixq + iyp + i yq = xp + ixq + iyp − yq
= ( xp − yq) + i ( xq + yp)
Let z = ( xp − yq) + i ( xq + yp) , therefore Re(z) is ( xp − yq ) and Im(z) is ( xq + yp)

Example
2
If z1 = 3 + 2i and z2 = 5 − 9i , find (i) 3z1 , (ii) z1 z2 , (iii) z1

(i) 3z1= 3(3 + 2i ) = 9 + 6i

(ii) z1 z2 = (3 + 2i )(5 − 9i ) = 3 × 5 + 2i × 5 − 3 × 9i + 18 = 33 − 17i +


2
(iii) z1 = (3 + 2i )(3 + 2i ) = 5 + 12i

Example
1) Show that (2 + 3i )(3 + 4i ) = −6 + 17i
(2 + 3i )(3 + 4i ) = 6 + 9i + 8i − 12

= − 6 + 17i

Page 68 Complex Numbers


MATH1179
More Division in Complex Numbers

Example
To find 4 − 3i the division can be written as: 4 − 3i 4 − 3i 5 + 2i
= ×
5 − 2i 5 − 2i 5 − 2i 5 + 2i
𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁
In General, if we have to do 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷
, this can be performed by:
𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝑂𝑂𝑂𝑂 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷
= ×
𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝑂𝑂𝑂𝑂 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶 𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝐷𝑛𝑛𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎

Where z1 = x1 + iy1 and z2 = x2 + iy2 then


x1 − iy1 z zz ( x − iy1 ) ( x2 + iy2 )
= 1 = 1 22 = 1 ×
x2 − iy2 z2 z2 ( x2 − iy2 ) ( x2 + iy2 )

Hence, 4 − 3i = 4 − 3i × 5 + 2i = 4 × 5 − 3i × 5 + 4 × 2i − 3 × 2i
5 − 2i 5 − 2i 5 + 2i 5 × 5 − 2i × 2i

26 7
= − i
29 29

Example
Find 7 + 2i
8 − 5i
7 + 2i 7 + 2i 8 + 5i 7 × 8 + 2i × 8 + 7 × 5i + 2i × 5i
= × =
8 − 5i 8 − 5i 8 + 5i 8 × 8 − 5i × 5i

56 + 16i + 35i − 10 46 51i


= = +
64 + 25 89 89

Quadratic Equations
Consider the following example of a quadratic equation of the form ax 2 + bx + c = 0 , and solve to
find the two roots.

Example
z2 + z + 1 = 0 , here a=1; b= 1; c=1.

− b ± b 2 − 4ac
Using z = substituting the values of a, b and c, we get
2a
−1± 1− 4 −1 3
z= = ± i
2 2 2
−1 3 −1 3
Which gives the two complex roots of the quadratic equation as z1 = + i and z2 = − i.
2 2 2 2

Mathematics for Computer Science Page 69


MATH1179
Example
−1 3
Express in Polar and Exponential Forms the complex number z = + i
2 2
1
In Polar form: r  x 2 + y 2  2
=
1
 − 1  2  3  2  2
r = z =   +    =1 z
 2   2  
 
 3  r
 y −1 
θ = tan   = tan  2
x
−1
 ( )
 = tan −1 − 3 = −60o
 − 12 
 
Since θ is in the 2nd Quadrant, θ = 120o
o
In Polar form, r∠θ , i.e. 1 ∠ 120
In Exponential form:
−1 3
z= + i
2 2
[
z = r [cos θ + i sin θ ] = 1 cos120
o
+ i sin 120o ]
120 2
converting 120o into radians, =
120o = π π
180 3
2 2
iπ iπ
Therefore the exponential form is given as z = z ei. arg( z ) = 1.e 3 = e3

Example
Sketch on an Argand diagram and express in Polar and Exponential Forms the following complex
numbers z1 = 1 + i , z2 = −2 − 4i .

[ ] [ ]
1 1
r1 = (1) + (1) r2 = (− 2 ) + (− 4 )
2 2 2 2 2 2
= 2 = 20
z1
1 −4 r1
θ1 = tan −1   = 45° θ 2 = tan −1   = 63.4°
1 −2 θ1
θ2 is in the 3rd quadrant θ2
θ2 = 180+63.4=243.4°
θ1 = π radians θ2 = 4.25 radians (2d.p.)
4
r2
In Polar form, r∠θ , z1= 2 ∠ 45o , z2= 20 ∠ 243.4o

In Exponential form, z1= 2e 4 , z2= 20e 4.25i
z2

Page 70 Complex Numbers


MATH1179

8: Pattern Matching and Unification


A brief introduction to pattern matching and unification.

Pattern matching
Given a constant term and a pattern term, where the pattern term has variables, pattern matching
involves finding a variable assignment that will make the two terms match.

Starting with terminology, terms are built from constants, variables and function applications:
- A lowercase letter represents a constant (such as an integer or a string),
- An uppercase letter represents a variable,
- 𝑓𝑓(. . . ) is a function 𝑓𝑓 with some parameters.

Example
Constant term: 𝑓𝑓(𝑎𝑎, 𝑏𝑏, 𝑔𝑔(𝑡𝑡)) Pattern term: 𝑓𝑓(𝑎𝑎, 𝑌𝑌, 𝑋𝑋)
A simple assignment, a substitution mapping variables to the assigned values, works
𝑌𝑌 = 𝑏𝑏 and 𝑋𝑋 = 𝑔𝑔(𝑡𝑡).
A variable in the pattern is allowed to match anything in the other structure.
Variables can appear multiple times in a pattern:
Constant term: 𝑓𝑓(𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓(𝑎𝑎), 𝑎𝑎, ℎ(𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓(𝑎𝑎)), 𝑥𝑥) Pattern term: 𝑓𝑓(𝑌𝑌, 𝑎𝑎, ℎ(𝑌𝑌), 𝑥𝑥)
Here the right substitution is 𝑌𝑌 = 𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓𝑓(𝑎𝑎).
If a variable occurs more than once in the pattern it must match the same thing in each case.

Further pattern matching examples, where x, y and z are variables.


1)
if [x, y] := [1, 2]
then # executes with x=1, y=2
else # does not execute
2)
if [x, y] := [1, 2, 3]
then # does not execute because the match failed
else # executes, but x and y are unassigned
The clause on the left of the assignment operator is the pattern. The pattern is part of the syntax of
the assignment and is not a data object on its own. On the right side is the subject.

Unification
In logic and computer science, unification is a process of automatically solving equations between
symbolic terms. Unification has several interesting applications, notably in logic programming and
type inference. Unification can be seen as a generalization of pattern matching.

Mathematics for Computer Science Page 71


MATH1179
Unification is bidirectional pattern matching. For instance, say Y = 1 in Prolog you are not assigning
the value of 1 to a variable Y, we are saying “to what do I need to map Y so that the left and right
sides have the same value?” Unification is both an equality predicate and a two-sided form of multiple
assignment that matches a value to a value instead of a pattern to a value.

Example
Consider a function such that 𝑓𝑓(𝑥𝑥, 𝐴𝐴) = 𝑓𝑓(𝐵𝐵, 𝑦𝑦).
What must we map to variables 𝐴𝐴 and 𝐵𝐵 so that the two sides have the same value?
Clearly {𝐴𝐴 → 𝑦𝑦, 𝐵𝐵 → 𝑥𝑥}
𝑓𝑓(𝑥𝑥, 𝐴𝐴) = 𝑓𝑓(𝑥𝑥, 𝑦𝑦), 𝑓𝑓(𝐵𝐵, 𝑦𝑦) = 𝑓𝑓(𝑥𝑥, 𝑦𝑦)
Sometimes there’s no mapping possible: 1 = 2 will never unify, nor will 𝑓𝑓(𝑋𝑋) = 𝑓𝑓(𝑋𝑋, 𝑌𝑌).

Example
Consider variables a and b
[a, b] = [1, 2] # succeeds and assigns a=1, b=2
[a, b] = [1, 2, 3] # fails
This example makes unification look a lot like pattern matching, but there are important differences.
What underlies the differences is this: unification is two-sided and works on logical variables, while
pattern matching is one-sided and works on normal programming-language variables or normal
single-assignment variables.

A logical variable is like a single-assignment variable but it can be used before it is assigned a value.
In other words, it can be put in a structure, passed to a function, returned from a function, or have a
predicate applied to it before ever being assigned. Pattern matching works on normal programming-
language variables--mutable variables, or normal single-assignment variables.

Differences between pattern matching and unification

Pattern matching Unification

Matches a syntactic pattern to a data object. Matches a data object to a data object.

mutable variables or single-assignment variables logical variables

one-sided: Only assigns to variables on the left. two-sided: Assigns in both directions.

No constraints are created. Can create equality constraints.

lexically restricted: Only assigns to variables that lexically unrestricted: Assigns to variables that do
occur syntactically in the statement. not occur syntactically in the statement.

sequential--Assignments must occur in the non-sequential--Assignments can be done in any


presented order. order.

non-idempotent--Assignments cannot generally idempotent--Assignments can be repeated without


be repeated. changing the effect.

A single pattern can match different terms with A term can only match another term with the same
different structures. structure.

Page 72 Pattern Matching and Unification

You might also like