# Recursion ouput is not coming for value more than 40.

• July 7th, 2011, 02:17 PM
Revati
Recursion ouput is not coming for value more than 40.
Hello All,

I have written following code using recursion.It is working however it is not giving me ouput for value 50.Infact nothing is display when I enter number greater than 40.

Code Java:

```import java.util.Scanner;   public class Test {     public static void main(String[] args) {   Scanner in =new Scanner(System.in); System.out.println("Enter number"); int n = in.nextInt();   System.out.println(Mymethod(n));   }       public static long Mymethod(long n){   if(n==0){   return 0; }   else if(n==1){   return 1; }   else if(n==2){   return 1; } else{   return (Mymethod(n-1))+ (Mymethod(n-2))+ (Mymethod(n-3));   } } }```
• July 7th, 2011, 02:36 PM
Norm
Re: Recursion ouput is not coming for value more than 40.
Have you waited long enough for the program to finish?
• July 7th, 2011, 03:20 PM
Revati
Re: Recursion ouput is not coming for value more than 40.
Hi..Thank you for reply but I waited for around 20 min.
• July 7th, 2011, 03:33 PM
Norm
Re: Recursion ouput is not coming for value more than 40.
Your algorithm must be doing a lot of recursive calling.
Where did you get the code? Did it give an expression or description of the number of calls it makes.
• July 7th, 2011, 03:36 PM
Revati
Re: Recursion ouput is not coming for value more than 40.
I have written this code.NO there is no description.Expression was

f(n) =f(n-1)+f(n-2)+f(n-3)

Base case was f(0)=1,f(1)=2,f(2)=1

Question was to write code in java or any object oriented language to return nth number from given expression.
• July 7th, 2011, 03:39 PM
Norm
Re: Recursion ouput is not coming for value more than 40.
Have you tried an iterative approach vs a recursive one?

f(1)=2

else if(n==1){

return 1;
• July 7th, 2011, 03:45 PM
Revati
Re: Recursion ouput is not coming for value more than 40.
Is there anything wrong in this code?
• July 7th, 2011, 03:47 PM
Revati
Re: Recursion ouput is not coming for value more than 40.
oh sorry no,it is f(1) =1 only.
sorry for that typing mistake.

I have not yet tried iterative way.
• July 7th, 2011, 03:48 PM
Revati
Re: Recursion ouput is not coming for value more than 40.
My computer is very slow.Is it running at your end for value 50?
• July 7th, 2011, 03:48 PM
copeg
Re: Recursion ouput is not coming for value more than 40.
Please do not post the same question twice to the forums, and please use the code tags. I've locked your other post, and am providing a link to said post as it has my reply - you must realize how many calls your code makes with a value of 50 (we're talking trillions +) which don't happen in the blink of an eye

http://www.javaprogrammingforums.com...ig-values.html
• July 7th, 2011, 04:02 PM
Revati
Re: Recursion ouput is not coming for value more than 40.
@ copeg:Thanks for reply.I registered today here so sorry about that.

Yes.. I waited around 20 min..Can I write this code using recursion better way?efficient?
• July 7th, 2011, 04:03 PM
Revati
Re: Recursion ouput is not coming for value more than 40.
there is no exception coming..
• July 7th, 2011, 06:49 PM
copeg
Re: Recursion ouput is not coming for value more than 40.
Quote:

Can I write this code using recursion better way?efficient?
Yes. Draw out how the algorithm actually works. Start from a given value, lets say 5. There is a better way to do it than like this, but here's a start:

5 recursively computes f(4), f(3), f(2).
->4 recursively computes f(3), f(2), f(1).
->3 recursively computes f(2), f(1), f(0).

So here's the BIG hint: notice how values are calculated more than once a) think about how this would be impacted with larger starting values. Is there really a need to recalculate what you've already calculated? b) if you understand the above, do you have ideas for reducing redundancy?

Edit: And suggested reading: http://en.wikipedia.org/wiki/Memoization