• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Stopping case a recursive and iterative code

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!)


 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 8530
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...



 
Marshal
Posts: 5471
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That seems right to me.
 
Marshal
Posts: 76869
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Our first order of business must be this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic