Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guiorgy/0f1ff03e564cead138ef02c0d1116135 to your computer and use it in GitHub Desktop.
Save Guiorgy/0f1ff03e564cead138ef02c0d1116135 to your computer and use it in GitHub Desktop.
prime.cpp
// prime calculation based on https://gist.github.com/rongjiecomputer/d52f34d27a21b8c9c9e82ca85b806640
// add increase limits or it wont compile for sieveSize = 1000000 -fconstexpr-loop-limit=2000000 -fconstexpr-ops-limit=335544320
// use debug build.
// release build will optimize most of the loop away.
#include <chrono>
#include <iostream>
#include <numeric>
#include <span>
#include <stdio.h>
using namespace std::chrono;
constexpr auto sieveSize = 1000000;
struct PrimeTable
{
constexpr
PrimeTable ()
: sieve ()
{
sieve[0] = sieve[1] = false;
for (size_t i = 2; i < sieveSize; i++)
sieve[i] = true;
for (size_t i = 2; i < sieveSize; i++)
{
if (sieve[i])
for (size_t j = i * i; j < sieveSize; j += 2*i)
sieve[j] = false;
}
}
bool sieve[sieveSize];
};
int
main ()
{
auto passes = 0;
auto tStart = steady_clock::now ();
constexpr auto tableCheck = PrimeTable{};
auto tableSpan = std::span{ tableCheck.sieve };
std::cout << "prim count: " << std::count (tableSpan.begin (), tableSpan.end (), true) << std::endl;
std::cout << "acutal primes : " << 78498 << std::endl;
while (duration_cast<seconds> (steady_clock::now () - tStart).count () < 5)
{
constexpr auto table = PrimeTable{};
if (table.sieve[0] == false) passes++;
}
std::cout << "passes: " << passes << std::endl;
std::cout << "duration: " << duration_cast<seconds> (steady_clock::now () - tStart).count () << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment