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

1. Member
Join Date
Oct 2013
Posts
63
Rep Power
0

## 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. ## Re: What are the base case/stopping case for these loop and recursive methods?

Can you post your reasons for making those choices?

3. Member
Join Date
Oct 2013
Posts
63
Rep Power
0

## 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. Tim Cooke
Join Date
Jul 2014
Location
Belfast
Posts
101
Rep Power
0

5. Member
Join Date
Oct 2013
Posts
63
Rep Power
0

## 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. Member
Join Date
Oct 2013
Posts
63
Rep Power
0

## 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. Tim Cooke
Join Date
Jul 2014
Location
Belfast
Posts
101
Rep Power
0

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

Originally Posted by csanch11
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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

9. Member
Join Date
Oct 2013
Posts
63
Rep Power
0

## 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. Member
Join Date
Oct 2013
Posts
63
Rep Power
0

## 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. ## Re: What are the base case/stopping case for these loop and recursive methods?

Originally Posted by csanch11
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

12. Member
Join Date
Oct 2013
Posts
63
Rep Power
0

## 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. ## Re: What are the base case/stopping case for these loop and recursive methods?

Originally Posted by csanch11
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

#### Posting Permissions

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