1
1
package com .thealgorithms .matrix .matrixexponentiation ;
2
2
3
+ <<<<<<< HEAD
3
4
import java .math .BigDecimal ;
4
5
import java .util .Scanner ;
5
6
6
7
import com .thealgorithms .matrix .utils .MatrixUtil ;
7
8
9
+ =======
10
+ import java .util .Scanner ;
11
+
12
+ >>>>>>> 754 bf6c5f8f55b758bdee2667f6cadf4f0ab659f
8
13
/**
9
14
* @author Anirudh Buvanesh (https://github.com/anirudhb11) For more information
10
15
* see https://www.geeksforgeeks.org/matrix-exponentiation/
@@ -15,13 +20,49 @@ private Fibonacci() {
15
20
}
16
21
17
22
// Exponentiation matrix for Fibonacci sequence
23
+ <<<<<<< HEAD
18
24
private static final BigDecimal ONE = BigDecimal .valueOf (1 );
19
25
private static final BigDecimal ZERO = BigDecimal .valueOf (0 );
20
26
21
27
private static final BigDecimal [][] FIB_MATRIX = {{ONE , ONE }, {ONE , ZERO }};
22
28
private static final BigDecimal [][] IDENTITY_MATRIX = {{ONE , ZERO }, {ZERO , ONE }};
23
29
// First 2 fibonacci numbers
24
30
private static final BigDecimal [][] BASE_FIB_NUMBERS = {{ONE }, {ZERO }};
31
+ =======
32
+ private static final int [][] FIB_MATRIX = {{1 , 1 }, {1 , 0 }};
33
+ private static final int [][] IDENTITY_MATRIX = {{1 , 0 }, {0 , 1 }};
34
+ // First 2 fibonacci numbers
35
+ private static final int [][] BASE_FIB_NUMBERS = {{1 }, {0 }};
36
+
37
+ /**
38
+ * Performs multiplication of 2 matrices
39
+ *
40
+ * @param matrix1
41
+ * @param matrix2
42
+ * @return The product of matrix1 and matrix2
43
+ */
44
+ private static int [][] matrixMultiplication (int [][] matrix1 , int [][] matrix2 ) {
45
+ // Check if matrices passed can be multiplied
46
+ int rowsInMatrix1 = matrix1 .length ;
47
+ int columnsInMatrix1 = matrix1 [0 ].length ;
48
+
49
+ int rowsInMatrix2 = matrix2 .length ;
50
+ int columnsInMatrix2 = matrix2 [0 ].length ;
51
+
52
+ assert columnsInMatrix1 == rowsInMatrix2 ;
53
+ int [][] product = new int [rowsInMatrix1 ][columnsInMatrix2 ];
54
+ for (int rowIndex = 0 ; rowIndex < rowsInMatrix1 ; rowIndex ++) {
55
+ for (int colIndex = 0 ; colIndex < columnsInMatrix2 ; colIndex ++) {
56
+ int matrixEntry = 0 ;
57
+ for (int intermediateIndex = 0 ; intermediateIndex < columnsInMatrix1 ; intermediateIndex ++) {
58
+ matrixEntry += matrix1 [rowIndex ][intermediateIndex ] * matrix2 [intermediateIndex ][colIndex ];
59
+ }
60
+ product [rowIndex ][colIndex ] = matrixEntry ;
61
+ }
62
+ }
63
+ return product ;
64
+ }
65
+ >>>>>>> 754 bf6c5f8f55b758bdee2667f6cadf4f0ab659f
25
66
26
67
/**
27
68
* Calculates the fibonacci number using matrix exponentiaition technique
@@ -30,6 +71,7 @@ private Fibonacci() {
30
71
* Outputs the nth * fibonacci number
31
72
* @return a 2 X 1 array as { {F_n+1}, {F_n} }
32
73
*/
74
+ <<<<<<< HEAD
33
75
public static BigDecimal [][] fib (int n ) {
34
76
if (n == 0 ) {
35
77
return IDENTITY_MATRIX ;
@@ -40,6 +82,18 @@ public static BigDecimal[][] fib(int n) {
40
82
return matrixExpResult ;
41
83
} else {
42
84
return MatrixUtil .multiply (FIB_MATRIX , matrixExpResult ).get ();
85
+ =======
86
+ public static int [][] fib (int n ) {
87
+ if (n == 0 ) {
88
+ return IDENTITY_MATRIX ;
89
+ } else {
90
+ int [][] cachedResult = fib (n / 2 );
91
+ int [][] matrixExpResult = matrixMultiplication (cachedResult , cachedResult );
92
+ if (n % 2 == 0 ) {
93
+ return matrixExpResult ;
94
+ } else {
95
+ return matrixMultiplication (FIB_MATRIX , matrixExpResult );
96
+ >>>>>>> 754 bf6c5f8f55b758bdee2667f6cadf4f0ab659f
43
97
}
44
98
}
45
99
}
@@ -48,7 +102,11 @@ public static void main(String[] args) {
48
102
// Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ]
49
103
Scanner sc = new Scanner (System .in );
50
104
int n = sc .nextInt ();
105
+ <<<<<<< HEAD
51
106
BigDecimal [][] result = MatrixUtil .multiply (fib (n ), BASE_FIB_NUMBERS ).get ();
107
+ =======
108
+ int [][] result = matrixMultiplication (fib (n ), BASE_FIB_NUMBERS );
109
+ >>>>>>> 754 bf6c5f8f55b758bdee2667f6cadf4f0ab659f
52
110
System .out .println ("Fib(" + n + ") = " + result [1 ][0 ]);
53
111
sc .close ();
54
112
}
0 commit comments