-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathRecursionLecture.java
176 lines (148 loc) · 4.32 KB
/
RecursionLecture.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
public class RecursionLecture {
static int num=2;
public static void recursion(){
if(num>0){
num--;
recursion();
}
System.out.println("100");
}
// public static void print5(){
// System.out.println(5);
// print4();
// }
// public static void print4(){
// System.out.println(4);
// print3();
// }
// public static void print3(){
// System.out.println(3);
// print2();
// }
// public static void print2(){
// System.out.println(2);
// print1();
// }
// public static void print1(){
// System.out.println(1);
// }
public static void main(String[] args) {
//recursion();
//print5();
}
public static class Practice{
static void myRFun1(int num){
if(num== 0) return;
System.out.println(num);
myRFun1(num-1);
}
static void myRFun2(int num){
if(num== 0) return;
myRFun2(num-1);
System.out.println(num);
myRFun2(num-1);
}
static void myRFun3(int num){
if(num== 0) return;
System.out.println(num);
myRFun3(num-1);
System.out.println(num);
}
public static void main(String[] args) {
myRFun3(3);
}
}
public static class DirectRecursion {
public static void countdown(int n) {
if (n == 0) {
System.out.println("Blastoff!");
} else {
System.out.println(n);
countdown(n - 1);
}
}
public static void main(String[] args) {
countdown(5);
}
}
public static class IndirectRecursion {
public static void fun1() {
System.out.println("fun1");
fun2();
System.out.println("After fun2");
}
public static void fun2() {
System.out.println("fun2");
// fun1();
// System.out.println("After fun1");
}
public static void main(String[] args) {
fun1();
}
}
public static class TreeRecursion {
public static int nthFibonacci(int n) {
if (n <= 1) {
return n;
} else {
return nthFibonacci(n - 1) + nthFibonacci(n - 2);
}
}
public static void main(String[] args) {
int result = nthFibonacci(4);
System.out.println(result);
}
}
public static class NestedRecursion {
public static int nestedSum(int n) {
if (n == 0) {
return 0;
} else {
return n + nestedSum(nestedSum(n - 1));
}
}
public static void main(String[] args) {
int result = nestedSum(2);
System.out.println(result);
}
}
public static class MutualRecursion {
public static boolean isEven(int n) {
if (n == 0) {
return true;
} else {
return isOdd(n - 1);
}
}
public static boolean isOdd(int n) {
if (n == 0) {
return false;
} else {
return isEven(n - 1);
}
}
public static void main(String[] args) {
System.out.println(isEven(6));
}
//ODD EVEN Direct Solution
public static class OddEven {
public static boolean isEven(int n) {
if (n == 0) {
return true; // Base case: 0 is an even number
} else if (n == 1) {
return false; // Base case: 1 is an odd number
} else {
return isEven(n - 2); // Recursive call with n-2
}
}
public static void main(String[] args) {
int number = 6;
if (isEven(number)) {
System.out.println(number + " is an even number.");
} else {
System.out.println(number + " is an odd number.");
}
}
}
}
}