#include <stdio.h>
#include <assert.h>

typedef int Integer;

// recursive factorial
Integer rfact(Integer n)
{
    assert(n >= 0);
    if (n == 0) 
      return 1;
    else
      return (n * rfact(n-1));
}

// iterative factorial
Integer ifact(Integer n)
{
    Integer m = 1;
    while (n > 0) m *= n--;
    return m;
}

// tail recursive factorial
// call with m=1
Integer tfact(Integer n, Integer m)
{
    if (n == 1 || n == 0) 
      return m; 
     else /* recurse with m *= n-- */
      return tfact(n-1, m*n);
}

Integer fact(Integer n) { return tfact(n,1); }

int main()
{
   int i;

   for (i = 0; i <= 15; ++i) {	
	printf("rfact %ld = %ld\n", i, rfact(i));
	printf("ifact %ld = %ld\n", i, ifact(i));
	printf("tfact %ld = %ld\n", i, fact(i));
   }

   printf("fact %ld = %ld\n", 100, fact(100));
}
