#include <array>
#include <cstdio>
#include <cmath>
#include <fstream>
#include <iostream>
#include <chrono>
using namespace std;
// Simple prime checker for validation.
static bool isPrime(long val) {
const long MaxVal = sqrt(val) + 1;
if (val % 2 == 0) return (val == 2); // 2 is the only even prime
for (long i = 3; i <= MaxVal; i += 2) {
if (val % i == 0) return false;
}
return (val > 1); // 1 isn't prime
}
struct record {
void init(unsigned int Mult) {
Multiple = Mult;
Minus = Mult * 6 - 1;
Plus = Mult * 6 + 1;
}
unsigned int Multiple; // Multiple of 6
unsigned int Minus;
unsigned int Plus;
bool MinusIsPrime = true; // IsPrime Value for Col A
bool PlusIsPrime = true; // IsPrime Value for Col B
bool TwinIsPrime = true;
bool TwinPsuedoCheck = false;
bool &GetMinusIsPrime () { return MinusIsPrime; }
void SetMinusIsPrime () { MinusIsPrime=false; }
bool &GetPlusIsPrime () { return PlusIsPrime; }
void SetPlusIsPrime () { PlusIsPrime=false; }
unsigned int &GetMinus () { return Minus; }
unsigned int &GetPlus () { return Plus; }
};
constexpr unsigned int N = 1000; // Set to Test Range
constexpr unsigned int MaxMultiple = N / 6;
constexpr unsigned int StopValue = sqrt(MaxMultiple) + 1;
std::array<record, MaxMultiple> List; // Create the List of the Values of the Set of (6X-1) U (6X+1) to the Size N
int main ()
{
// Starting with the Subset ( 6X ± 1 ) arranged in pairs (5, 7) (11, 13) (17, 19) ... ( 6n - 1, 6n + 1)
// Col A = { 5, 11, 17, 23, 29, ... } and Col B = { 7, 13, 19, 25, 31, ... }
// Minus_Check, and Plus_Check are indicators of ( Possible Prime ) status, Default Value = Prime
// ----------------------------------------------------------------------------------
volatile typedef std::chrono::high_resolution_clock Time;
typedef std::chrono::duration<float> fsec;
typedef std::chrono::milliseconds ms;
typedef std::chrono::microseconds mu;
typedef std::chrono::nanoseconds ns;
auto t0 = chrono::steady_clock::now();
// ----------------------------------------------------------------------------------
for (unsigned int i = 1; i < List.size(); ++i) List[i].init(i);
volatile bool StartingSide = false; // indicator of which side of the 6X±1 of the First Record
volatile bool Side = false; // indicator of which side of the 6X±1
volatile bool CurrentFirstSide;
volatile unsigned int CurrentMultiple = 1;
volatile unsigned int CurrentOddNumber = 3;
do { // Outside Loop
volatile unsigned int CurrentRecord = CurrentMultiple; // Goto First Record for Current Possible Prime
volatile const unsigned int FirstStep = CurrentOddNumber; // First Multiple to Set IsPrime Value
volatile const unsigned int SecondStep = CurrentMultiple * 2; // Second Multiple to Set IsPrime Value
/*
volatile unsigned int CurrentFirstMinus = List[CurrentRecord].GetMinus();
volatile unsigned int CurrentFirstPlus = List[CurrentRecord].GetPlus();
*/
if (!StartingSide) {
Side=true;
CurrentFirstSide = List[CurrentRecord].GetMinusIsPrime ();
}
else {
Side=false;
CurrentFirstSide = List[CurrentRecord].GetPlusIsPrime ();
}
if (CurrentFirstSide) // if Current Value is Marked as Prime
{
do {
CurrentRecord += FirstStep; // First Step
std::cout << "CurrentRecord=" << CurrentRecord << " List.size()=" << List.size();
if (CurrentRecord >= List.size())
std::cout << " ** invalid **\n";
else
std::cout << "\n";
/*
volatile unsigned int CurrentMinus = List[CurrentRecord].GetMinus();
volatile unsigned int CurrentPlus = List[CurrentRecord].GetPlus();
*/
if (!Side)
List[CurrentRecord].SetMinusIsPrime (); // Set IsPrime Value for Col A
else
List[CurrentRecord].SetPlusIsPrime (); // Set IsPrime Value for Col B
if (Side)
Side=false;
else
Side=true;
CurrentRecord += SecondStep; // Second Step
if (!Side)
List[CurrentRecord].SetMinusIsPrime (); // Set IsPrime Value for Col A
else
List[CurrentRecord].SetPlusIsPrime (); // Set IsPrime Value for Col B
if (Side)
Side=false;
else
Side=true;
} while (CurrentRecord + FirstStep + SecondStep < MaxMultiple + 275);
} // if Current Value is Marked as Prime
if ( StartingSide ){
CurrentMultiple = CurrentMultiple + 1;
StartingSide = false;
}
else
StartingSide = true;
CurrentOddNumber += 2;
} while ( CurrentMultiple <= StopValue ); // Outside Loop
auto t1 = chrono::steady_clock::now();
fsec fs = t1 - t0;
ms d = std::chrono::duration_cast<ms>(fs);
mu e = std::chrono::duration_cast<mu>(fs);
ns f = std::chrono::duration_cast<ns>(fs);
int MyTime = e.count();
/*
std::cout << fs.count() << "s\n";
std::cout << d.count() << "ms\n";
*/
// Show List of Primes
ofstream myfile;
myfile.open ("list.txt");
unsigned int Total = 0; // Total Number of Primes Found
for (unsigned int i = 1; i < MaxMultiple; i++) {
const record &rec = List[i];
if (List[i].GetMinusIsPrime()){
myfile << List[i].GetMinus() << "\n";
Total += 1;
}
if (List[i].GetPlusIsPrime()){
myfile << List[i].GetPlus() << "\n";
Total += 1;
}
}
myfile << "Total Primes: " << Total << " " << MyTime << "mus\n";
myfile.close();
}