The following code example is taken from the book
C++20 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2021
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <iostream>
#include <array>
constexpr
bool isPrime(int value)
{
for (int i = 2; i <= value/2; ++i) {
if (value % i == 0) {
return false;
}
}
return value > 1; // 0 and 1 are not prime numbers
}
template<int Num>
consteval
std::array<int, Num> primeNumbers()
{
std::array<int, Num> primes;
int idx = 0;
for (int val = 1; idx < Num; ++val) {
if (isPrime(val)) {
primes[idx++] = val;
}
}
return primes;
}
int main()
{
// init with prime numbers:
auto primes = primeNumbers<100>();
for (auto v : primes) {
std::cout << v << '\n';
}
}