Sequences
• Last lecture: We introduced sequences, which
are infinite lists of nos in a particular order
• e.g. 1, 3, 5, 7, 9, 11, … & 5, 10, 15, 20, 25, 30, …
• The nth term of a sequence is denoted by t(n) –
e.g. t(1) is the 1st term & t(7) is the 7th term
• Sometimes we can write down an explicit rule for
the nth term of a sequence
• e.g. For 1, 3, 5, 7, 9, 11, …, it is t(n) = 2n – 1
• Such a formula provides a non-recursive
definition of the sequence
1
1
Discrete Mathematics 2002 Lecture 23, 12-September-2002
Recursive Definitions
• The mathematical notation for ‘the first term is 1,
and each subsequent term is obtained by adding 2
to the previous term’ is:
t(1) = 1
t(n) = t(n – 1) + 2 (n > 1)
• These 2 equations combine to give a recursive
definition of the sequence
• It is recursive because the formula for t(n)
contains the previous term t(n – 1) – so the
formula calls itself
• t(1) = 1 gives a base (starting pt) for the recursion
5
2
Discrete Mathematics 2002 Lecture 23, 12-September-2002
More Examples
• Example: Find recursive and non-recursive
defns for the sequence 3, 7, 11, 15, 19, 23, …
• Note that in the above example, the non-
recursive formula is more difficult to find than
the recursive formula, but is easier to use once it
has been found – this is fairly typical
• Example: Find the first 4 terms of the recursively
defined sequence
t(1) = 6
t(n) = 2n + 2t(n – 1) (n > 1)
• Answer: 6, 16, 40, 96 7
Example of an Algorithm
• Example: Write an algorithm to output the first
m terms of the sequence in the previous example
• Solution: The following algorithm is suitable:
1. Input m
2. t ← 6
3. Output t
4. For n = 2 to m do
4.1. t ← 2n + 2t
4.2. Output t
8
Iterative Algorithms
• The algorithm on the previous slide is based
on a recursive definition of the sequence,
but is not a recursive algorithm.
• Instead, it is an iterative algorithm
• The word ‘iterate’ means ‘to do something
repeatedly’, as in a For-do loop
• We will look at recursive algorithms later
3
Discrete Mathematics 2002 Lecture 23, 12-September-2002
4
Discrete Mathematics 2002 Lecture 23, 12-September-2002