Fibonnacci recursive call









up vote
0
down vote

favorite












How many times is the fib method in invoked for fib(6).
where the fib method is



public static long fib(long index) 
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return fib(index - 1) + fib(index - 2);

}


I end up tracing the recursive call to find how many times this method was called but this is so unpractical. Does this have a closed form function where you replace the value tested to know how many recursive call you will get?



Note that this was a question in my previous exam, (Exam is written) I know I could have used a count variable. Sorry for not mentioning this in the first place. This why am asking if there is a close form function for it.










share|improve this question



















  • 2




    How about incrementing a counter every time it's called, and printing it at the end?
    – JB Nizet
    Nov 10 at 15:28










  • You should try to solve the recurrence equation
    – Juan Ignacio Sánchez
    Nov 10 at 15:29










  • add counter which you can increment after calling this method and at the end you can print count value.
    – GauravRai1512
    Nov 10 at 15:33










  • Might be of some help - math.stackexchange.com/questions/178375/…
    – nullpointer
    Nov 10 at 15:33






  • 1




    Case base: 0 and 1 it returns 1, the no base case: calls(n) = 1 + calls(n -1) + calls(n -2). so: calls(fib(0)): 1, calls(fib(1)): 1, calls(fib(2)): 3, calls(fib(3)): 5, calls(fib(4)): 9, calls(fib(5)): 15, calls(fib(6)): 25
    – David Pérez Cabrera
    Nov 10 at 15:35















up vote
0
down vote

favorite












How many times is the fib method in invoked for fib(6).
where the fib method is



public static long fib(long index) 
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return fib(index - 1) + fib(index - 2);

}


I end up tracing the recursive call to find how many times this method was called but this is so unpractical. Does this have a closed form function where you replace the value tested to know how many recursive call you will get?



Note that this was a question in my previous exam, (Exam is written) I know I could have used a count variable. Sorry for not mentioning this in the first place. This why am asking if there is a close form function for it.










share|improve this question



















  • 2




    How about incrementing a counter every time it's called, and printing it at the end?
    – JB Nizet
    Nov 10 at 15:28










  • You should try to solve the recurrence equation
    – Juan Ignacio Sánchez
    Nov 10 at 15:29










  • add counter which you can increment after calling this method and at the end you can print count value.
    – GauravRai1512
    Nov 10 at 15:33










  • Might be of some help - math.stackexchange.com/questions/178375/…
    – nullpointer
    Nov 10 at 15:33






  • 1




    Case base: 0 and 1 it returns 1, the no base case: calls(n) = 1 + calls(n -1) + calls(n -2). so: calls(fib(0)): 1, calls(fib(1)): 1, calls(fib(2)): 3, calls(fib(3)): 5, calls(fib(4)): 9, calls(fib(5)): 15, calls(fib(6)): 25
    – David Pérez Cabrera
    Nov 10 at 15:35













up vote
0
down vote

favorite









up vote
0
down vote

favorite











How many times is the fib method in invoked for fib(6).
where the fib method is



public static long fib(long index) 
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return fib(index - 1) + fib(index - 2);

}


I end up tracing the recursive call to find how many times this method was called but this is so unpractical. Does this have a closed form function where you replace the value tested to know how many recursive call you will get?



Note that this was a question in my previous exam, (Exam is written) I know I could have used a count variable. Sorry for not mentioning this in the first place. This why am asking if there is a close form function for it.










share|improve this question















How many times is the fib method in invoked for fib(6).
where the fib method is



public static long fib(long index) 
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return fib(index - 1) + fib(index - 2);

}


I end up tracing the recursive call to find how many times this method was called but this is so unpractical. Does this have a closed form function where you replace the value tested to know how many recursive call you will get?



Note that this was a question in my previous exam, (Exam is written) I know I could have used a count variable. Sorry for not mentioning this in the first place. This why am asking if there is a close form function for it.







java recursion tracing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 15:45









Mikhail Kholodkov

3,74752443




3,74752443










asked Nov 10 at 15:24









Roland

143




