斐波那契数列算法

  斐波那契数列具有一个通式:

当n>2时,f(n)=f(n-1)+f(n-2);当n=1或n=2时,f(1)=f(2)=1.

代码实现:

package com.lk.C;

public class Test4 {
    public static int compute(int index){
        if((index == 1)||(index == 2)){
            return 1;
        }else{
            return compute(index-1)+compute(index-2);
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(compute(20));
    }

}
6765

 

posted @ 2015-04-07 21:16  luankun0214  阅读(190)  评论(0编辑  收藏  举报