go to previous page   go to home page   go to next page

Answer:

100*99*98

It would be unwise to separately compute 100! and 97! and then do the division.


Iterative Implementation of Factorial

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.


QUESTION 8:

What does this version compute if given a parameter of -1?


go to previous page   go to home page   go to next page