求斐波那契数

package test;

public class ImprovedFibonacci {

//    public static long fib1(long index) {
//        if (index == 0) {
//            return 0;
//        } else if (index == 1) {
//            return 1;
//        } else {
//            return fib1(index - 1) + fib1(index - 2);
//        }
//    }
    public static long fib(long index) {
        long f0 = 0;
        long f1 = 1;
        long f2 = 1;

        if (index == 0) {
            return f0;
        } else if (index == 1) {
            return f1;
        } else if (index == 2) {
            return f2;
        }
        for (int i = 3; i <= index; i++) {
            f0 = f1;
            f1 = f2;
            f2 = f0 + f1;
        }
        return f2;

    }

    public static void main(String[] args) {
        
    }

}

原文地址:https://www.cnblogs.com/huangzhijun/p/2981646.html