It would be unwise to separately compute 100! and 97! and then do the division.
Any recursive function can be written as an iterative function (and vise versa). Here is the math-like definition of recursion (again):
factorial( 0 ) = 1 factorial( N ) = N * factorial( N-1 )
And here is an iterative implementation:
int factorial( int N ) { int product = 1; for ( int j=1; j<=N; j++ ) product *= j; return product; }
Of course, this version suffers from the same overflow problem as the
recursive version.
For any argument larger than 12 the product is too large to hold
in an int
and incorrect values will be returned.
What does this version compute if given a parameter of -1?