-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgoTest.java
More file actions
33 lines (25 loc) · 914 Bytes
/
AlgoTest.java
File metadata and controls
33 lines (25 loc) · 914 Bytes
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
/**
* Directions:
* 1) Run this as is and see how long it takes.
* 2) Make changes to the body of "f" to make it faster without changing the output of the program.
*/
public class AlgoTest {
public static void main(String[] args) {
long start = System.currentTimeMillis();
for(int num = 1; num < 20; num++) {
System.out.println("f(" + num + ", " + num + ") = " + f(num, num));
System.out.println("f(" + num + ", " + (num+1) + ") = " + f(num, num + 1));
}
long end = System.currentTimeMillis();
System.out.println("Test took " + ((float)(end - start) / 1000) + " seconds.");
}
private static int f(int a, int b) {
if (a == 1 && b == 1)
return 1;
if(a == 1)
return 1 + f(a, b-1);
if(b == 1)
return 1 + f(a-1, b);
return (f(a-1, b) + f(a, b-1));
}
}