
Untitled
By: a guest on Apr 9th, 2011 | syntax:
C++ | size: 1.43 KB | hits: 115 | expires: Never
/* Solves Project Euler problem 1
------------------------------
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
James Brotchie - brotchie@gmail.com
*/
#include <iostream>
#include <boost/mpl/or.hpp>
#include <boost/mpl/logical.hpp>
#include <boost/mpl/modulus.hpp>
#include <boost/mpl/arithmetic.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/lambda.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/less.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/assert.hpp>
using namespace boost::mpl;
#define MAX_NUMBER 1000
typedef or_<
equal_to<modulus<_1, int_<5>>, int_<0>>,
equal_to<modulus<_1, int_<3>>, int_<0>>> condition;
template <int current, int sum>
struct euler1 {
typedef typename apply<condition, int_<current>>::type divisible;
typedef typename euler1<
plus<int_<current>, int_<1>>::value,
if_<divisible,
plus<int_<sum>, int_<current>>,
int_<sum>>::type::value
>::type type;
};
template<int sum>
struct euler1<MAX_NUMBER, sum> {
typedef int_<sum> type;
};
int main() {
std::cout << euler1<0, 0>::type::value << std::endl;
return 0;
}