-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibo.java
More file actions
41 lines (36 loc) · 883 Bytes
/
Copy pathFibo.java
File metadata and controls
41 lines (36 loc) · 883 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
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;
/**
*
* @author yt646712
*/
public class Fibo {
public static int FibonacciRecursive(int n){
if (n > 2){
return (FibonacciRecursive(n-1) + FibonacciRecursive(n-2));
}
else{
if (n == 0){
return 0;
}
else{
return 1;
}
}
}
public static int FibonacciIterative(int n){
int a = 0;
int b = 1;
//int totalC = 0;
for (int i = 1; i < n; i++){
int c = a+b;
a = b;
b = c;
}
return b;
}
public static double FibonacciNbOr(double n){
double val = ((sqrt(5) / 5) * pow(( (1 + sqrt(5)) / 2), n)-(sqrt(5) / 5) * pow(( (1 - sqrt(5)) / 2), n));
return val;
}
}