-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigIntegerFibonacci.java
76 lines (69 loc) · 1.77 KB
/
BigIntegerFibonacci.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**************************************
Fib1(5)= Fib1(4) + Fib1(3)
---------------------------------------------
Fib1(4)=Fib1(3)+Fib1(2)
Fib1(3)=Fib1(2)+Fib1(1)
---------------------------------------------
Fib1(3)=Fib1(2)+Fib1(1)
Fib1(2)=Fib1(1)+Fib1(0)
Fib1(1)=1
---------------------------------------------
Fib1(2)=Fib1(1)+Fib1(0)
Fib1(1)=1
Fib1(0)=1
---------------------------------------------
Fib1(0)=1
***********************************************/
import java.math.*;
import java.math.BigInteger;
public class BigIntegerFibonacci {
public static BigInteger fib1(int n){
BigInteger fib= new BigInteger("0");
if(n==0 || n==1) {
return fib=BigInteger.ONE;
}
else {
return fib1(n-2).add(fib1(n-1));
}
}
public static BigInteger fib2(int n){
BigInteger back= new BigInteger("0");
BigInteger a= new BigInteger("0");
BigInteger b= new BigInteger("0");
BigInteger c= new BigInteger("0");
int i=1;
while(i<=n) {
if(i==1) {
a=a.add(BigInteger.ONE);
b=b.add(BigInteger.ONE);
back=a;
i=i+1;
}
else if(i==2){
a=a.add(b);
c=a.subtract(b);
b=c;
back=a;
i=i+1;
}
else {
c=a.add(b);
b=c.subtract(b);
a=c;
back=c;
i=i+1;
} // end of if-else
}
return back;
}
public static void main(String[] args) {
BigInteger r= new BigInteger("0");
String ausgabe ="";
for(int i=0; i<=15; i++) {
r=fib1(i);
ausgabe = ausgabe + "["+(r.toString())+"]";
}
BigInteger s=fib2(7);
System.out.println(ausgabe+"\n"+s.toString());
}
}