• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

new error message help?

 
Daniel Brackett
Greenhorn
Posts: 8
Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am lacking understand as to why I am getting this error message
at Euler1.divideBy5(Euler1.java:23)
at Euler1.divideBy5(Euler1.java:23)
at Euler1.divideBy5(Euler1.java:23)
at Euler1.divideBy5(Euler1.java:23)
at Euler1.divideBy5(Euler1.java:23)
at Euler1.divideBy5(Euler1.java:23)

I get the message about 1000 times any ideas?


Here's the Tester class
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you can't see the first line before the first 10K times the error occurred. It's probably a stack overflow due to a recursive method that calls itself so many times that the stack overflows. Check the exit path of your recursive algorithm to see why it might not be executing.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or if you want to see the whole thing, redirect the output to a file, then look at that.

Another option would be to run it for a smaller input...say 20, and see what that does.

Greg is correct..Here is what i get at the top (my number is different since I just put all your code in one file):

 
Daniel Brackett
Greenhorn
Posts: 8
Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Greg Brannon
It's probably a stack overflow due to a recursive method that calls itself so many times that the stack overflows. Check the exit path of your recursive algorithm to see why it might not be executing.

How does one check the exit path? what is the exit path?

I just want the output on the console. This is possible no?

So I could just rewrite it as a for loop Yes?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Brackett wrote:How does one check the exit path? what is the exit path?


Whenever you have a recursive method - one where the method calls itself - there must always be at least one situation where it doesn't call itself. And that must be a situation that will eventually happen. Otherwise you get an infinite loop, which is what happened to you.

The divideBy5() method that you've written always calls itself again. There's no exit condition: once it's called, there are no circumstances where it will ever end.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"check the exit path" means "look at the code and see what it is doing - how does your code break out of the recursive call?"

So let's say you go into your divideBy5 method, and val does equal 0. What happens? you call maketotal() (by the way, this really does nothing), decrement val, and then you call divideBy5. Val will now be -1. you call makeTotal, decrement val, etc...

So assuming you had enough memory, EVENTUALLY you would get to the lower limit of val, it would roll over to a positive value, and then run through a different branch of divideBy5....

but there is no way to ever get out of your recursive call.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does one check the exit path? what is the exit path?

I was going to ask you the same questions, except I might have led off with, "Is there an exit path?"

A recursive algorithm usually identifies a "base case" and tests for that during execution of the recursive method. Once the base case is achieved, then the recursive algorithm exits rather than calling itself (recursing) and 'unwinds'. So when should your divideBy5() method stop? What's the base case or indication that it doesn't need to continue calling itself.
 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The recursion is never termianted.

I think you should check your else-statement. Seems like the curly brackets are missing, which means the divideBy5() is called even if val<=0 is true.

I hope taht helps!

Michael
 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way, is it an exercise to practise recursion?

Otherwise, I believe that a single for-loop would do the job and the code would be easier to understand an debug. :-)

Cheers,
Michael
 
Mihai Boisteanu
Greenhorn
Posts: 8
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the problem:


You need to add some brackets there because after you test those you always invoke that function again, and again, and again until you get bored to death. So just use some brackets like here and you will be ok:
 
Daniel Brackett
Greenhorn
Posts: 8
Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Krimgen wrote:by the way, is it an exercise to practise recursion?

Otherwise, I believe that a single for-loop would do the job and the code would be easier to understand an debug. :-)

Cheers,
Michael


OK I need to review for loops before I do that. I started with recursion because it seemed right.

I would like to be able to do it recursively as-well I guess that I'll set up two different methods to do the same thing. good practice for next year.

Still havin' a rough go at it here's what doing recently.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it seems like you are putting the cart before the horse. The right way to do it is to think through the problem first, in English, and actually write out the steps. This seems VERY complicated. I don't understand why you need to use recursion AT ALL.

So...tell us your algorithm. What are you trying to do, and why do you need to do it recursively?
 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about



I don't want to do your work here, but what you want seems to be too simple to discuss for hours ...

Cheers,
Michael
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Challenge: Now do it without using % 5. You only need one % operator. You do need two loops, but miss out most of the % tests which will come out false.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Krimgen wrote:how about



I don't want to do your work here...


Please DON'T do their work. We encourage people to figure this out for themselves. Providing a full blown solution doesn't really help anyone.
 
Daniel Brackett
Greenhorn
Posts: 8
Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so It's over.
Here is the solutions both in Recursive and For Loop variants.
Why recursive? because practice makes me better and I have only been at programming for about 9 months now. Thinking about different solutions to problems is good practice to understanding how the problem could be resolved.



and

 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let’s see a version not using % 5. It can be done; it took me ages. Well over three minutes.
 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Challenge: Now do it without using % 5. You only need one % operator. You do need two loops, but miss out most of the % tests which will come out false.


Next challenge would be to have no % operator and no loop. Took me about 2 minutes. :-)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not bad. I shall have to think about that one. Is it a version of the sum of an arithmetical progression?
 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Not bad. I shall have to think about that one. Is it a version of the sum of an arithmetical progression?


It's a simple modified version of the 1 + 2 + 3 ... + n formula. Would be much more performant for really big numbers (using BigInteger). Imagine loops i case of n = 10^20 ...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, 1 + 2 + 3 +...+ n is an arithmetical progression, as is 3 + 9 + 12 ... + n. What do you do about duplicates, eg 15?
 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yes, 1 + 2 + 3 +...+ n is an arithmetical progression, as is 3 + 9 + 12 ... + n. What do you do about duplicates, eg 15?


first calculate 3 + 6 + 9 + ...
then caculate 5 + 10 + 15 ...
add the two results
then subtract 15 + 30 + 45 ...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It’s so easy and obvious when it is pointed out. Why don’t we have a smilie with Homer Simpson saying D’oh?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Brackett wrote:


There is some unnecessary stuff here. do you really need an "else if"?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic