init
This commit is contained in:
commit
00b08d7412
35
SieveOfEratosthenes.hpp
Normal file
35
SieveOfEratosthenes.hpp
Normal 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
41
main.cpp
Normal 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();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user