Eine gute Antwort wäre:

Yes. No matter what you are computing, if there is a recursive way to do it, then there is an iterative way to do it. And if there is an iterative way to do it, then there is a recursive way.

Iterative Implementation of Factorial

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 to the caller.


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