Data Structures, Algorithms, & Applications in C++
Chapter 2, Exercise 7

The nonrecursive function is in the file iterativeFactorial.cpp . The code is given below.


int factorial(int n)
{// Return n!.
   if (n <= 1)
      return 1;
   int fact = 2;
   for (int i = 3; i <= n; i++)
      fact *= i;
   return fact;
}



For this nonrecursive function, Sfactorial(n) = 0 while the space requirements of the recursive version are 8 * max{n, 1} (see Example 2.5). The nonrecursive version uses less space than its recursive counterpart.