Here's my code and it runs well with values of 400 to 4000 but once it's about 4mil, I get stack overflow errors.
Thanks in advance!
public class Fib {
static int c=1,b=2;
static long sum1=0,sum2=0;
static long fib(long a){
if(a==1){
return 1;
}
if(a==2){
return 2;
}
else{
return fib(a-1)+fib(a-2);
}
}
public static void main(String[] args){
sum2= fib(4000000);
System.out.println("Sum %f" +sum2);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…