80 likes | 376 Views
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.
E N D
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 • 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
Recursion in Factorial (!n) • Example
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
What to Remember • Recursion means to 'recursively' call the same function • It can make things look simple at the cost of memory
References • http://www.danzig.us/java_class/recursion.html • http://en.wikipedia.org/wiki/Recursion