Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Factorial

 

Math code snippet to calculate a factorial.

There are multiple ways to calculate a factorial:

  1. A plain function (best)
  2. A recursive function
  3. The algorithm way
  4. A template metaprogram

 

 

 

 

 

plain function

 

Using a for-loop withing the function.

 

#include <cassert>

int Factorial(const int n)
{
  assert(n>=0);
  int result = 1;
  for (int i=1; i<=n; ++i)
  {
    result*=i;
  }
  return result;
}

int main()
{
  assert(Factorial(0)==1);
  assert(Factorial(1)==1);
  assert(Factorial(2)==2);
  assert(Factorial(3)==6);
  assert(Factorial(4)==24);
  assert(Factorial(5)==120);
}

 

 

 

 

 

Recursive function

 

#include <cassert>

int Factorial(const int n)
{
  assert(n >= 0);
  if (n==0) return 1;
  return (n * Factorial(n-1));
}

int main()
{
  assert(Factorial(0)==1);
  assert(Factorial(1)==1);
  assert(Factorial(2)==2);
  assert(Factorial(3)==6);
  assert(Factorial(4)==24);
  assert(Factorial(5)==120);
}

 

 

 

 

 

The algorithm way

 

The code belows shows a way how to use algorithms to calculate a factorial. This is not the preferred option.

 

#include <functional>
#include <vector>
#include <algorithm
#include <numeric>

//From http://www.richelbilderbeek.nl/CppFunctorIncrease.htm
struct Increase : public std::unary_function<void,int>
{
  explicit Increase(const int& initValue = 0) : mValue(initValue) {}
  void operator()(int& anything)
  {
    anything = mValue;
    ++mValue;
  }
  private:
  int mValue;
};

//From http://www.richelbilderbeek.nl/CppGetFactorialTerms.htm
const std::vector<int> GetFactorialTerms(const int n)
{
  std::vector<int> v(n);
  std::for_each(v.begin(), v.end(),Increase(1));
  return v;
}

//From http://www.richelbilderbeek.nl/CppFactorial.htm
int Factorial(const int n)
{
  const std::vector<int> v = FactorialTerms(n);
  return std::accumulate(v.begin(),v.end(),1,std::multiplies<int>());
}

#include <cassert>
#include <iostream>

int main()
{
  assert(Factorial(0)==1);
  assert(Factorial(1)==1);
  assert(Factorial(2)==2);
  assert(Factorial(3)==6);
  assert(Factorial(4)==24);
  assert(Factorial(5)==120);

  std::cout << "Program finished successsfully" << std::endl;
}

 

 

 

 

 

Template metaprogram

 

With compile-time checking using a template version of assert.

 

template <bool>
struct CtAssert;

template <>
struct CtAssert<true> {};

//The template metaprogram for factorial
template <unsigned int N>
struct factorial
{
  static unsigned const value = N * factorial<N-1>::value;
};

template <>
struct factorial<0>
{
  static unsigned const value = 1;
};

int main()
{
  CtAssert<(factorial<0>::value==1)>();
  CtAssert<(factorial<1>::value==1)>();
  CtAssert<(factorial<2>::value==2)>();
  CtAssert<(factorial<3>::value==6)>();
  CtAssert<(factorial<4>::value==24)>();
  CtAssert<(factorial<5>::value==120)>();
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict