// timeFact.cpp
// P. Conrad, Spring 2004
// Time the factorial function


#include <iostream>
using std::cout;
using std::cerr;

#include "StopWatch.h"

int iterativeFact(int k); // compute factorial by iteration

int recursiveFact(int k); // compute factorial by iteration

int main(void)
{

  const int LIMIT=10;
  const int OUTERLIMIT=20000;

  StopWatch s;
  
  s.reset();
  s.start();

  int x;

  for (int j=1; j<=OUTERLIMIT; j++)
    for (int i=1; i<=LIMIT; i++)
      {
	x = iterativeFact(i);
      }
  
  s.stop();
  
  cout << "iterativeFact: Elapsed CPU time:" << s.readValue() << endl;


  s.reset();
  s.start();


  for (int j=1; j<=OUTERLIMIT; j++)
    for (int i=1; i<=LIMIT; i++)
      {
	x = recursiveFact(i);
      }
  
  s.stop();
  
  cout << "recursiveFact: Elapsed CPU time:" << s.readValue() << endl;


  return 0;
}



int iterativeFact(int k)  // compute factorial by iteration
{
  int product = 1;
  for (int i=1; i<=k; i++)
    product *= i;
  return product;
}


int recursiveFact(int k)  // compute factorial by iteration
{

  if (k==0)
    return 1;
  else
    return k * recursiveFact(k-1);

}

