then in the factorial function 3 is subtracted by 1 equaling 2, and then multiplied by 3 for a result of 6
No.
In the factorial function, 3 "is subtracted by 1" equalling 2, then this 2 is passed to factorial
in a recursive call:
factorial(2) * 3
= factorial(1) * 2 * 3
= 1 * 2 * 3
= 6
The result of this call is what is multiplied by 3 for a result of 6.
It's (factorial(n-1) * n)
, not ((n-1) * n)
.
However, when the program gets to num equaling 4 this doesn't seem to make any sense anymore, because the int result of the factorials() functions should equal 12 not 24.
If factorial(3)
is 6, then factorial(4)
clearly cannot be only 12 because that's only twice 6. It's not grown enough.
factorial(3) * 4
= factorial(2) * 3 * 4
= factorial(1) * 2 * 3 * 4
= 1 * 2 * 3 * 4
= 24
3 * 4 = 12, not 24
Correct, but that is not 4 factorial. 4 factorial is 1 * 2 * 3 * 4.
I suspect you're sort of half-confusing factorial with the fibonacci sequence.