Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By JosAH

Thread: What are the base case/stopping case for these loop and recursive methods?

  1. #1
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default What are the base case/stopping case for these loop and recursive methods?

    I want to this verify with you guys. Is the stopping case for the code that uses loop the variable n when it's 0? For the recursive method, the stopping case is when n is 1? Anyone disagree?

    HERE ARE THE TWO METHODS. ONE OF THEM USES LOOP AND THE OTHER USES RECURSIVE

    Code that uses loop to figure out the factorial of (n!)
    Java Code:
    public static int loopFactorial(int n) {
            int factorial = 1;
            if ((n == 0) || (n == 1)) {
                return 1;
            }
    
            while (n >= 1) {
                factorial = factorial * n;
                n--;
            }
            return factorial;
        }

    Code that uses recursive to figure out the factorial of (n!)
    Java Code:
    public static int recursiveFactorial(int n) {
            int factorial = n;
            if ((n == 0) || (n == 1)) {
                return 1;
            } else {
                factorial = recursiveFactorial(n - 1) * factorial;
            }
            return factorial;
        }
    Last edited by csanch11; 11-15-2015 at 03:59 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    Can you post your reasons for making those choices?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    Well in my book, they said that "One or more of these cases should provide a solution that does not require recursion. These are the base cases, or stopping cases. If you look at the method that uses recursive you'll see that when the value inside the parenthesis of recursiveFactorial(n-1) becomes 0 then if ((n == 0) || (n == 1)) there will be no recursion required. Does that make sense?
    Last edited by csanch11; 11-14-2015 at 11:43 PM.

  4. #4
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: What are the base case/stopping case for these loop and recursive methods?


  5. #5
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    Yes, I post the same questions on two different forums. Much more efficient If you ask me.

  6. #6
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    Alright next time I will cite where I've cross post. Thanks for the heads up!

  7. #7
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    Quote Originally Posted by csanch11 View Post
    Yes, I post the same questions on two different forums. Much more efficient If you ask me.
    Perhaps more efficient for you in the short term. But for the guys spending their own free time thinking about and answering your question, only to find it has been already answered on another forum 2 days ago, it's really really annoying. In the long term, regular helpers may just not bother with your questions any more. Alienating forum regular helpers is not very efficient.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    It all depends on the implementation. In your example, the "stopping case" could be a previously calculated and cached factorial. This technique is known as memoization.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    So no one here disagrees with my initial statements regarding the stopping case of the two methods?????????

  10. #10
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    Never mind. My question was answered in my cross post. Thanks.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    Quote Originally Posted by csanch11 View Post
    Never mind. My question was answered in my cross post. Thanks.
    I'd say the answer in the other forum is incorrect: the loop or recursion just not start when n == 0, but it also does the same when n == 1 (see your code); assuming that negative values for n are rules out, the stop condition is n < 2.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  12. #12
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    For the loop method it is true that the stopping case is n < 2 if the parameter variable is 0 or 1 to start with because the first thing my code checks is if n is equal to either 0 or 1. If n is not initially 0 or 1, then it seems to me that the stopping case is exactly 0, since the program will exit the while loop because 0 >= 1 will be false and the factorial will be returned.

    The same goes for my recursive method, the stopping case is n < 2 if the parameter variable is 0 or 1 to start. Although if n is not initially 0 or 1, then the stopping case would be 1.

    What do you think?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: What are the base case/stopping case for these loop and recursive methods?

    Quote Originally Posted by csanch11 View Post
    For the loop method it is true that the stopping case is n < 2 if the parameter variable is 0 or 1 to start with because the first thing my code checks is if n is equal to either 0 or 1. If n is not initially 0 or 1, then it seems to me that the stopping case is exactly 0, since the program will exit the while loop because 0 >= 1 will be false and the factorial will be returned.

    The same goes for my recursive method, the stopping case is n < 2 if the parameter variable is 0 or 1 to start. Although if n is not initially 0 or 1, then the stopping case would be 1.

    What do you think?
    I agree, but if you replace line #7 in your first example with the equivalent 'while (n > 1) ', you'll have only one stop condition: n < 2. Multiplying by one in the loop body (in your version) doesn't do much ...

    kind regards,

    Jos
    csanch11 likes this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. karel the robot - are methods case sensitive?
    By Roee Yossef in forum New To Java
    Replies: 1
    Last Post: 11-07-2011, 08:23 AM
  2. Replies: 1
    Last Post: 09-09-2011, 06:57 PM
  3. While loop with case - getting error
    By jmanv888 in forum New To Java
    Replies: 3
    Last Post: 07-20-2011, 07:13 AM
  4. Java, i need help with a recursion base case please
    By farahm in forum Advanced Java
    Replies: 1
    Last Post: 04-18-2011, 12:27 AM
  5. Help with while loop inside a switch case
    By Shesaid in forum New To Java
    Replies: 2
    Last Post: 04-01-2011, 03:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •