-1

I've been playing with some code tonight and I coded a very simple algorithm that I think is a very good solution for a fibonacci recursive and for big numbers. I would like to know what you guys think about it and if you have any contribution for making it better, share with us... take a look at the code bellow

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(); 
    }

}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 4
    This question probably should be asked at [codereview](http://codereview.stackexchange.com/) – Pshemo Feb 12 '13 at 01:12
  • 2
    Is there a specific aspect of the program you are concerned with ? Or do you just want someone to look over it? If the latter, it would be better posted on codereview.stackexchange.com – Hunter McMillen Feb 12 '13 at 01:12
  • Just dont do it using recursion - its a horrible way to calculate Fibonacci numbers, because you have to calculate the values repeatedly for many times (instead of memorizing the partial solutions). See image here: http://en.algoritmy.net/article/45658/Fibonacci-series , it might helpp you to grasp the problem of recursive Fibonacci better. – malejpavouk Feb 15 '13 at 10:26

5 Answers5

1

Fibonacci sequences only depend on the previous two values, your base cases are F(0) = 1 and F(1) = 1, and for general F(n) = F(n - 1) + F(n - 2). All you need to do is remember these as you calculate. Recursion is a poor way to solve Fibonacci numbers as the higher the values the more calls you needlessly make. You'll soon run out of stack space.

James
  • 9,064
  • 3
  • 31
  • 49
1

I think a recursive fibonacci has a very high computational complexity too,probably O(n!), while the iterative is just O(n) :)

TravellingGeek
  • 1,581
  • 11
  • 16
1

The best way to compute the i-th Fibonacci is not by recursion. You can use these formulas:

enter image description here

enter image description here

enter image description here

phi is the golden ratio. However the problem with Fibonacci numbers is not related to the method to use but to the number of digits of number. They will become difficult to write for large numbers.

bitfox
  • 2,281
  • 1
  • 18
  • 17
0

One problem is, that calculateFib() does not return any calculation result. it only prints the result. So change the code such that it returns a result.

Once that works, try also the non recursive variant, which is in practise better suited.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

Because F 9999999 has ~2089876 digits, you'll want to use an approximation. Also consider memoization.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045