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 ...

6
  • 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. Commented Aug 24, 2019 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? --- "... 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. Commented Aug 24, 2019 at 20:19
  • 1
    Possible duplicate of Java recursive Fibonacci sequence Commented Aug 24, 2019 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. Commented Aug 24, 2019 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. Commented Aug 24, 2019 at 23:26

2 Answers 2

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I got it right now after replacing the Loop with If condition. I learnt a lot from your answer. Thank you for helping this newbie, really appreciate it.
No problem, glad I could help :)
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);
        }
    }
}

3 Comments

Thank you for suggesting the design pattern. I was trying to practice recursion and got lost with the code.
See above with recursion.
@mentalurg, Thank you, I got it right after replacing the Loop with If condition. I learnt a lot from your answer. Thank you for helping this newbie, really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.