Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<unsigned p, unsigned d>
- struct DoIsPrime {
- static constexpr bool value = (p%d != 0) && DoIsPrime<p,d-1>::value;
- };
- template<unsigned p>
- struct DoIsPrime<p, 2> {
- static constexpr bool value = (p % 2 != 0);
- };
- template<unsigned p>
- struct IsPrime {
- static constexpr bool value = DoIsPrime < p, p / 2 >::value;
- };
- template<>
- struct IsPrime<0> {
- static constexpr bool value = false;
- };
- template<>
- struct IsPrime<1> {
- static constexpr bool value = false;
- };
- template<>
- struct IsPrime<2> {
- static constexpr bool value = true;
- };
- template<>
- struct IsPrime<3> {
- static constexpr bool value = true;
- };
- template<unsigned p>
- bool isPrime = IsPrime<p>::value;
- unsigned int p[] = { }
- template<::std::uint32_t value_upper_bound> constexpr auto
- Collect_Primes_Impl(void)
- {
- static_assert(0 < value_upper_bound);
- ::std::array<::std::uint32_t, value_upper_bound> primes{};
- primes[0] = 1;
- ::std::uint32_t primes_count{1};
- ::std::uint32_t value{1};
- while(value_upper_bound != value)
- {
- ++value;
- bool is_prime{true};
- ::std::uint32_t prime_index{1};
- while(primes_count != prime_index)
- {
- if(0 == (value % primes[prime_index]))
- {
- is_prime = false;
- break;
- }
- ++prime_index;
- }
- if(is_prime)
- {
- primes[primes_count] = value;
- ++primes_count;
- }
- }
- return ::std::make_pair(primes_count, primes);
- }
- template<::std::uint32_t value_upper_bound> constexpr auto
- Collect_Primes(void)
- {
- static_assert(0 < value_upper_bound);
- constexpr const auto collected{Collect_Primes_Impl<value_upper_bound>()};
- ::std::array<::std::uint32_t, collected.first> primes{};
- ::std::uint32_t prime_index{0};
- while(collected.first != prime_index)
- {
- primes[prime_index] = collected.second[prime_index];
- ++prime_index;
- }
- return primes;
- }
- constexpr const auto pr20{Collect_Primes<20>()};
- static_assert(::std::size_t{9} == pr20.size());
- static_assert(::std::uint32_t{ 1} == pr20[0]);
- static_assert(::std::uint32_t{ 2} == pr20[1]);
- static_assert(::std::uint32_t{ 3} == pr20[2]);
- static_assert(::std::uint32_t{ 5} == pr20[3]);
- static_assert(::std::uint32_t{ 7} == pr20[4]);
- static_assert(::std::uint32_t{11} == pr20[5]);
- static_assert(::std::uint32_t{13} == pr20[6]);
- static_assert(::std::uint32_t{17} == pr20[7]);
- static_assert(::std::uint32_t{19} == pr20[8]);
Add Comment
Please, Sign In to add comment