1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <iostream>
template <bool b> struct bool_instant { constexpr static int value = b; };
using true_instant = bool_instant<true>; using false_instant = bool_instant<false>;
template <int... pack> struct intArrayPack { constexpr static int length = sizeof...(pack); constexpr static int value[sizeof...(pack)] = {pack...}; };
template <int N, int current = 2, int flag = 0> struct isPrime : isPrime<N, current + 1, !!!(N % current)>{};
template <int N> struct isPrime<N, N, 0> : true_instant{};
template <int N, int current> struct isPrime<N, current, 1> : false_instant{};
template <int N, int current = 2, bool flag = false, int... pack> struct primePack : primePack<N, current + 1, isPrime<current>::value, pack...>{};
template <int N, int current, int... pack> struct primePack<N, current, true, pack...> : primePack<N, current, false, current - 1, pack...>{};
template <int N, int... pack> struct primePack<N, N, false, pack...> : intArrayPack<pack...>{};
int main() { auto numlist = primePack<220>::value; for (auto i = 0; i < primePack<220>::length; i++) { std::cout << numlist[i] << " "; } return 0; }
|