2016-11-30 13:32:06 +00:00
|
|
|
#include <iostream>
|
2016-11-30 20:08:44 +00:00
|
|
|
#include <omp.h>
|
2016-11-30 14:15:52 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <csignal>
|
2016-11-30 20:08:44 +00:00
|
|
|
#include <cassert>
|
2016-11-30 14:15:52 +00:00
|
|
|
|
|
|
|
static bool run;
|
|
|
|
|
|
|
|
void leave(int signum);
|
2016-11-30 13:32:06 +00:00
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
std::cout << "Indivisible v0.1\n";
|
2016-11-30 14:15:52 +00:00
|
|
|
run = true;
|
|
|
|
|
|
|
|
signal(SIGINT, leave);
|
|
|
|
|
|
|
|
std::vector<unsigned long long> primes;
|
|
|
|
primes.push_back(2);
|
|
|
|
unsigned long long num = 2;
|
|
|
|
|
2016-11-30 20:08:44 +00:00
|
|
|
// Use for to accomodate for OpenMP
|
|
|
|
#pragma omp parallel
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
unsigned long long myNum;
|
|
|
|
#pragma omp critical
|
|
|
|
{
|
|
|
|
myNum = num;
|
|
|
|
++num;
|
|
|
|
}
|
|
|
|
bool isPrime = true;
|
|
|
|
for(auto i : primes) {
|
|
|
|
if(i > myNum / 2) break;
|
|
|
|
if(myNum % i == 0) {
|
|
|
|
isPrime = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(isPrime) {
|
|
|
|
#pragma omp critical
|
|
|
|
{
|
|
|
|
primes.push_back(num);
|
|
|
|
std::cout << num << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(run);
|
2016-11-30 14:15:52 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 13:32:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-11-30 14:15:52 +00:00
|
|
|
|
|
|
|
void leave(int signum) {
|
|
|
|
std::cout << "Exiting (" << signum << ")\n";
|
|
|
|
run = false;
|
|
|
|
}
|