Guest

Untitled

By: a guest on Apr 9th, 2011  |  syntax: C++  |  size: 1.43 KB  |  hits: 115  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. /*  Solves Project Euler problem 1
  2.     ------------------------------
  3.  
  4.     If we list all the natural numbers below 10 that are multiples of 3 or 5,
  5.     we get 3, 5, 6 and 9. The sum of these multiples is 23.
  6.  
  7.     Find the sum of all the multiples of 3 or 5 below 1000.
  8.  
  9.     James Brotchie - brotchie@gmail.com
  10. */
  11.  
  12. #include <iostream>
  13.  
  14. #include <boost/mpl/or.hpp>
  15. #include <boost/mpl/logical.hpp>
  16. #include <boost/mpl/modulus.hpp>
  17. #include <boost/mpl/arithmetic.hpp>
  18. #include <boost/mpl/int.hpp>
  19. #include <boost/mpl/equal_to.hpp>
  20. #include <boost/mpl/comparison.hpp>
  21. #include <boost/mpl/lambda.hpp>
  22. #include <boost/mpl/apply.hpp>
  23. #include <boost/mpl/if.hpp>
  24. #include <boost/mpl/less.hpp>
  25. #include <boost/mpl/plus.hpp>
  26. #include <boost/mpl/assert.hpp>
  27.  
  28. using namespace boost::mpl;
  29.  
  30. #define MAX_NUMBER 1000
  31.  
  32. typedef or_<
  33.     equal_to<modulus<_1, int_<5>>, int_<0>>,
  34.     equal_to<modulus<_1, int_<3>>, int_<0>>> condition;
  35.  
  36. template <int current, int sum>
  37. struct euler1 {
  38.     typedef typename apply<condition, int_<current>>::type divisible;
  39.     typedef typename euler1<
  40.         plus<int_<current>, int_<1>>::value,
  41.         if_<divisible,
  42.             plus<int_<sum>, int_<current>>,
  43.             int_<sum>>::type::value
  44.         >::type type;
  45. };
  46.  
  47. template<int sum>
  48. struct euler1<MAX_NUMBER, sum> {
  49.     typedef int_<sum> type;
  50. };
  51.  
  52. int main() {
  53.     std::cout << euler1<0, 0>::type::value << std::endl;
  54.     return 0;
  55. }