143







  • 2




    How about incrementing a counter every time it's called, and printing it at the end?
    – JB Nizet
    Nov 10 at 15:28










  • You should try to solve the recurrence equation
    – Juan Ignacio Sánchez
    Nov 10 at 15:29










  • add counter which you can increment after calling this method and at the end you can print count value.
    – GauravRai1512
    Nov 10 at 15:33










  • Might be of some help - math.stackexchange.com/questions/178375/…
    – nullpointer
    Nov 10 at 15:33






  • 1




    Case base: 0 and 1 it returns 1, the no base case: calls(n) = 1 + calls(n -1) + calls(n -2). so: calls(fib(0)): 1, calls(fib(1)): 1, calls(fib(2)): 3, calls(fib(3)): 5, calls(fib(4)): 9, calls(fib(5)): 15, calls(fib(6)): 25
    – David Pérez Cabrera
    Nov 10 at 15:35













  • 2




    How about incrementing a counter every time it's called, and printing it at the end?
    – JB Nizet
    Nov 10 at 15:28










  • You should try to solve the recurrence equation
    – Juan Ignacio Sánchez
    Nov 10 at 15:29










  • add counter which you can increment after calling this method and at the end you can print count value.
    – GauravRai1512
    Nov 10 at 15:33










  • Might be of some help - math.stackexchange.com/questions/178375/…
    – nullpointer
    Nov 10 at 15:33






  • 1




    Case base: 0 and 1 it returns 1, the no base case: calls(n) = 1 + calls(n -1) + calls(n -2). so: calls(fib(0)): 1, calls(fib(1)): 1, calls(fib(2)): 3, calls(fib(3)): 5, calls(fib(4)): 9, calls(fib(5)): 15, calls(fib(6)): 25
    – David Pérez Cabrera
    Nov 10 at 15:35








2




2




How about incrementing a counter every time it's called, and printing it at the end?
– JB Nizet
Nov 10 at 15:28




How about incrementing a counter every time it's called, and printing it at the end?
– JB Nizet
Nov 10 at 15:28












You should try to solve the recurrence equation
– Juan Ignacio Sánchez
Nov 10 at 15:29




You should try to solve the recurrence equation
– Juan Ignacio Sánchez
Nov 10 at 15:29












add counter which you can increment after calling this method and at the end you can print count value.
– GauravRai1512
Nov 10 at 15:33




add counter which you can increment after calling this method and at the end you can print count value.
– GauravRai1512
Nov 10 at 15:33












Might be of some help - math.stackexchange.com/questions/178375/…
– nullpointer
Nov 10 at 15:33




Might be of some help - math.stackexchange.com/questions/178375/…
– nullpointer
Nov 10 at 15:33




1




1




Case base: 0 and 1 it returns 1, the no base case: calls(n) = 1 + calls(n -1) + calls(n -2). so: calls(fib(0)): 1, calls(fib(1)): 1, calls(fib(2)): 3, calls(fib(3)): 5, calls(fib(4)): 9, calls(fib(5)): 15, calls(fib(6)): 25
– David Pérez Cabrera
Nov 10 at 15:35





Case base: 0 and 1 it returns 1, the no base case: calls(n) = 1 + calls(n -1) + calls(n -2). so: calls(fib(0)): 1, calls(fib(1)): 1, calls(fib(2)): 3, calls(fib(3)): 5, calls(fib(4)): 9, calls(fib(5)): 15, calls(fib(6)): 25
– David Pérez Cabrera
Nov 10 at 15:35













4 Answers
4






active

oldest

votes

















up vote
0
down vote













You can pass some kind of Counter as an additional parameter that gets increased every time the function gets called:



class Counter 
private int value = 0;
public void increment()
value++;


public int getCurrentValue()
return value;




So that your code will become:



public static long fib(long index, Counter counter) 
counter.increment();
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return fib(index - 1, counter) + fib(index - 2, counter);

}


The invocation:



Counter counter = new Counter();
fib(6,counter);
counter.getCurrentValue(); // retrieve the current value which is exactly the number of invocation of fib function





