-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
43 lines (32 loc) · 735 Bytes
/
Fibonacci.java
File metadata and controls
43 lines (32 loc) · 735 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
41
42
43
package com.company;
public class Fibonacci {
public int getN(int n){
if (n<2) return n;
int n1 = 0;
int n2 = 1;
int holder;
for (int i=2; i<=n; i++){
holder = n1;
n1 = n2;
n2 += holder;
}
return n2;
}
public int [] getSequence(int n){
int [] seq = new int[n+1];
seq[0]=0;
seq[1]=1;
for (int i=2; i<=n; i++){
seq[i]=seq[i-1]+seq[i-2];
}
return seq;
}
public int getNrec(int n){
if (n<2) return n;
return get(0,1,n);
}
private int get(int n1, int n2, int n){
if (n<2) return n2;
return get(n2,n1+n2,n-1);
}
}