• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping case a recursive and iterative code  RSS feed

 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the stopping case for the code that uses loop the variable n when it's 1? Also for the recursive method, is the stopping case when n is 1?

Code that uses loop to figure out the factorial of (n!)




Code that uses recursive to figure out the factorial of (n!)


 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe 0! = 1, not 0. (That doesn't answer your questions, but I'm sure someone will.)
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you recursive function is a little more complicated than it needs to be. Try getting rid of the factorial variable.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It would also help if the OP would define what is meant by "stopping case".


For recursion, I would speculate that it is the "termination condition", which is the condition that is met, before recursion stops.

For iteration, that would be harder to speculate, as there are cases of n where the loop is never executed.

Henry
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I believe 0! = 1, not 0. (That doesn't answer your questions, but I'm sure someone will.)


Okay I fixed it. I returned 1 when n == 0 or n == 1.
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
It would also help if the OP would define what is meant by "stopping case".


For recursion, I would speculate that it is the "termination condition", which is the condition that is met, before recursion stops.

For iteration, that would be harder to speculate, as there are cases of n where the loop is never executed.

Henry


Yes, you are right with your speculation. I actually think that the stopping case for both method is 0. What do you think about my proposed stopping cases?
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think n == 1 is the terminating condition in the recursive code, because you never get to n == 0 unless you enter 0.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using ternary operator (? : ) you could get body of you factorial method as short as 1 line of code.

[edit] got smile inside parenthesis, had to fix it.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got emoji spammed. (?:) You can disable smilies in a message.
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Well, I think n == 1 is the terminating condition in the recursive code, because you never get to n == 0 unless you enter 0.


You know what, now that you said it, it makes sense. Well a stopping case is basically what causes the methods to end. As for the loop method it stops when n is 0. When it's 0 it goes out of the while loop and returns the factorial. Correct?
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Just as an example; here is a recursive code example that build a flat list from an n-tree node (a parent can have an unlimited number of children). It would look pretty complex without recursion.

Here the stop condition occurs multiple times, it stops and pop the stack every time a node without a child is encountered and continue with the next child.

In prolog, you are required to use recursion since loops don't even exist! Code is usually only a few lines but require more thinking...



 
Tim Cooke
Marshal
Posts: 4038
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conrado, while we don't mind if you cross post we do ask that you tell us that you have done so and cite where. Please read -> BeForthrightWhenCrossPostingToOtherSites

Exact same question asked at java-forums.org.
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Conrado, while we don't mind if you cross post we do ask that you tell us that you have done so and cite where. Please read -> BeForthrightWhenCrossPostingToOtherSites

Exact same question asked at java-forums.org.


Alright next time I will cite where I've cross post. Thanks for the heads up!
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Well, I think n == 1 is the terminating condition in the recursive code, because you never get to n == 0 unless you enter 0.


For the loop method it stops when n is 0. When it's 0 it goes out of the while loop and returns the factorial. Correct?
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That seems right to me.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You got emoji spammed. (?:) . . .
You can also write ‍ after the : and the emoji will vanish. That means zero‑width joiner.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!