This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFibonacci.java
51 lines (39 loc) · 1.49 KB
/
Fibonacci.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
package com.michaelmidura.kataday.day031;
import java.math.BigInteger;
public class Fibonacci {
public static BigInteger fib(BigInteger n) {
BigInteger result[][] = {{BigInteger.ONE, BigInteger.ONE}, {BigInteger.ONE, BigInteger.ZERO}};
boolean negated = false;
switch (n.compareTo(BigInteger.ZERO)) {
case -1:
n = n.negate();
negated = true;
break;
case 0:
return BigInteger.ZERO;
}
power(result, n.subtract(BigInteger.ONE));
if (negated)
return n.mod(BigInteger.valueOf(2)).equals(BigInteger.ONE) ? result[0][0] : result[0][0].negate();
return result[0][0];
}
private static void power(BigInteger array[][], BigInteger n) {
if (n.equals(BigInteger.ZERO) || n.equals(BigInteger.ONE))
return;
BigInteger identity[][] = {{BigInteger.ONE, BigInteger.ONE}, {BigInteger.ONE, BigInteger.ZERO}};
power(array, n.divide(BigInteger.valueOf(2)));
multiply(array, array);
if (!n.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO))
multiply(array, identity);
}
private static void multiply(BigInteger arr1[][], BigInteger arr2[][]) {
BigInteger x = (arr1[0][0].multiply(arr2[0][0])).add(arr1[0][1].multiply(arr2[1][0]));
BigInteger y = (arr1[0][0].multiply(arr2[0][1])).add(arr1[0][1].multiply(arr2[1][1]));
BigInteger z = (arr1[1][0].multiply(arr2[0][0])).add(arr1[1][1].multiply(arr2[1][0]));
BigInteger w = (arr1[1][0].multiply(arr2[0][1])).add(arr1[1][1].multiply(arr2[1][1]));
arr1[0][0] = x;
arr1[0][1] = y;
arr1[1][0] = z;
arr1[1][1] = w;
}
}