Download presentation
Presentation is loading. Please wait.
1
R ECURSION
2
O VERVIEW Definition of Recursion Where recursion is used Recursion in Factorial (!n)
3
D EFINITION OF R ECURSION 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
W HERE 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: C 0 = 1, C n + 1 = (4 n + 2) C n / ( n + 2) Computing compound interest The Tower of Hanoi
5
R ECURSION IN F ACTORIAL (! N ) Example
6
H OWEVER 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
W HAT TO R EMEMBER Recursion means to 'recursively' call the same function It can make things look simple at the cost of memory
8
R EFERENCES http://www.danzig.us/java_class/recursion.html http://en.wikipedia.org/wiki/Recursion
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.