#include <iostream>
#include "Stop_Watch.h"
using namespace std;

// GIVEN K AND x COMPUTE x^8/0! + x^8/1! + ... + x^8/K!
double f_alg0(double x, int k)
{
  double S = 0.0;
  for(int n = 1; n <= k; n++)
  {
    // Compute x^8
    double x8 = 1.0;
    for(int i = 0; i < 8; i++)    
      x8 = x8*x;
    
    // Compute n!
    double f = 1.0;
    for(int i = 1; i <= n; i++)
      f = f*i;
    
    // Add next term
    S += x8/f;
  }
  return S;
}

int main()
{
  // Get user input
  double x;
  int k, n;
  cout << "Enter x and k: ";
  cin >> x >> k;
  cout << "Test on microseconds (u) or milliseconds (m): ";
  char c;
  cin >> c;
  if (c == 'u')
    n = 1000000;
  else
    n = 1000;

  // Do the timing
  Stop_Watch S;
  S.Start();
  for(int i = 0; i < n; i++)
    f_alg0(x,k);
  S.Stop();
  cout << "f(" << x << ',' << k <<") = " << f_alg0(x,k) 
       << " which took " << S.Time() 
       << (c == 'u' ? " microseconds" : " milliseconds")
       << " to compute" << endl;
}