share|improve this answer



























    up vote
    0
    down vote













    One option might be to add a static counter to your containing class:



    public class YourFib 
    private static int counter;

    public static long fib(long index)
    ++counter;
    if (index == 0) // Base case
    return 0;
    else if (index == 1) // Base case
    return 1;
    else // Reduction and recursive calls
    return fib(index - 1) + fib(index - 2);


    public static void main(String args)
    counter = 0;
    fib(6l);
    System.out.println("There were " + counter + " recursive calls.");




    By the way, computing the Fibonacci sequence using recursion is seriously sub-optimal. You should read about the dynamic programming say of doing this, where each level only needs to be computed once.






    share|improve this answer



























      up vote
      0
      down vote













      You can use something like this,



      public class A 
      private static int count = 0;

      public static void main(String argv)

      System.out.println(fib(6));
      System.out.println(count);


      public static long fib(long index)
      count++;
      if (index == 0) // Base case
      return 0;
      else if (index == 1) // Base case
      return 1;
      else // Reduction and recursive calls
      return fib(index - 1) + fib(index - 2);







      share|improve this answer



























        up vote
        0
        down vote













        Create a integer variable in your main function, public static int count = 0;. Then but this variable into your recursive function shown as below:



        `public static long fib(long index) 
        count ++;
        if (index == 0) // Base case
        return 0;
        else if (index == 1) // Base case
        return 1;
        else // Reduction and recursive calls
        return fib(index - 1) + fib(index - 2);`





        share|improve this answer




















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240398%2ffibonnacci-recursive-call%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          You can pass some kind of Counter as an additional parameter that gets increased every time the function gets called:



          class Counter 
          private int value = 0;
          public void increment()
          value++;


          public int getCurrentValue()
          return value;




          So that your code will become:



          public static long fib(long index, Counter counter) 
          counter.increment();
          if (index == 0) // Base case
          return 0;
          else if (index == 1) // Base case
          return 1;
          else // Reduction and recursive calls
          return fib(index - 1, counter) + fib(index - 2, counter);

          }


          The invocation:



          Counter counter = new Counter();
          fib(6,counter);
          counter.getCurrentValue(); // retrieve the current value which is exactly the number of invocation of fib function





          share|improve this answer
























            up vote
            0
            down vote













            You can pass some kind of Counter as an additional parameter that gets increased every time the function gets called:



            class Counter 
            private int value = 0;
            public void increment()
            value++;


            public int getCurrentValue()
            return value;




            So that your code will become:



            public static long fib(long index, Counter counter) 
            counter.increment();
            if (index == 0) // Base case
            return 0;
            else if (index == 1) // Base case
            return 1;
            else // Reduction and recursive calls
            return fib(index - 1, counter) + fib(index - 2, counter);

            }


            The invocation:



            Counter counter = new Counter();
            fib(6,counter);
            counter.getCurrentValue(); // retrieve the current value which is exactly the number of invocation of fib function





            share|improve this answer






















              up vote
              0
              down vote










              up vote
              0
              down vote









              You can pass some kind of Counter as an additional parameter that gets increased every time the function gets called:



              class Counter 
              private int value = 0;
              public void increment()
              value++;


              public int getCurrentValue()
              return value;




              So that your code will become:



              public static long fib(long index, Counter counter) 
              counter.increment();
              if (index == 0) // Base case
              return 0;
              else if (index == 1) // Base case
              return 1;
              else // Reduction and recursive calls
              return fib(index - 1, counter) + fib(index - 2, counter);

              }


              The invocation:



              Counter counter = new Counter();
              fib(6,counter);
              counter.getCurrentValue(); // retrieve the current value which is exactly the number of invocation of fib function





              share|improve this answer












              You can pass some kind of Counter as an additional parameter that gets increased every time the function gets called:



              class Counter 
              private int value = 0;
              public void increment()
              value++;


              public int getCurrentValue()
              return value;




              So that your code will become:



              public static long fib(long index, Counter counter) 
              counter.increment();
              if (index == 0) // Base case
              return 0;
              else if (index == 1) // Base case
              return 1;
              else // Reduction and recursive calls
              return fib(index - 1, counter) + fib(index - 2, counter);

              }


              The invocation:



              Counter counter = new Counter();
              fib(6,counter);
              counter.getCurrentValue(); // retrieve the current value which is exactly the number of invocation of fib function






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 10 at 15:30









              Mark Bramnik

              11.2k32445




              11.2k32445






















                  up vote
                  0
                  down vote













                  One option might be to add a static counter to your containing class:



                  public class YourFib 
                  private static int counter;

                  public static long fib(long index)
                  ++counter;
                  if (index == 0) // Base case
                  return 0;
                  else if (index == 1) // Base case
                  return 1;
                  else // Reduction and recursive calls
                  return fib(index - 1) + fib(index - 2);


                  public static void main(String args)
                  counter = 0;
                  fib(6l);
                  System.out.println("There were " + counter + " recursive calls.");




                  By the way, computing the Fibonacci sequence using recursion is seriously sub-optimal. You should read about the dynamic programming say of doing this, where each level only needs to be computed once.






                  share|improve this answer
























                    up vote
                    0
                    down vote













                    One option might be to add a static counter to your containing class:



                    public class YourFib 
                    private static int counter;

                    public static long fib(long index)
                    ++counter;
                    if (index == 0) // Base case
                    return 0;
                    else if (index == 1) // Base case
                    return 1;
                    else // Reduction and recursive calls
                    return fib(index - 1) + fib(index - 2);


                    public static void main(String args)
                    counter = 0;
                    fib(6l);
                    System.out.println("There were " + counter + " recursive calls.");




                    By the way, computing the Fibonacci sequence using recursion is seriously sub-optimal. You should read about the dynamic programming say of doing this, where each level only needs to be computed once.






                    share|improve this answer






















                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      One option might be to add a static counter to your containing class:



                      public class YourFib 
                      private static int counter;

                      public static long fib(long index)
                      ++counter;
                      if (index == 0) // Base case
                      return 0;
                      else if (index == 1) // Base case
                      return 1;
                      else // Reduction and recursive calls
                      return fib(index - 1) + fib(index - 2);


                      public static void main(String args)
                      counter = 0;
                      fib(6l);
                      System.out.println("There were " + counter + " recursive calls.");




                      By the way, computing the Fibonacci sequence using recursion is seriously sub-optimal. You should read about the dynamic programming say of doing this, where each level only needs to be computed once.






                      share|improve this answer












                      One option might be to add a static counter to your containing class:



                      public class YourFib 
                      private static int counter;

                      public static long fib(long index)
                      ++counter;
                      if (index == 0) // Base case
                      return 0;
                      else if (index == 1) // Base case
                      return 1;
                      else // Reduction and recursive calls
                      return fib(index - 1) + fib(index - 2);


                      public static void main(String args)
                      counter = 0;
                      fib(6l);
                      System.out.println("There were " + counter + " recursive calls.");




                      By the way, computing the Fibonacci sequence using recursion is seriously sub-optimal. You should read about the dynamic programming say of doing this, where each level only needs to be computed once.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 10 at 15:31









                      Tim Biegeleisen

                      212k1384132




                      212k1384132




















                          up vote
                          0
                          down vote













                          You can use something like this,



                          public class A 
                          private static int count = 0;

                          public static void main(String argv)

                          System.out.println(fib(6));
                          System.out.println(count);


                          public static long fib(long index)
                          count++;
                          if (index == 0) // Base case
                          return 0;
                          else if (index == 1) // Base case
                          return 1;
                          else // Reduction and recursive calls
                          return fib(index - 1) + fib(index - 2);







                          share|improve this answer
























                            up vote
                            0
                            down vote













                            You can use something like this,



                            public class A 
                            private static int count = 0;

                            public static void main(String argv)

                            System.out.println(fib(6));
                            System.out.println(count);


                            public static long fib(long index)
                            count++;
                            if (index == 0) // Base case
                            return 0;
                            else if (index == 1) // Base case
                            return 1;
                            else // Reduction and recursive calls
                            return fib(index - 1) + fib(index - 2);







                            share|improve this answer






















                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              You can use something like this,



                              public class A 
                              private static int count = 0;

                              public static void main(String argv)

                              System.out.println(fib(6));
                              System.out.println(count);


                              public static long fib(long index)
                              count++;
                              if (index == 0) // Base case
                              return 0;
                              else if (index == 1) // Base case
                              return 1;
                              else // Reduction and recursive calls
                              return fib(index - 1) + fib(index - 2);







                              share|improve this answer












                              You can use something like this,



                              public class A 
                              private static int count = 0;

                              public static void main(String argv)

                              System.out.println(fib(6));
                              System.out.println(count);


                              public static long fib(long index)
                              count++;
                              if (index == 0) // Base case
                              return 0;
                              else if (index == 1) // Base case
                              return 1;
                              else // Reduction and recursive calls
                              return fib(index - 1) + fib(index - 2);








                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 10 at 15:32









                              Sand

                              725111




                              725111




















                                  up vote
                                  0
                                  down vote













                                  Create a integer variable in your main function, public static int count = 0;. Then but this variable into your recursive function shown as below:



                                  `public static long fib(long index) 
                                  count ++;
                                  if (index == 0) // Base case
                                  return 0;
                                  else if (index == 1) // Base case
                                  return 1;
                                  else // Reduction and recursive calls
                                  return fib(index - 1) + fib(index - 2);`





                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    Create a integer variable in your main function, public static int count = 0;. Then but this variable into your recursive function shown as below:



                                    `public static long fib(long index) 
                                    count ++;
                                    if (index == 0) // Base case
                                    return 0;
                                    else if (index == 1) // Base case
                                    return 1;
                                    else // Reduction and recursive calls
                                    return fib(index - 1) + fib(index - 2);`





                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      Create a integer variable in your main function, public static int count = 0;. Then but this variable into your recursive function shown as below:



                                      `public static long fib(long index) 
                                      count ++;
                                      if (index == 0) // Base case
                                      return 0;
                                      else if (index == 1) // Base case
                                      return 1;
                                      else // Reduction and recursive calls
                                      return fib(index - 1) + fib(index - 2);`





                                      share|improve this answer












                                      Create a integer variable in your main function, public static int count = 0;. Then but this variable into your recursive function shown as below:



                                      `public static long fib(long index) 
                                      count ++;
                                      if (index == 0) // Base case
                                      return 0;
                                      else if (index == 1) // Base case
                                      return 1;
                                      else // Reduction and recursive calls
                                      return fib(index - 1) + fib(index - 2);`






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 10 at 15:33









                                      isydmr

                                      15710




                                      15710



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240398%2ffibonnacci-recursive-call%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Use pre created SQLite database for Android project in kotlin

                                          Darth Vader #20

                                          Ondo