Skip to content

Commit 4876991

Browse files
author
laileon
committed
comments
1 parent 48fbeb1 commit 4876991

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/com/blankj/custom/pretest/Fibonacci.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static BigInteger fib2(int n) {
3737
* @author bing
3838
*/
3939

40+
// https://blog.csdn.net/flyfish1986/article/details/48014523
4041

4142
// 关联矩阵
4243
private static final BigInteger[][] UNIT = {{BigInteger.valueOf(1), BigInteger.valueOf(1)}, {BigInteger.valueOf(1), BigInteger.valueOf(0)}};
@@ -49,19 +50,24 @@ static BigInteger fib2(int n) {
4950
* @param n
5051
* @return
5152
*/
53+
// 问题转换为二阶矩阵的n次幂
5254
public static BigInteger[][] fib3(int n) {
5355
if (n == 0) {
5456
return ZERO;
5557
}
5658
if (n == 1) {
5759
return UNIT;
5860
}
59-
// n是奇数
61+
// n是偶数
62+
//以计算A6为例
63+
//将6转化成二进制110 为A的4次方 和 A的2次方
6064
if ((n & 1) == 0) {
6165
BigInteger[][] matrix = fib3(n >> 1);
6266
return matrixMultiply(matrix, matrix);
6367
}
64-
// n是偶数
68+
// n是奇数
69+
//10进制7 = 二进制 111
70+
// 例如A7=A4次方∗A2次方∗A
6571
BigInteger[][] matrix = fib3((n - 1) >> 1);
6672
return matrixMultiply(matrixMultiply(matrix, matrix), UNIT);
6773
}

0 commit comments

Comments
 (0)