# Again recursion question

• 10-04-2010, 09:58 PM
luke
Again recursion question
Code:

```public class Neper_number {         static double factorial( int n ) {                 double result;                 if( n <= 0 )                         return 1;                 else {                         result = 1 + (n * factorial(n - 1));                         return result;                 } } }```
I am trying to do recursion in order to solve a problem. Since my original code is not working correctly (I am not doing the recursion correctly) I decided to do recursion but for a simple problem in order to understand what exactly is going on.
With the above code I thought that for example if I input n = 2 I would get
result = 1 + 2! = 3; if n = 3 I would get result 1 + 3! = 1 + 3x2x1 = 7 and so on. But instead when I input n = 2 I am getting result = 5, when n = 3 I am getting result = 16 etc.
Does anybody know why that is?
• 10-04-2010, 10:03 PM
Zack
Code:

`result = [color=red]1 + [/color](n * factorial(n - 1));`
• 10-04-2010, 10:12 PM
luke
Quote:

Originally Posted by Zack
Code:

`result = [color=red]1 + [/color](n * factorial(n - 1));`

I need to add it. I thought that since
Code:

`result = n * factorial(n - 1);`
gives me result = n! (factorial)

Code:

`result = [color=red]1 + [/color](n * factorial(n - 1));`
would give me result = 1 + n! but it's not. Why?
• 10-04-2010, 10:17 PM
Zack
Firstly, factorial never adds. 7! is 5040, not 5041. 3! is 6, not 7.

If you want the result of 1+!n, you cannot add it in that location because it's recursive (it will repeat this action as well as the others).

Work it out step-by-step using 4 as a value:
Code:

```factorial(4) -> result = 1 + (4 * factorial(3)) factorial(3) -> result = 1 + (3 * factorial(2)) factorial(2) -> result = 1 + (2 * factorial(1)) factorial(1) -> result = 1 + (1 * factorial(0)) factorial(0) -> return 1;```
Code:

```Simplified, that's: factorial(4) = 1 + (4 * (1 + 3 * (1 + 2 * (1 + (1 * 1))))); factorial(4) = 1 + (4 * (1 + 3 * (1 + 2 * (1 + 1)))); factorial(4) = 1 + (4 * (1 + 3 * (1 + 2 * 2))); factorial(4) = 1 + (4 * (1 + 3 * (1 + 4))); factorial(4) = 1 + (4 * (1 + 3 * 5)); factorial(4) = 1 + (4 * 16); factorial(4) = 1 + 64; factorial(4) = 65; // Should be 1+4! == 1 + 4*3*2*1 == 1 + 24 == 25```
See how the "1 +" recurs over and over? That's where the problem is. Instead of doing that, you'd want something like:
Code:

```public static double FactorialPlusOne(int n) {     return 1 + factorial(n); }```
Since this is not recursive, 1 will only be added once.
• 10-04-2010, 11:30 PM
luke
Quote:

Originally Posted by Zack
Firstly, factorial never adds. 7! is 5040, not 5041. 3! is 6, not 7.

If you want the result of 1+!n, you cannot add it in that location because it's recursive (it will repeat this action as well as the others).

Work it out step-by-step using 4 as a value:
Code:

```factorial(4) -> result = 1 + (4 * factorial(3)) factorial(3) -> result = 1 + (3 * factorial(2)) factorial(2) -> result = 1 + (2 * factorial(1)) factorial(1) -> result = 1 + (1 * factorial(0)) factorial(0) -> return 1;```
Code:

```Simplified, that's: factorial(4) = 1 + (4 * (1 + 3 * (1 + 2 * (1 + (1 * 1))))); factorial(4) = 1 + (4 * (1 + 3 * (1 + 2 * (1 + 1)))); factorial(4) = 1 + (4 * (1 + 3 * (1 + 2 * 2))); factorial(4) = 1 + (4 * (1 + 3 * (1 + 4))); factorial(4) = 1 + (4 * (1 + 3 * 5)); factorial(4) = 1 + (4 * 16); factorial(4) = 1 + 64; factorial(4) = 65; // Should be 1+4! == 1 + 4*3*2*1 == 1 + 24 == 25```
See how the "1 +" recurs over and over? That's where the problem is. Instead of doing that, you'd want something like:
Code:

```public static double FactorialPlusOne(int n) {     return 1 + factorial(n); }```
Since this is not recursive, 1 will only be added once.

Thanks a lot for the help, Zack. I got it.
Now I have to try to do the more complicated recursion. I want to try it myself before asking you :)
By the way how can I change the status of a post to [SOLVED] ?
• 10-05-2010, 12:02 AM
Zack