Discrete Maths 2003 Lecture 23 3 Slides Pp

Embed Size (px)

Citation preview

  • 8/13/2019 Discrete Maths 2003 Lecture 23 3 Slides Pp

    1/5

    Lecture 23, 11-September-200Discrete Mathematics 2003

    1

    Sequences

    Last lecture: We introduced sequences, whichare 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 forthe 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-recursivedefinition of the sequence

    2

    Algorithms to Generate Sequences

    A non-recursive defn can be used in an algorithmto obtain the first m terms of a sequence

    An algorithm is a list of steps or instructions forperforming a task

    The algorithm on the following slide generatesthe first m terms of 1, 3, 5, 7, 9, 11,

    Note that the algorithm is written inpseudocode,a form of structured English which can be easilyconverted to code in the chosen programming

    language

    3

    Algorithm for First m Terms of

    1, 3, 5, 7, 9, 11,

    1. Input m

    2. Forn = 1 tom do (n is the index variable)

    2.1. t 2n 1 (means assignment) 2.2. Output t

    This uses the non-recursive defnt(n) = 2n 1

    However, the sequence can be generated in adifferentway, based on the fact that the first termis 1, and each subsequent term is obtained byadding 2 to the previous term

  • 8/13/2019 Discrete Maths 2003 Lecture 23 3 Slides Pp

    2/5

    Lecture 23, 11-September-200Discrete Mathematics 2003

    4

    A Different Algorithm for First m

    Terms of 1, 3, 5, 7, 9, 11,

    1. Input m

    2. t 1

    3. Output t

    4. Forn = 2 tom do

    4.1. t t+ 2

    4.2. Output t

    A trace of this algorithm for m = 5 shows thatit does, indeed, output the first 5 terms of thesequence

    5

    Recursive Definitions

    The mathematical notation for the first term is 1,and each subsequent term is obtained by adding 2to the previous term is:

    t(1) = 1

    t(n) = t(n 1) + 2 (n > 1)

    These 2 equations combine to give a recursivedefinition of the sequence

    It is recursive because the formula for t(n)contains the previous term t(n 1) so theformula calls itself

    t(1) = 1 gives a base (starting pt) for the recursion

    6

    Recursive & Non-Recursive Defns

    We thus have 2 ways of describing the sequence1, 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 ofthe sequence

    Example: Calculate the 5th term using both defns

  • 8/13/2019 Discrete Maths 2003 Lecture 23 3 Slides Pp

    3/5

    Lecture 23, 11-September-200Discrete Mathematics 2003

    7

    More Examples

    Example: Find recursive and non-recursivedefns for the sequence 3, 7, 11, 15, 19, 23,

    Note that in the above example, the non-recursive formula is more difficult to find thanthe recursive formula, but is easier to use once ithas been found this is fairly typical

    Example: Find the first 4 terms of the recursivelydefined sequence

    t(1) = 6

    t(n) = 2n + 2t(n 1) (n > 1)

    Answer: 6, 16, 40, 96

    8

    Example of an Algorithm

    Example: Write an algorithm to output the firstm terms of the sequence in the previous example

    Solution: The following algorithm is suitable:

    1. Input m

    2. t 6

    3. Output t

    4. Forn = 2 tom do

    4.1. t 2n + 2t

    4.2. Output t

    9

    Iterative Algorithms

    The algorithm on the previous slide is basedon a recursive definition of the sequence,

    but is nota recursive algorithm. Instead, it is an iterative algorithm

    The word iterate means to do somethingrepeatedly, as in a For-do loop

    We will look at recursive algorithms later

  • 8/13/2019 Discrete Maths 2003 Lecture 23 3 Slides Pp

    4/5

    Lecture 23, 11-September-200Discrete Mathematics 2003

    10

    An 800-year old Problem

    Example: The following

    problem was posed by the Italianmathematician 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?

    11

    Solution to the Problem

    TimeNo. of Non-Productive

    Pairs

    No. ofProductive

    Pairs

    TotalNo. of

    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

    12

    More on the Rabbit Problem

    The resulting sequence is1, 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 thetwo previous terms

    This leads to the recursive definition

    t(1) = 1, t(2) = 1,

    t(n) = t(n 2) + t(n 1) (n > 2)

  • 8/13/2019 Discrete Maths 2003 Lecture 23 3 Slides Pp

    5/5

    Lecture 23, 11-September-200Discrete Mathematics 2003

    13

    More on the Fibonacci Sequence

    Its not so straightforward to obtain a non-

    recursive defn of the Fibonacci sequence

    In fact, the formula is

    +=

    nn

    nt2

    51

    2

    51

    5

    1)(

    This goes to show that non-recursive

    formulas can sometimes be quite difficult to

    calculate and rather complicated in nature!