-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
37 lines (35 loc) · 1.02 KB
/
Fibonacci.java
File metadata and controls
37 lines (35 loc) · 1.02 KB
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
import java.util.*;
import java.math.BigInteger;
class Fibonacci
{
public static BigInteger F(int n)
{
if (n == 0)
return BigInteger.ZERO;
if (n == 1)
return BigInteger.ONE;
if (n % 2 == 0)
{
int k = n / 2;
BigInteger a = F(k), b = F(k - 1);
return a.multiply(BigInteger.valueOf(2).multiply(b).add(a));
}
int k = (n - 1) / 2;
BigInteger a = F(k), b = F(k - 1);
return BigInteger.valueOf(2).multiply(a).add(b).multiply(BigInteger.valueOf(2).multiply(a).subtract(b)).add(BigInteger.valueOf(-4 * (k % 2) + 2));
}
public static void main(String args[])
{
System.out.println("This program computes the n-th Fibonacci number.");
Scanner sc = new Scanner(System.in);
int n;
do
{
System.out.print("Enter n :\t");
n = sc.nextInt();
if (n >= 0)
System.out.printf("F(%,d)\t=%, d\n", n, F(n));
}
while (n >= 0);
}
}