1. 程式人生 > >斐波那契數列的矩陣解法(java實現)

斐波那契數列的矩陣解法(java實現)

使用了二分法

import java.util.Scanner;

/**
 * 求斐波那契數列<br/>
 * <pre>
 * [F(n+1) F(n)]    [1  1 ]^n   (n次方,可以使用歸納法證明)<br/>
 * |           |   =|     |                     <br/>
 * [F(n) F(n-1)]    [1  0 ]                     <br/>
 * </pre>
 * @author bing
 *
 */
public class F {
	// 關聯矩陣
	private static final int[][] UNIT = { { 1, 1 }, { 1, 0 } };
	// 全0矩陣
	private static final int[][] ZERO = { { 0, 0 }, { 0, 0 } };

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			// 第n個斐波那契數,從0開始
			int n = scanner.nextInt();
			long t1 = System.currentTimeMillis();
                        int[][] m = fb(n);
			long t2 = System.currentTimeMillis();
			System.out.println(m[0][1]);
			System.out.println("Time is: " + (t2 - t1));
		}
	}

	/**
	 * 求斐波那契數列
	 * 
	 * @param n
	 * @return
	 */
	public static int[][] fb(int n) {
		if (n == 0) {
			return ZERO;
		}
		if (n == 1) {
			return UNIT;
		}
		// n是奇數
		if ((n & 1) == 0) {
			int[][] matrix = fb(n >> 1);
			return matrixMultiply(matrix, matrix);
		}
		// n是偶數
		int[][] matrix = fb((n - 1) >> 1);
		return matrixMultiply(matrixMultiply(matrix, matrix), UNIT);
	}

	/**
	 * 矩陣相乘
	 * 
	 * @param m
	 *            r1*c1
	 * @param n
	 *            c1*c2
	 * @return 新矩陣,r1*c2
	 */
	public static int[][] matrixMultiply(int[][] m, int[][] n) {
		int rows = m.length;
		int cols = n[0].length;
		int[][] r = new int[rows][cols];
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				r[i][j] = 0;
				for (int k = 0; k < m[i].length; k++) {
					r[i][j] += m[i][k] * n[k][j];
				}
			}
		}
		return r;
	}

}