Download presentation
Presentation is loading. Please wait.
1
Recursion
2
Factorial! 6! = 6*5*4*3*2*1
3
Factorial! 75! = ???
4
Factorial! 75! = 75*74!
5
Factorial! 75! = 75*74! Defined in terms of itself
6
Factorial! 75! = 75*74! To solve this problem, we need to figure out 74!
7
Factorial! 74! = 74*73! 75! = 75*74! To solve this problem, we need to figure out 73!
8
73! = 73*72! 74! = 74*73! 75! = 75*74! When would it stop? Factorial!
To solve this problem, we need to figure out 72! When would it stop?
9
Factorial! In general… n! = n*(n-1)!
10
Recursion When a method calls itself Requirements Easy to identify
Going to create “clones” of the function Usually, the clone has a smaller problem to work Requirements Must have the recursive call Must have a terminating condition Must make progress towards terminating
11
Recursion A recursive method can only solve the simplest problem – called the base case. Usually divides the problem into two pieces: The part it can immediately solve The remainder of the problem
12
Recursive Technique Design
First: Determine the base case, i.e. the stopping point for the recursion. It should normally be the simplest case. Second: What is the case that is just one step above it? Can it be generalized enough to fit? 4/26/2018
13
Recursive Factorial Calculations
A recursive declaration of the factorial method is arrived at by observing the following relationship: n! = n . (n – 1)! What is the simplest case/ terminating state? you could use either 0! =1 or 1!=1 so … 4/26/2018
14
base case / stopping state
public int factorial(int n) { if (n==1) return 1; … 4/26/2018
15
What if n == 2? 2! = 2 *1! which leads to the rest of the code:
return n * factorial(n-1); 4/26/2018
16
A recursive factorial method
public int factorial(int n) { if(n==1) return 1; return n* factorial(n-1); } Let’s trace it! 4/26/2018
17
static int factorial (int n) { PRINT(n);
if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); }
18
static int factorial (int n) { PRINT(n);
if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } function stack
19
static int factorial (int n) { PRINT(n);
if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } main f = factorial(4)
20
n == 4 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(4) main f = factorial(4)
21
n == 4 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(4) main f = factorial(4)
22
Output 4 n == 4 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(4) main f = factorial(4)
23
Output 4 n == 4 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(4) main f = factorial(4)
24
Output 4 n == 4 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(4) main f = factorial(4)
25
Output 4 n == 4 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(4) result = 4 * factorial(3) main f = factorial(4)
26
Pending Work! Output 4 n == 4 static int factorial (int n) { PRINT(n);
if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } Pending Work! fact(4) result = 4 * factorial(3) main f = factorial(4)
27
Output 4 n == 3 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(3) fact(4) result = 4 * factorial(3) main f = factorial(4)
28
Output 4 3 n == 3 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(3) fact(4) result = 4 * factorial(3) main f = factorial(4)
29
Output 4 3 n == 3 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(3) fact(4) result = 4 * factorial(3) main f = factorial(4)
30
Output 4 3 n == 3 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
31
Pending Work! Output 4 3 n == 3
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } Pending Work! fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
32
Output 4 3 n == 2 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(2) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
33
Output 4 3 2 n == 2 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(2) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
34
Output 4 3 2 n == 2 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(2) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
35
Output 4 3 2 n == 2 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(2) result = 2 * factorial(1) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
36
Pending Work! Output 4 3 2 n == 2
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } Pending Work! fact(2) result = 2 * factorial(1) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
37
Output 4 3 2 n == 1 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(1) fact(2) result = 2 * factorial(1) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
38
Output 4 3 2 1 n == 1 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(1) fact(2) result = 2 * factorial(1) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
39
Output 4 3 2 1 n == 1 static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(1) fact(2) result = 2 * factorial(1) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
40
We “terminated” Output 4 3 2 1 n == 1
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(1) return 1 fact(2) result = 2 * factorial(1) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
41
Collapse the stack… Output 4 3 2 1
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(1) return 1 fact(2) result = 2 * factorial(1) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
42
Collapse the stack… Output 4 3 2 1
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(1) return 1 fact(2) result = 2 * factorial(1) fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
43
Collapse the stack… Output 4 3 2 1 n == 2
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(2) result = 2 * 1 fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
44
Collapse the stack… Output 4 3 2 1 n == 2
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(2) result = 2 * 1 = 2 fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
45
Collapse the stack… Output 4 3 2 1 n == 2
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(2) result = 2 * 1 = 2 fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
46
Collapse the stack… Output 4 3 2 1 n == 2
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(2) result = 2 * 1 = 2 fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
47
Collapse the stack… Output 4 3 2 1 n == 2
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(2) result = 2 * 1 = 2 fact(3) result = 3 * factorial(2) fact(4) result = 4 * factorial(3) main f = factorial(4)
48
Collapse the stack… Output 4 3 2 1 n == 3
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(3) result = 3 * 2 = 6 fact(4) result = 4 * factorial(3) main f = factorial(4)
49
Collapse the stack… Output 4 3 2 1 n == 3
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(3) result = 3 * 2 = 6 fact(4) result = 4 * factorial(3) main f = factorial(4)
50
Collapse the stack… Output 4 3 2 1 n == 3
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(3) result = 3 * 2 = 6 fact(4) result = 4 * factorial(3) main f = factorial(4)
51
Collapse the stack… Output 4 3 2 1 n == 3
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(3) result = 3 * 2 = 6 fact(4) result = 4 * factorial(3) main f = factorial(4)
52
Collapse the stack… Output 4 3 2 1 n == 4
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(4) result = 4 * 6 = 24 main f = factorial(4)
53
Collapse the stack… Output 4 3 2 1 n == 4
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(4) result = 4 * 6 = 24 main f = factorial(4)
54
Collapse the stack… Output 4 3 2 1 n == 4
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(4) result = 4 * 6 = 24 main f = factorial(4)
55
Collapse the stack… Output 4 3 2 1 n == 4
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } fact(4) result = 4 * 6 = 24 main f = factorial(4)
56
Collapse the stack… Output 4 3 2 1 n == 4
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } main f = 24
57
Collapse the stack… Output 4 3 2 1 24 n == 4
static int factorial (int n) { PRINT(n); if (n == 1) { return 1; } else { int result = n * factorial(n-1); PRINT(n); return result; } } { int f = factorial(4); PRINT(f); } main f = 24
58
Common Programming Error
Either omitting the base case or writing the recursion step incorrectly so that it does not converge on the base case will cause infinite recursion, eventually exhausting memory. This error is analogous to the problem of an infinite loop in an iterative (non-recursive) solution. 4/26/2018
59
Stack Overflow Avoid stack overflow. Stack overflow occurs when you recurse too many times and run out of memory. Often, it is more efficient to perform calculations via iteration (looping), but it can also be easier to express an algorithm via recursion. Recursion is especially useful for non-linear situations 4/26/2018
60
Challenge Write the recursive process for the following: x^y
You may assume that x and y are both positive integers 4/26/2018
61
x^y solution public long power(int x, int y) { if ( y ==1) return x; return x*power(x, y-1); } 4/26/2018
62
x^y solution with a Java Program
4/26/2018
63
x^y solution with a C# Program
4/26/2018
64
What is generated by PrintNumbers(30)?
public void PrintNumbers(int n) { if (n > 0) Console.Write(n + " "); PrintNumbers(n - 1); } public void PrintNumbers(int n) { if (n > 0) System.out.println(n + " "); PrintNumbers(n - 1); } 4/26/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.