今晚我一直在玩一些代码,我编写了一个非常简单的算法,我认为它对于斐波那契递归和大数字来说是一个非常好的解决方案。我想知道你们对此有何看法,如果您对改进它有任何贡献,请与我们分享……看看下面的代码
public class RecursiveFibonacci {
public void calculateFib(long first, long next, long counter, int n) {
if(counter == n) {
return;
}
counter++;
if(first == 0 && next == 0) {
System.out.println("0");
calculateFib(0, 1, counter, n);
return;
}
if(first == 0 && next == 1) {
System.out.println("1");
calculateFib(1, 0, counter, n);
return;
}
if(first == 1 && next == 0) {
System.out.println("1");
calculateFib(1, 1, counter, n);
return;
}
long result = first + next;
if(result > 1) {
System.out.println(result);
calculateFib(next, result, counter, n);
}
}
public RecursiveFibonacci() {
calculateFib(0, 0, 0, 9999999);
}
public static void main(String[] args) {
new RecursiveFibonacci();
}
}