Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Java recursion problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java recursion problem" Watch "Java recursion problem" New topic

Java recursion problem

thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 119

hey guys,

im sort of new to java and im trying to write this complicated(for me) basically given a number n,the the output is 2*function(n-1)+function(n-1)
for n=1,n=0 return 1 and o respectively.

And the constraint is that the value once computed should not be computed again that is all computed values should be stored and used the next time when the same calculation is done. so i tried to figure out TreeMaps and also generics. and i believe that i have the program obeying the conditions.

but the biggest value the program gives an output is 4250 after which an exception is thrown and its getMessage prints a null . i want to be able to give inputs till 10000.

I dont know why i did it..but i googled out the largest number datatype and coded using the BigInteger but the result is not correct better than Integer though.

Please tell my mistake.

on job hunt
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
First off, "2*function(n-1)+function(n-1)" is the same as "3*function(n-1)". This sequence of numbers will grow fast, so you definitely need to use BigInteger. As for storage, any Map<Integer, BigInteger> will do - HashMap, for example.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

As for the exception: change your code to print the stack trace, not just the message. Like this:

Then you will know exactly what exception was thrown and exactly what line of code threw it.
Henry Wong

Joined: Sep 28, 2004
Posts: 20526

thejwal pavithran wrote:

Well, since the code has been deleted -- and for what seems like an important reason -- this topic is arguably less likely to generate detailed answers. No reason to allow it to continue.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Java recursion problem
It's not a secret anymore!