Advertisement
Guest User

Factorials

a guest
May 29th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <boost/mpl/int.hpp>
  4. #include <boost/mpl/for_each.hpp>
  5. #include <boost/mpl/push_front.hpp>
  6. #include <boost/mpl/list.hpp>
  7. #include <boost/mpl/transform.hpp>
  8. #include <boost/mpl/vector.hpp>
  9. #include <boost/mpl/push_back.hpp>
  10. #include <boost/mpl/multiplies.hpp>
  11. #include <boost/mpl/range_c.hpp>
  12. #include <boost/mpl/back.hpp>
  13.  
  14. namespace mpl = boost::mpl;
  15. using namespace mpl;
  16. const int border = 15;
  17.  
  18. typedef range_c<int, 1, border> nums;
  19. typedef vector<int_<1>> data;
  20.  
  21. typedef mpl::fold<
  22.     nums, data,
  23.     mpl::push_back<_1, mpl::multiplies<_2, back<_1>>>
  24. >::type Factorials;
  25.  
  26.  
  27. // Print result sequence
  28. template <class T> struct wrap { };
  29. struct print_value
  30. {
  31.     template <class T>
  32.     void operator () (wrap <T>) const {
  33.         std::cout
  34.             << T::value << std::endl;
  35.     }
  36. };
  37. template <class TSeq> void print_values()
  38. {
  39.     mpl::for_each <TSeq, wrap <_> >(print_value());
  40. }
  41.  
  42. int main() {
  43.     std::cout << "Factorials from 0 to " << std::to_string(border) << std::endl;
  44.     print_values<Factorials>();
  45.     system("pause");
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement