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, 04: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, 04:39 PM

Norm

Re: Recursion ouput is not coming for value more than 40.

Have you tried an iterative approach vs a recursive one?

Your code doesn't follow the equations:

f(1)=2

else if(n==1){

return 1;

July 7th, 2011, 04:45 PM

Revati

Re: Recursion ouput is not coming for value more than 40.

Is there anything wrong in this code?

July 7th, 2011, 04: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, 04: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, 04: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

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?