This commit is contained in:
Dmitriy Shishkov 2022-05-25 00:23:48 +03:00
commit 00b08d7412
No known key found for this signature in database
GPG Key ID: 26720CB2A9608C97
2 changed files with 76 additions and 0 deletions

35
SieveOfEratosthenes.hpp Normal file
View File

@ -0,0 +1,35 @@
#include <array>
#include <array>
#include <cmath>
using namespace std;
template <typename U, typename F>
constexpr size_t c_div(U a, F b)
{
return static_cast<U>(a / b + 0.5);
};
template <size_t N>
class Primes
{
public:
constexpr Primes()
{
for (size_t i = 0; i < N; i++)
b[i] = 1;
for (size_t i = 2; i <= N; i++)
if (b[i-1])
for (size_t j = 2 * i; j <= N; j += i)
b[j-1] = 0;
for (size_t i = 1; i < N; i++)
if (b[i])
arr[n++] = i+1;
};
size_t n;
array<bool, N> b;
array<size_t, N> arr;
};

41
main.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <fstream>
#include <iostream>
#include <array>
#include <array>
// #include "SieveOfEratosthenes.hpp"
using namespace std;
template <size_t N>
class Primes
{
public:
constexpr Primes()
{
bool b[N]{0};
for (size_t i = 2; i <= N; i++)
if (!b[i-1])
for (size_t j = 2 * i; j <= N; j += i)
b[j-1] = 1;
for (size_t i = 1; i < N; i++)
if (!b[i])
arr[n++] = i+1;
};
size_t n = 0;
array<size_t, N> arr{};
};
int main () {
constexpr Primes< 262144U > primes;
ofstream out("out.txt");
for (size_t i = 0; i < primes.n; i++)
out << primes.arr[i] << endl;
out.close();
}