The next generation Mordern iSeries Developer
The next generation Mordern iSeries Developer
thejwal pavithran wrote:Hey guys, i wrote this program for an interview process...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
The next generation Mordern iSeries Developer
Winston Gutkowski wrote:
thejwal pavithran wrote:Hey guys, i wrote this program for an interview process...
Thejwal,
Please don't put excessively long lines inside code blocks. It makes your thread hard to read. I've broken them up for you this time, but please re-read the UseCodeTags page thoroughly.
Thanks.
Winston
The next generation Mordern iSeries Developer
The next generation Mordern iSeries Developer
The next generation Mordern iSeries Developer
thejwal pavithran wrote:Can the above function result in infinite recursive calls? I get correct output for values less than 4000. but after that i get a StackOverFlowError.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
The next generation Mordern iSeries Developer
thejwal pavithran wrote:so basically there is nothing wrong with the code right?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:
thejwal pavithran wrote:so basically there is nothing wrong with the code right?
I don't think anybody is saying there is nothing wrong with the code.
What they are saying is that you don't necessarily have an infinite number of recursive calls just because you get a stack overflow. You may have an infinite number of calls, or you may not. You may have some other error - your computation may be wrong, you may have a spelling typo, there may be other errors. Most people here aren't going to take the time to do any DEEP analysis of your code.
Looking at your code for about 10 seconds, my biggest criticism is that there aren't any comments - nothing to explain what the code is doing or how it works.
Next, "tm" is a TERRIBLE variable name. Trademark? Too Much? Ted Might? Tickle Me? What the heck does "tm" mean? Variable names should be descriptive, not cryptic. I'm guessing it stands for "Tree Map", but what happens if you need two TreeMaps? would you use tm1 and tm2? I would have a long conversation with anyone on my team who wrote production code with such terrible names.
Sadly, now i look a little more at your code, and I see you actually DID use "sc" and "sc2". this is terrible.
The next generation Mordern iSeries Developer
Winston Gutkowski wrote:...[pell(4000)] Won't fit in a BigInteger...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
thejwal pavithran wrote:I am still trying to get the hang of java or to be really frank programming..Thanks for pointing out the smaller things I can pay attention to..To be honest all I had in mind was to get a program that printed the correct outputs before the tight deadline..
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski wrote:
(a) Won't fit in a BigInteger.
Jeff Verdegan wrote:My understanding is that BI itself doesn't impose any size limits, and numbers can grow arbitrarily large, subject only to the limit of your available memory and how long you're willing to wait for an answer. Or is there some inherent hard upper limit, like no more than Integer.MAX_VALUE digits or something?
Mike Simmons wrote:The magnitude needs to fit in an int[], which can have a max of Integer.MAX_VALUE elements. So the upper limit from that is ((2^32)^(2^31-1)) - 1. You'd need 2 GB of memory to achieve this.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
The next generation Mordern iSeries Developer
thejwal pavithran wrote:So is there any precaution that can be taken(apart from the compilation option to increase stack size) in the code to prevent the stackOverFlow error?
Paul Clapham wrote:Seems to me that if you use enough memoization, as described in your requirements, it wouldn't be necessary to use recursion at all. But the requirements say to use recursion anyway -- I can't figure out why they would say that.
The next generation Mordern iSeries Developer
Jeff Verdegan wrote:So is there any precaution that can be taken(apart from the compilation option to increase stack size) in the code to prevent the stackOverFlow error?
Other than increasing stack size, your only option is to use less stack. There are some general things you can do toward that end. I don't know how well they'll apply to your specific situation.
1. Make sure your recursion actually ends eventually. Most current hardware does not support infinite stacks.![]()
4. Break the problem into multiple iterative steps at the large-scale level. That is, as an example, instead of making one recursive call that goes 10,000 levels deep, make 100 calls that each go 100 levels deep.
The next generation Mordern iSeries Developer
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|