转载请注明出处:http://blog.csdn.net/huaiyiheyuan/article/details/53885852
一、介绍
在数学上,费波那契数列是以递归的方法来定义:
{\displaystyle F_{0}=0} F_{0}=0
{\displaystyle F_{1}=1} F_{1}=1
{\displaystyle F_{n}=F_{n-1}+F_{n-2}} F_{n}=F_{{n-1}}+F_{{n-2}}(n≧2)
用文字来说,就是费波那契数列由0和1开始,之后的费波那契系数就是由之前的两数相加而得出。首几个费波那契系数是:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233……(OEIS中的数列A000045)
特别指出:0不是第一项,而是第零项。
https://zh.wikipedia.org/wiki/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97
二、代码
/**
* Created by Jon on 2016/5/27.
* 15.3泛型接口
*/
public interface Generator<T> {
T next();
}
/**
* Created by Administrator on 2016/12/26.
* 费波那契数列
*/
public class Fibonacci implements Generator<Integer> {
private int count = 0;
@Override
public Integer next() {
return fib(count++);
}
private int fib(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
}
return fib(n - 2) + fib(n - 1);
}
}
Fibonacci fbn = new Fibonacci();
for (int i = 0; i < 5; i++) {
System.out.println(fbn.next());
}