1 / 8

Recursion

Recursion. Overview. Definition of Recursion Where recursion is used Recursion in Factorial (!n). Definition of Recursion. Is CS, recursion is when a function calls itself Nearly anytime a number is repeatedly changed with in a loop, recursion could be used. Where recursion is used.

vivek
Download Presentation

Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion

  2. Overview • Definition of Recursion • Where recursion is used • Recursion in Factorial (!n)

  3. Definition of Recursion • Is CS, recursion is when a function calls itself • Nearly anytime a number is repeatedly changed with in a loop, recursion could be used

  4. Where recursion is used • Factorial:  f(n) = f(n − 1) *f(n − 2) * etc... • Fibonacci numbers: f(n) = f(n − 1) + f(n − 2) • Catalan numbers: C0 = 1, Cn + 1 = (4n + 2)Cn / (n + 2) • Computing compound interest • The Tower of Hanoi

  5. Recursion in Factorial (!n) • Example

  6. However • Writing a recursive factorial function is a mistake because the recursive solution's memory usage is O(N) instead of O(1). • It's inappropriate when the iterative solution is better. • BUT it makes for a simple example

  7. What to Remember • Recursion means to 'recursively' call the same function • It can make things look simple at the cost of memory

  8. References • http://www.danzig.us/java_class/recursion.html • http://en.wikipedia.org/wiki/Recursion

More Related