1. Member Join Date
Sep 2010
Posts
62
Rep Power
0

## Again recursion question

Java 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?  Reply With Quote

2. ## Java Code:
`result = [color=red]1 + [/color](n * factorial(n - 1));`  Reply With Quote

3. Member Join Date
Sep 2010
Posts
62
Rep Power
0

##  Originally Posted by Zack Java Code:
`result = [color=red]1 + [/color](n * factorial(n - 1));`
I need to add it. I thought that since
Java Code:
`result = n * factorial(n - 1);`
gives me result = n! (factorial)

Java Code:
`result = [color=red]1 + [/color](n * factorial(n - 1));`
would give me result = 1 + n! but it's not. Why?  Reply With Quote

4. ## 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:
Java 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;```
Java 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:
Java Code:
```public static double FactorialPlusOne(int n) {
return 1 + factorial(n);
}```
Since this is not recursive, 1 will only be added once.  Reply With Quote

5. Member Join Date
Sep 2010
Posts
62
Rep Power
0

##  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:
Java 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;```
Java 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:
Java 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] ?  Reply With Quote

6. ##   Reply With Quote