#include <iostream>
#include <map>
#include <cstdint>
#include <chrono>
namespace SieveOfEratosthenes{//追記型エラトステネスの篩
//えーっと、Prime[2]=trueだけはどうやっても導き方がわからんのでハードコーディングで死守している。
static std::map < std::uint64_t, bool> Prime;
static bool IsInit = false;
#define ISDANGOMETHOD 1
std::uint64_t GetLastFound(){
if (IsInit == false){
Prime[2] = true;
Prime[3] = true;
IsInit = true;
}
return (*(Prime.rbegin())).first;
}
bool FindNextPrime(){
if (IsInit == false){
Prime[2] = true;
Prime[3] = true;
IsInit = true;
}
std::uint64_t i = GetLastFound() + 1;
bool F = true;
#if ISDANGOMETHOD
int sub[2] = { -1, 1 };
static int C = 0;
static int j = 1;
i = 6*j + sub[C++];
if (C == 2){
C = 0;
j++;
}
#endif
while (true){
F = true;
for (auto& o : Prime){
if (o.first > ((i / 2)+2)) break;
if (i%o.first == 0){
F = false;
break;
}
}
if (F == true){
Prime[i] = true;
return true;
}
#if ISDANGOMETHOD
i = 6*j + sub[C++];
if (C == 2){
C = 0;
j++;
}
//goto L;
#else
i++;
#endif
}
}
bool Clear(){
Prime.clear();
return true;
}
bool FindToCount(std::uint64_t N){
while (Prime.size() < N){
FindNextPrime();
}
return true;
}
bool IsPrime(std::uint64_t N){
while (GetLastFound() < N){
FindNextPrime();
}
auto it = Prime.find(N);
return (it != Prime.end());
}
}
int main(){
//bool B = SieveOfEratosthenes::IsPrime(10000);
//bool b2 = SieveOfEratosthenes::IsPrime(10007);
std::cout << "おぎゃー" << std::endl;
auto S=std::chrono::system_clock::now();
//bool b3 = SieveOfEratosthenes::IsPrime(65535);
SieveOfEratosthenes::FindToCount(10000);
auto E = std::chrono::system_clock::now();
auto Eli = std::chrono::duration_cast<std::chrono::milliseconds>(E - S);
std::cout << "経過時間は" << Eli.count() << "ミリ秒だ!" << std::endl;//17268ms on my pc.
return 0;
}