-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRecursionPractice.java
74 lines (69 loc) · 2.43 KB
/
RecursionPractice.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class RecursionPractice {
// Compilation of functions example: f(f(f(a)))
//f(a) = 5 + a
//f(20) = 5 + 20 = 25
//f(f(f(20))) = f(f(25))
//f(25) = 5 + 25 = 30
//f(f(25)) = f(30)
//f(30) = 30 + 5 = 35
//end state is 35
// Summation example: 5 + 4 + 3 + 2 + 1
// Summation example: 3 + 2 + 1
public static int Summation(int n) {
// Base Case: We are at the end
if (n <= 0) {
return 0; // additive identity property
}
// Recursive Case: Keep going
else {
// Example using the number 3 as a start
// Step 1: 3 + Summation(2) | Recursive
// Step 2: 3 + 2 + Summation(1) | Recursive
// Step 3: 3 + 2 + 1 + Summation(0) | Recursive
// Step 4: 3 + 2 + 1 + 0 | Base
return n + Summation(n-1);
}
}
// Example Factorial: 5!
// 5 * 4 * 3 * 2 * 1
public static int Factorial(int n) {
// Base Case:
if (n <= 1) {
return 1; // multiplicative identity
}
// Recurisve Case
else {
// Example using 5 as a start
// Step 1: 5 * Factorial(4) | Recursive
// Step 2: 5 * 4 * Factorial(3) | Recursive
// Step 3: 5 * 4 * 3 * Factorial(2) | Recursive
// Step 4: 5 * 4 * 3 * 2 * Factorial(1) | Recursive
// Step 5: 5 * 4 * 3 * 2 * 1 | Base
return n * Factorial(n-1);
}
}
// Example Exponential Case: 5^3
// % * 5 * 5
public static int Exponentiation(int n, int p) {
// Base Case:
if (p <= 0) {
return 1; // multiplicative identity
}
// Recursive Case
else {
// Example using (5, 5) to start
// Step 1: 5 * Exponentiation(5, 4) | Recursive
// Step 2: 5 * 5 * Exponentiation(5, 3) | Recursive
// Step 3: 5 * 5 * 5 * Exponentiation(5, 2) | Recursive
// Step 4: 5 * 5 * 5 * 5 * Exponentiation(5, 1) | Recursive
// Step 5: 5 * 5 * 5 * 5 * 5 * Exponentiation(5, 0) | Recursive
// Step 6: 5 * 5 * 5 * 5 * 5 * 1 | Base
return n * Exponentiation(n, p-1);
}
}
public static void main(String[] args) {
System.out.println("Summation " + Summation(5));
System.out.println("Factorial " + Factorial(5));
System.out.println("Exponential " + Exponentiation(5, 5));
}
}