/**
* 求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
* @author Tang
*
*/
public class Test2 {
/**
* 计算斐波那契数列
* @param index
* @return
*/
public static int computeFab(int index) {
if (index<=0) {
throw new IllegalStateException("min:1 , index:"+index);
}
if (index>=30) {
throw new IllegalStateException("max:30 , index:"+index);
}
if (index==1||index==2) {
return 1;
}else {
return computeFab(index-2)+computeFab(index-1);
}
}
public static void main(String[] args) {
System.out.println(computeFab(4));
System.out.println(computeFab(5));
System.out.println(computeFab(7));
}
}