0

I'm unable to understand why the output is wrong.

I've tried to write a recursive code of printing Fibonacci numbers and got the expected output and a stream of unexpected values.

public class FibonacciSeries {

    static int limitNum = 10;  //the desired number of Fibonacci values

    public static void main(String[] args) {
        FibonacciSeries series = new FibonacciSeries();
        series.printRecursiveFibonacci(0,1,1);
    }

    public void printRecursiveFibonacci(int a, int b, int count)
    {
        while(count<=limitNum)
        {
          if(count==1||count==2)
          {
            System.out.println(count-1);
            count++;
            continue;
          }

        int k=a+b;
        a=b;
        b=k;
        System.out.println(b);
        count++;
        printRecursiveFibonacci(a, b, count);   
        }
    }

}

The expected output is 0 1 1 2 3 5 8 13 21 34

But I got - 0 1 1 2 3 5 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 5 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 3 5 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 5 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 2 3 5 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 8 13 21 34 34 21 34 34 13 21 ...

halfer
  • 19,824
  • 17
  • 99
  • 186
AzharKhaji
  • 21
  • 1
  • 3
    What's the purpose of your `while(count<=limitNum)` loop in your recursive function? That loop is occurring on every recursive call which doesn't make much sense. If you're using recursion, you don't need a loop. – lurker Aug 24 '19 at 20:14
  • 1
    "*Please help me in understand the output of this recursive Fibonacci java code ...*" --- Please read: [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) --- "*... and correct the logic*" - How is that supposed to help? If we solve it for you, we take away every chance for you learning something. – Turing85 Aug 24 '19 at 20:19
  • 1
    Possible duplicate of [Java recursive Fibonacci sequence](https://stackoverflow.com/questions/8965006/java-recursive-fibonacci-sequence) – Butiri Dan Aug 24 '19 at 20:21
  • @lurker, Thank you very much for pointing that out. I was practicing recursion and got lost. after replacing the while with if, it worked. Thank you again, I learnt my lesson. – AzharKhaji Aug 24 '19 at 23:13
  • @Turing85, I knew there is a mistake in my code and tried seeking help here to identity it as I was lost with it. Now, because of you, I learnt a better way to phrase my question and thanks a lot to your suggestion, really appreciate it. and I also spotted the mistake in my code. – AzharKhaji Aug 24 '19 at 23:26
  • @ButiriDan I'm afraid not and nonetheless, I got the answer. Thank you anyway, appreciate it. – AzharKhaji Aug 24 '19 at 23:28

2 Answers2

2

I'm not sure what exactly is going on in your code, a lot of weird things are going on:

  • while and recursion in the same method
  • if and continue instead of if/else
  • printing count - 1 sometimes and b other times.

I suspect it has something to do with your loop not terminating as you expect. In general, try to stick to either recursion or loops. Here is an example implementation:

public static void printFibLimit(int a, int b, int count) {
    if (count >= EXTERNALLY_DEFINED_LIMIT_VARIABLE) return;  // if true, we're done, no more work has to be done. 
// Since this is a tail-recursive function, return will terminate the function

    System.out.println(a);  // print our first value

    printFibLimit(b, a + b, count + 1);
    // a -> b
    // b -> a + b
    // count -> count + 1

}
cameron1024
  • 9,083
  • 2
  • 16
  • 36
1

The problem is that you have mixed 3 (!) different tasks within a single method:

  1. Calculation of the next Fibonacci number
  2. Printing Fibonacci number
  3. Limiting the number of Fibonacci numbers you are going to find

There is a good principle: separation of concerns. Each method should perform exactly one task.

Besides, if you refer recusrsion, probably you meant following:

fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)

Split your method:

  1. In one method keep only calculation the next Fibonacci number.
  2. In another method organize a loop and printing.

Here is a possible solution:

public class Fibonacci {

    private static final int LIMIT = 10;

    private static int fibonacci(int n) {
        if (n == 1) {
            // The 1st Fibonacci number is 0
            return 0;
        }

        if (n == 2) {
            // The 2nd Fibonacci number is 1
            return 1;
        }

        return fibonacci(n - 1) + fibonacci(n - 2);
    }

    public static void main(String[] args) {
        for (int i = 1; i <= LIMIT; i++) {
            int fibo = fibonacci(i);
            System.out.println(fibo);
        }
    }
}
mentallurg
  • 4,967
  • 5
  • 28
  • 36