00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef META_MATH_IS_PRIME_INCLUDE
00013 #define META_MATH_IS_PRIME_INCLUDE
00014
00015 #include <boost/mpl/bool.hpp>
00016 #include <boost/numeric/meta_math/sqrt.hpp>
00017 #include <boost/config/concept_macros.hpp>
00018 #ifdef __GXX_CONCEPTS__
00019 # include <concepts>
00020 #endif
00021
00022
00023 namespace meta_math {
00024
00025 namespace mpl = boost::mpl;
00026
00027 namespace impl {
00028
00029
00030 template <long int x, long int max_odd>
00031 struct is_prime_to_max_odd
00032 {
00033 static bool const value = x % max_odd != 0
00034 && is_prime_to_max_odd<x, max_odd-2>::value;
00035 };
00036
00037
00038 template <long int x> struct is_prime_to_max_odd<x, 1> : mpl::true_ {};
00039
00040
00041
00042
00043 template <long int x>
00044 struct max_prime_compare
00045 {
00046 static long int const tmp = sqrt<x>::value,
00047 value = tmp % 2 == 0 ? tmp + 1 : tmp + 2;
00048 };
00049
00050
00051
00052
00053
00054 template <long int x, bool Disable>
00055 struct check_odd
00056 {
00057 static bool const value = is_prime_to_max_odd<x, max_prime_compare<x>::value>::value;
00058 };
00059
00060
00061 template <long int x>
00062 struct check_odd<x, true>
00063 {
00064 static bool const value = false;
00065 };
00066
00067 }
00068
00069 template <long int x>
00070 struct is_prime
00071 {
00072 static bool const value = impl::check_odd<x, x % 2 == 0>::value;
00073 };
00074
00075 template <> struct is_prime<0> : mpl::false_ {};
00076 template <> struct is_prime<1> : mpl::false_ {};
00077 template <> struct is_prime<2> : mpl::true_ {};
00078 template <> struct is_prime<3> : mpl::true_ {};
00079 template <> struct is_prime<5> : mpl::true_ {};
00080
00081
00082 #ifdef __GXX_CONCEPTS__
00083 concept Prime<long int N> {}
00084
00085 template <long int N>
00086 where std::True<is_prime<N>::value>
00087 concept_map Prime<N> {}
00088 #endif
00089
00090
00091
00092 }
00093
00094 #endif // META_MATH_IS_PRIME_INCLUDE