# (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 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 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 #include #include //From http://www.richelbilderbeek.nl/CppFunctorIncrease.htm struct Increase : public std::unary_function {   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 GetFactorialTerms(const int n) {   std::vector 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 v = FactorialTerms(n);   return std::accumulate(v.begin(),v.end(),1,std::multiplies()); } #include #include 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 struct CtAssert; template <> struct CtAssert {}; //The template metaprogram for factorial template struct factorial {   static unsigned const value = N * factorial::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)>(); } ``` 