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.
java recursion tracing
add a comment |
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.
java recursion tracing
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
and1
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
add a comment |
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.
java recursion tracing
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
java recursion tracing
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
and1
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
add a comment |
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
and1
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
add a comment |
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
add a comment |
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.
add a comment |
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);
add a comment |
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);`
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 10 at 15:30
Mark Bramnik
11.2k32445
11.2k32445
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 10 at 15:31
Tim Biegeleisen
212k1384132
212k1384132
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 10 at 15:32
Sand
725111
725111
add a comment |
add a comment |
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);`
add a comment |
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);`
add a comment |
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);`
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);`
answered Nov 10 at 15:33
isydmr
15710
15710
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
and1
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