-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib.java
More file actions
40 lines (36 loc) · 949 Bytes
/
Copy pathfib.java
File metadata and controls
40 lines (36 loc) · 949 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
33
34
35
36
37
38
39
40
public class fib {
static long[] fibResults;
public static void main(String[] args)
{
int n = 50;
//System.out.println(fibRecursive(n));
fibResults = new long[n+1];
fibResults[0] = 1;
fibResults[1] = 1;
System.out.println(fibMemoization(n));
}
//I think the O(n) for fibRecursive is 2^n, since every call to fibRecursive results in two more calls to it, and it starts with n
private static int fibRecursive(int n)
{
if (n == 1 || n == 0)
{
return 1;
}
else
{
return fibRecursive(n-1) + fibRecursive(n-2);
}
}
private static long fibMemoization(int n)
{
if (fibResults[n] != 0)
{
return fibResults[n];
}
else
{
fibResults[n] = fibMemoization(n-1) + fibMemoization(n-2);
return fibResults[n];
}
}
}