数字问题之斐波那契数列全解<Java实现>

/**
 * 斐波那契数列问题:
 *            0                   n=0;
 * F(n)= 1                    n=1;
 *           F(n-1)+F(n-2) n>1;
 * @author Sking

1.利用递推公式:
利用F(n)=F(n-1)+F(n-2)

2.利用通项公式:
F(n)=sqrt(5)/5*((1+sqrt(5))/2)^n-sqrt(5)/5*((1-sqrt(5))/2)^n

3.利用二阶递推关系:
(F(n),F(n-1))=(F(n-1),F(n-2))*A
A为二阶方阵,为 1 1
                        1 0
则(F(n),F(n-1))=(F(n-1),F(n-2))*A=(F(n-2),F(n-3))*A^2....=(F1,F0)*A^(n-1)
 */
package 数字问题;

public class 斐波那契数列 {
	
	/**
	 * 递归方法求解斐波那契数列问题
	 * @param n 数列下标
	 * @return  指定数的斐波那契值
	 */
	public static int fibonacci0(int n){
		if(n<=0)
			return 0;
		if(n==1)
			return 1;
		else
			return fibonacci0(n-1)+fibonacci0(n-2);
	}
	
	/**
	 * 预处理,使用数组存储计算过的值
	 * @param n 斐波那契数列长度
	 * @return 存储斐波那契数列的数组
	 */
	public static int[]  preDo(int n){
		if(n<=0)
			return new int[]{1};
		if(n==1)
			return new int[]{0,1};
		int[] array=new int[n+1];
		array[0]=0;
		array[1]=1;
		for(int i=2;i<=n;i++)
			array[i]=array[i-1]+array[i-2];
		return array;
	}
	
	/**
	 * 利用数组存储改进解斐波那契数列问题
	 * @param n  数列下标
	 * @return 指定数的斐波那契值
	 */
	public static int fibonacci1(int n){
		int[] fibonacci=preDo(n);
		return fibonacci[n];
	}
	
	
	/**
	 * 利用通项公式求解斐波那契数列问题
	 * 性能:O(1)
	 * 性能瓶颈:引入无理数,不能保证结果的精度
	 * @param n 数列下标
	 * @return 指定数的斐波那契值
	 */
	public static int fibonacci2(int n){
		double a= Math.pow((Math.sqrt(5)+1)/2, n);
		double b= Math.pow((1-Math.sqrt(5))/2, n);
		return (int)(Math.sqrt(5)/5*a-Math.sqrt(5)/5*b);
	}

	/**
	 * 利用二阶递推关系求解斐波那契数列问题
	 * 时间复杂度:O(log2(n))
	 * @param n 数列下标
	 * @return 指定数的斐波那契值
	 * @throws Exception 矩阵异常
	 */
	public static int fibonacci3(int n) throws Exception{
		int[][] array=new int[][]{{1,1},{1,0}};
		SquareMatrix A=new SquareMatrix(array);
		SquareMatrix an=SquareMatrix.SquareMatrixPow(A, n-1);
		//(F(n),F(n-1))==(F1,F0)*A^(n-1)=(1,0)*A^(n-1)
		return an.values[0][0];		
	}
}

/**
 * 方阵类
 * @author Sking
 */
class SquareMatrix{
	  protected int   size;//方阵大小
	  protected int values[][];//元素数组
	  
	  /**
	   * 指定大小的构造函数
	   * @param size 方阵的大小
	   */
		public SquareMatrix(int size) {
			this.size = (size > 0) ? size : 1;
			values = new int[size][size];
		}
		
		/**
		 * 指定元素数组的构造函数
		 * @param array 用于赋值的元素数组
		 */
		public SquareMatrix(int[][] array){
			values=array;
			size=array.length;
		}
		
		/**
		 * 计算当前方阵与指定方阵相乘结果
		 * @param m 指定方阵
		 * @return 当前方阵与指定方阵相乘结果
		 * @throws Exception 矩阵相乘条件不满足,抛出异常
		 */
		public SquareMatrix multiply(SquareMatrix m) throws Exception{
			if(this.size!=m.size)
				throw new Exception("两矩阵不满足相乘条件!");
			int pv[][] = new int[m.size][m.size];
			for (int r = 0; r < m.size; ++r) {
				for (int c = 0; c < m.size; ++c) {
					int dot = 0;
					for (int k = 0; k < m.size; ++k) {
						dot += values[r][k] * m.values[k][c];
					}
					pv[r][c] = dot;
				}
			}
			return new SquareMatrix(pv);
		}   
		
		/**
		 * 计算矩阵的幂方
		 * @param m 指定矩阵
		 * @param n 幂方指数
		 * @return 指定矩阵的幂方
		 * @throws Exception 
		 */
		public static SquareMatrix SquareMatrixPow(SquareMatrix m,int n) throws Exception{
			int s=m.size;
			SquareMatrix result=new SquareMatrix(s);
			for(int i=0;i<result.size;i++)//结果矩阵初始设置为单位矩阵
				for(int j=0;j<result.size;j++)
					result.values[i][j]=(i==j)?1:0;
			SquareMatrix temp=m;
			for(;n!=0;n>>=1){
				if((n&1)!=0)
						result=result.multiply(temp);
				temp=temp.multiply(temp);
			}
			return result;
		}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值