@@ -34,6 +34,7 @@ public static long FastFib(int N, HashMap<Integer, Long> cache) {
34
34
}
35
35
}
36
36
37
+
37
38
public class PracticeAlgorithms {
38
39
public static boolean isBetweenZeroAndOne (double x , double y ) {
39
40
return (0 < x && x < 1 ) && (0 < y && y < 1 );
@@ -112,7 +113,7 @@ public static int[][] matrixTransposition(int[][] theMatrix) {
112
113
113
114
114
115
/**
115
- * recursive method that returns the greatest integer not greater than log(N)
116
+ * static recursive method that returns the greatest integer not greater than log(N)
116
117
* to base two
117
118
* */
118
119
public static int lg (int N ) {
@@ -121,6 +122,15 @@ public static int lg(int N) {
121
122
return 1 + lg (N / 2 );
122
123
}
123
124
125
+ /**
126
+ * static recursive method to compute the factorial of an integer "N"
127
+ * */
128
+ public static long fact (int N ) {
129
+ if (N == 1 ) return 1 ;
130
+
131
+ return N * fact (N - 1 );
132
+ }
133
+
124
134
125
135
public static void main (String [] args ) {
126
136
Random generator = new Random ();
@@ -165,17 +175,19 @@ public static void main(String[] args) {
165
175
166
176
// loop utilizing dynamic programming and memoization
167
177
System .out .println ("- Dynamic Programming and Memoization" );
168
- for (int i = 0 ; i <= 80 ; i ++) {
178
+ for (int i = 0 ; i <= 40 ; i ++) {
169
179
System .out .println (Fibonacci .FastFib (i , cache ));
170
180
}
171
181
System .out .println ();
172
182
173
- // loop utilizing the recursive fib method
174
- System .out .println ("- Recursion" );
175
- for (int i = 0 ; i <= 80 ; i ++) {
176
- System .out .println (Fibonacci .Fib (i ));
177
- }
178
- System .out .println ();
183
+ // loop utilizing the recursive fib method
184
+ System .out .println ("- Recursion" );
185
+ for (int i = 0 ; i <= 40 ; i ++) {
186
+ System .out .println (Fibonacci .Fib (i ));
187
+ }
188
+ System .out .println ();
179
189
190
+ System .out .printf ("31!: %d\n " , fact (31 )); // N > 31 causes overflow
191
+ System .out .println ();
180
192
}
181
193
}
0 commit comments