Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) GetFactorialTerms

 

Math code snippet to obtain the product terms of a factorial. One can use this approach with DivideTerms to calculate the division of large factorials.

 

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

//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;
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict