Fibonacci数列

费波那契数列由0和1开始,之后的费波那契系数就是由之前的两数相加而得出

 
0,1,1,2,3,5,8,13,21,34,55,89,144,233
 
package com.wolaidai.finance.loan.service.confirm.impl;

/**
 * f(0) = 0;
 * f(1) = 1;
 * f(2) = 1;
 * f(3) = 2;
 * 
 * 0, 1, 1, 2, 3, 5, 8, 
 * <img src=""></img>
 * @author rong.e
 *
 */
public class T {

    public static void main(String[] args) {
        new T().test();
    }

    public void test() {
        // f(n) = ?
        int r = fibonacci(6, 1, 1);
        System.err.println(r);
    }

    public int fibonacci(int n, int a, int b) {
        if (n < 0) {
            return -1;
        }

        else if (n == 1 || n == 2) {
            return b;
        }

        else {
            while (n > 2) {
                return fibonacci(n - 1, b, b + a);
            }
        }
        return 0;
    }
}

 

posted on 2017-12-19 09:56  jis117  阅读(143)  评论(0编辑  收藏  举报

导航