{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08 Recursion

08 Recursion - 8 Recursion Motivation Recursion is one of...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Recursion - 1 8 - Recursion Motivation Recursion is one of the most powerful ways to solve problems with computers. It is also usually the simplest way to write programs. But it can be tricky to understand at first. This lecture will explain recursion and help you to think recursively. You will need to practice thinking and programming this way. It will take some time, but you will find that it is worth the effort. Our mygcd.m program was an example of a recursive program. Mathematical induction again Let’s start with a simple expression: = = N k k N 1 2 ) 1 2 ( This says that you can get the square of a number N by adding up a sequence of consecutive odd numbers up to 2N–1 . In other words: 3 2 =9=1+3+5 6 2 =36=1+3+5+7+9+11 Let’s prove this by induction. Our inductive hypothesis is that 2 1 1 2 ) ( N k N S N k = = = Base case N =1, S ( N ) has just one term, which is one. So S(1)=1 which equals N 2 so we are OK. Inductive step We assume the hypothesis holds for N , that is, we assume that = = + + + + = = N k N N k N S 1 2 ) 1 2 ( ... 5 3 1 ) 1 2 ( ) ( Now we try to prove the main hypothesis, that S(N+1)=(N+1) 2 . Let’s write it down again:
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Recursion - 2 + = = + 1 1 ) 1 2 ( ) 1 ( N k k N S The sum is the hard part. But we have a sum that looks a lot like it, namely the sum S ( N ). In fact the only difference is that S ( N+1 ) has one extra term over S(N), which is 2N+1 . In other words ) 1 2 ( ) ( ) 1 ( + + = + N N S N S This brings us closer to a proof because we know what S ( N ) is, or at least we assume we know that it is, ( N –1) 2 by our inductive hypothesis. Let’s write it now S ( N+1 ) = ( N ) 2 + (2 N +1) which can be expanded into S ( N+1 ) = N 2 +2N+1 = (N+1) 2 Thus we find that S ( N+1 ) = ( N+1) 2 , which is exactly what we wanted to prove. Turning proofs into programs Recursion is a programming technique that turns mathematical induction into programs. We can write a program to compute S ( N ) using recursion. During the proof above, there was a step in which we essentially used the fact that: S(N) = S(N-1) + (2N – 1) This line is called a recursive definition of or the recursive relation for S ( N ). Not only do most proofs by induction include a recursive relation, but that line allows you to easily program the expression, if we also have a base case. In this case, the base case is given by: S(1) = 1 We can now write a program for S ( N ). In MATLAB, the program looks like this: %s.m %recursively calculates s(n)=n^2 %using the relationship s(n)=s(n-1)+(2n-1) %E77 reader %07-01 D# function [tot] = s(n) if (n==1) tot = 1; % base case else
Image of page 2
Recursion - 3 tot = s(n-1) + 2*n -1; % recursive relationship end The program first tests the base case to see if n = 1 . If so, it returns tot =1 . Otherwise, it uses the recursive definition of S ( N ) to compute tot , which gets returned by the function.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern