Anda di halaman 1dari 5

Discrete Mathematics 2002 Lecture 23, 12-September-2002

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

Algorithms to Generate Sequences


• A non-recursive defn can be used in an algorithm
to obtain the first m terms of a sequence
• An algorithm is a list of steps or instructions for
performing a task
• The algorithm on the following slide generates
the first m terms of 1, 3, 5, 7, 9, 11, …
• Note that the algorithm is written in pseudocode,
a form of structured English which can be easily
converted to code in the chosen programming
language
2

Algorithm for First m Terms of


1, 3, 5, 7, 9, 11, …
1. Input m
2. For n = 1 to m do (n is the index variable)
2.1. t ← 2n – 1 (← means assignment)
2.2. Output t
• This uses the non-recursive defn t(n) = 2n – 1
• However, the sequence can be generated in a
different way, based on the fact that the first term
is 1, and each subsequent term is obtained by
adding 2 to the previous term 3

1
Discrete Mathematics 2002 Lecture 23, 12-September-2002

A Different Algorithm for First m


Terms of 1, 3, 5, 7, 9, 11, …
1. Input m
2. t←1
3. Output t
4. For n = 2 to m do
4.1. t ← t + 2
4.2. Output t
• A trace of this algorithm for m = 5 shows that
it does, indeed, output the first 5 terms of the
sequence
4

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

Recursive & Non-Recursive Defns


• We thus have 2 ways of describing the sequence
1, 3, 5, 7, 9, 11, …
• 1: Recursive Definition
t(1) = 1
t(n) = t(n – 1) + 2 (n > 1)
• 2: Non-Recursive Definition
t(n) = 2n – 1
• Either definition can be used to calculate terms of
the sequence
• Example: Calculate the 5th term using both defns
6

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

An 800-year old Problem


• Example: The following
problem was posed by the Italian
mathematician Leonardo Pisano
(‘Leonardo of Pisa’) in 1202
• A man put a pair of newborn rabbits in a place
surrounded on all sides by a wall. How many
pairs of rabbits will be present at later times if
it is supposed that every month each pair begets
a new pair which from the second month on
becomes productive? 10

Solution to the Problem


No. of Non- No. of Total
Time Productive Productive No. of
Pairs Pairs Pairs
Initial 1 0 1
After 1 month 0 1 1
After 2 months 1 1 2
After 3 months 1 2 3
After 4 months 2 3 5
After 5 months 3 5 8
After 6 months 5 8 13
After 7 months 8 13 21
After 8 months 13 21 34
After 9 months 21 34 55
11

More on the Rabbit Problem


• The resulting sequence is
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, …
• This is known as the Fibonacci sequence
(‘Fibonacci’ was another name for Leonardo)
• It is obtained from the pattern ‘1st term is 1,
2nd term is 1, then each term is the sum of the
two previous terms’
• This leads to the recursive definition
t(1) = 1, t(2) = 1,
t(n) = t(n – 2) + t(n – 1) (n > 2)
12

4
Discrete Mathematics 2002 Lecture 23, 12-September-2002

More on the Fibonacci Sequence


• It’s not so straightforward to obtain a non-
recursive defn of the Fibonacci sequence
• In fact, the formula is
n n
1 1+ 5  1− 5 
t(n) =   −  
5  2   2  
• This goes to show that non-recursive
formulas can sometimes be quite difficult to
calculate and rather complicated in nature!
13

Anda mungkin juga menyukai