Created
December 15, 2014 10:19
-
-
Save LYP951018/d1336a143e0f4b9bee0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <type_traits> | |
| template<bool I,typename T1,typename T2> | |
| struct If | |
| { | |
| typedef T1 type; | |
| }; | |
| template<typename T1, typename T2> | |
| struct If<false,T1,T2> | |
| { | |
| typedef T2 type; | |
| }; | |
| template<int N,int I=0> | |
| struct Sqrt | |
| { | |
| typedef typename If < (I*I >= N), std::integral_constant<int, I>, Sqrt<N, I + 1>>::type type; | |
| static constexpr int value = type::value; | |
| }; | |
| template<int N,int I> | |
| struct Seive | |
| { | |
| typedef typename If<N % I, Seive<N, I + 1>, std::integral_constant<bool, false>>::type type; | |
| typedef typename If<(I > Sqrt<N>::value || I==N), std::integral_constant<bool, true>, type > ::type temptype; | |
| static constexpr bool value = temptype::value; | |
| }; | |
| template<int N,int I,int C> | |
| struct Process | |
| { | |
| typedef typename If<Seive<N, 2>::value, Process<N + 1, I + 1, C>, Process<N + 1, I, C>>::type type; | |
| static constexpr int result = type::result; | |
| }; | |
| template<int N,int C> | |
| struct Process<N,C,C> | |
| { | |
| static constexpr int result = N-1; | |
| }; | |
| template<int N> | |
| struct Primes | |
| { | |
| static constexpr int result = Process<2,0,N>::result; | |
| }; | |
| int main() | |
| { | |
| std::cout << Sqrt<5>::value; | |
| std::cout << Primes<20>::result; | |
| system("pause"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment