Introduction to Recursion Introduction to Recursion In order

  • Slides: 23
Download presentation
Introduction to Recursion

Introduction to Recursion

Introduction to Recursion In order to understand recursion, we’ll need to take a brief

Introduction to Recursion In order to understand recursion, we’ll need to take a brief look at sequences. A sequence is a set of numbers that have a specific order.

Introduction to Recursion A very simple example of a sequence is: {1, 2, 3,

Introduction to Recursion A very simple example of a sequence is: {1, 2, 3, 4, 5, …} This sequence happens to be infinite in size, but that is not true of all sequences.

Introduction to Recursion It’s very important to understand the notation used in sequences. Perhaps

Introduction to Recursion It’s very important to understand the notation used in sequences. Perhaps most importantly, you’ll need to know the difference between a term and a term number.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} The

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} The terms are the numbers themselves. The term number indicates the position of a term in the sequence.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} So

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} So the first term is 4. The second term is 5, etc.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} A

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} A convention is to use a capital letter to represent the term, such as T. A subscript indicates the term number.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} In

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} In our case, T 1 = 4, T 2 = 5, T 3 = 6, etc.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} A

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} A recursion uses the same process to work from one term to the next in a sequence. In our example, the process is to add 1.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} 4

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} 4 + 1 = 5, 5 + 1 = 6. 6 + 1 = 7, etc.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} We

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} We want a notation to show this relationship. We use this notation to show that the value of one term depends on the value of the previous term.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} For

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} For example, you need to know the first term in order to determine the second term. You need to know the second term to determine third term, etc.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} Using

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} Using our notation to show the relationship between a term and its preceding term: T 2 = T 1 + 1 T 3 = T 2 + 1 etc.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} We

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} We want a generic form to show this “add 1” relationship for the sequence. In general, the term in the nth position, Tn, depends on the term in the previous position, Tn-1.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} In

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} In short, Tn = Tn-1 + 1

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} In

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} In short, Tn = Tn-1 + 1 If you want to determine the 10 th term, for example, take the 9 th term and add 1.

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} Notice

Introduction to Recursion Consider another simple example: {4, 5, 6, 7, 8, …} Notice that if we know this recursive rule and the starting point of the sequence (the first term), we can determine the entire sequence.

Introduction to Recursion Example 2 {2, 5, 8, 11, …} What is the recursive

Introduction to Recursion Example 2 {2, 5, 8, 11, …} What is the recursive rule?

Introduction to Recursion Example 2 {2, 5, 8, 11, …} We get from one

Introduction to Recursion Example 2 {2, 5, 8, 11, …} We get from one term to the next by adding 3, so the rule is Tn = Tn-1 + 3

Introduction to Recursion Example 3 {5, 1, -3, -7, …} We get from one

Introduction to Recursion Example 3 {5, 1, -3, -7, …} We get from one term to the next by subtracting 4, so the rule is Tn = Tn-1 - 4

Introduction to Recursion Example 4 {2, 4, 8, 16, …} In this case we

Introduction to Recursion Example 4 {2, 4, 8, 16, …} In this case we get from one term to the next by multiplying by 2, so the rule is Tn = 2 Tn-1

Introduction to Recursion Example 4 {8, 4, 2, 1, …} In this case we

Introduction to Recursion Example 4 {8, 4, 2, 1, …} In this case we get from one term to the next by dividing by 2. This is the same as multiplying by 1/2 so the rule is Tn = (1/2)Tn-1

Introduction to Recursion Example 4 {2, 4, 16, 256, …} In this case we

Introduction to Recursion Example 4 {2, 4, 16, 256, …} In this case we get from one term to the next by squaring the previous term so the rule is Tn = (Tn-1)2