{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

03+-+Tail+Recursion+and+Intro+to+Testing

03+-+Tail+Recursion+and+Intro+to+Testing - Recursion...

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

View Full Document Right Arrow Icon
9/1/10 1 Tail Recursion and Intro to Testing EECS 280 Programming and Introductory Data Structures Recursion Another kind of factorial int fact_helper(int n, int result) // REQUIRES: n >= 0 // EFFECTS: returns result * n! { if (n == 0) { return result; } else { return fact_helper(n-1, result * n); } } int factorial(int num) // REQUIRES: n >= 0 // EFFECTS: returns num! { return fact_helper(num, 1); } Re-write the recursive version to use the same amount of space as is required by the iterative version (approximately). Recursion Group Exercise: Another kind of factorial This function is equivalent to the original factorial. There are two steps. First, prove the base case, and second, the inductive step. int fact_helper(int n, int result) // REQUIRES: n >= 0 // EFFECTS: returns result * n! { if (n == 0) { return result; } else { return fact_helper(n-1, result * n); } } int factorial(int num) // REQUIRES: n >= 0 // EFFECTS: returns num! { return fact_helper(num, 1); } Recursion Another kind of factorial There is an important thing to notice about fact_helper. For every call to fact_helper: n! * result == num! For the first call, this is easy to see, since: n == num result == 1 int fact_helper(int n, int result) // REQUIRES: n >= 0 // EFFECTS: returns result * n! { if (n == 0) { return result; } else { return fact_helper(n-1,result*n); } } int factorial(int num) // REQUIRES: n >= 0 // EFFECTS: returns num! { return fact_helper(num, 1); } Recursion Another kind of factorial For every call to fact_helper: n! * result == num! For the second call: n == (num - 1) result == (1*num) == num Substituting, we get: (num-1)! * num == num! This is true by inspection. You can continue unwinding if you like. int fact_helper(int n, int result) // REQUIRES: n >= 0 // EFFECTS: returns result * n! { if (n == 0) { return result; } else { return fact_helper(n-1,result*n); } } int factorial(int num) // REQUIRES: n >= 0 // EFFECTS: returns num! { return fact_helper(num, 1); } Recursion Another kind of factorial For every call to fact_helper: n! * result == num! This is called the “recursive invariant” of fact_helper and is something that is always true. Being able to write down invariants makes it much easier to write these sorts of functions.
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
9/1/10 2 Recursion Another kind of factorial So what's the big deal?
Image of page 2
Image of page 3
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