public static void main(String[] args) {
System.out.println(function_2(100));
}
//求不死神兔
public static BigInteger function_2(int i){
if(i==1||i==2) {
BigInteger big=new BigInteger("1");
return big;
}
else{
BigInteger big1=function_2(i-2);
BigInteger big2=function_2(i-1);
BigInteger big=big1.add(big2);
return big;
}
}
差不多每增加5运行次数增长11-12倍,到50就得一天了(实际上我设置成50,跑了一天多才出结果)。
粗略算一下,i=50需要一天,每加5增长10倍,那么到100就是10的10次方,需要2000多万年才能执行完。
10:
calling times:109, time elapsed:0.005s
11
calling times:177, time elapsed:0.008s
12:
calling times:287, time elapsed:0.010s
15:
calling times:1219, time elapsed:0.025s
20:
calling times:13529, time elapsed:0.134s
25:
calling times:150049, time elapsed:0.781s
30:
calling times:1664079, time elapsed:6.698s
35:
calling times:18454929, time elapsed:64.653s
递归调用的次数太多了,需要运行的时间有点长。