-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib.java
More file actions
35 lines (30 loc) · 676 Bytes
/
fib.java
File metadata and controls
35 lines (30 loc) · 676 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
import java.util.*;
public class fib{
public static void main(String args[]){
int N = 10;
System.out.println(Fib(N));
}
public static int Fib(int N){
if(N < 2) return N;
ArrayList<Integer> result = new ArrayList<>();
result.add(0);
result.add(1);
int n = N-result.size()+1;
for(int i = 0;i<n;i++){
result.add(result.get(result.size()-1)+result.get(result.size()-2));
}
return result.get(result.size()-1);
}
public static int Fib2(int N){
if(N <= 1)
return N;
int a = 0, b = 1;
while(N-- > 1)
{
int sum = a + b;
a = b;
b = sum;
}
return b;
}
}