-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03bcaf9
commit be3a71c
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <cstdint> | ||
#include <climits> | ||
#include <cmath> | ||
#include <iostream> | ||
#include <fstream> | ||
#include <list> | ||
|
||
#define PRIMES_FILENAME "primes.txt" | ||
#define PRIME_TYPE uint64_t | ||
#define PRIME_LIMIT ULLONG_MAX // > 18e18 | ||
|
||
// SIGINT catcher | ||
// Credit: https://stackoverflow.com/questions/13010531/leaving-an-infinite-while-loop-in-c | ||
//#include <unistd.h> | ||
#include <csignal> | ||
volatile sig_atomic_t stop; | ||
void inthand(int signum) { | ||
stop = 1; | ||
} | ||
|
||
std::list<PRIME_TYPE> primes; | ||
std::fstream primesFile; | ||
|
||
void addPrime(PRIME_TYPE prime) { | ||
if (primes.size() % 100000 == 0) std::cout << primes.size() << "th prime: " << prime << "\n"; | ||
primesFile << prime << "\n"; | ||
primes.push_back(prime); | ||
} | ||
|
||
int main() { | ||
std::cout << "Started!\n"; | ||
PRIME_TYPE n = 0; | ||
|
||
// Open primes file for reading | ||
std::cout << "Opening " << PRIMES_FILENAME << "...\n"; | ||
primesFile.open(PRIMES_FILENAME, std::ios::in | std::ios::out | std::ios::app); | ||
|
||
// Read in the current list of primes | ||
if (primesFile.is_open()) { | ||
while (true) { | ||
primesFile >> n; | ||
if (primesFile.eof()) break; | ||
primes.push_back(n); | ||
} | ||
} else { | ||
primesFile.open(PRIMES_FILENAME, std::ios::out); | ||
addPrime(2); | ||
addPrime(3); | ||
n = 3; | ||
} | ||
std::cout << "Primes loaded.\n"; | ||
primesFile.close(); | ||
|
||
// Open primes file for writing | ||
primesFile.open(PRIMES_FILENAME, std::ios::out | std::ios::app); | ||
|
||
// Loop to generate more primes, until ctrl-C (SIGINT) is given | ||
std::cout << "Searching for primes... (do ctrl-C to stop)\n"; | ||
signal(SIGINT, inthand); | ||
while (!stop && n < PRIME_LIMIT) { | ||
n += 2; | ||
bool n_is_prime = true; | ||
|
||
for (PRIME_TYPE prime : primes) { | ||
if (prime > sqrt(n)) break; | ||
|
||
if (n % prime == 0) { // n is divisible by prime | ||
n_is_prime = false; | ||
break; | ||
} | ||
} | ||
|
||
if (n_is_prime) | ||
addPrime(n); | ||
} | ||
|
||
std::cout << "n = " << n << "\n"; | ||
std::cout << "Last prime found: " << primes.back() << "\n"; | ||
primesFile.close(); | ||
return 0; | ||
} |