#include <iostream>

#include <vector>

#include <cmath>

#include <fstream>

#include <iterator>

#include <string>

std::vector<unsigned long long> P(0);

int prime_pointer = 0;

void find_primes() {

int p_size = P.size();

bool loop;

int i;

for (unsigned long long test = P.back() + 2; P.size() == p_size;

test += 2) {

for (loop = true, i = 0; P[i] < sqrt(test) && loop == true;

i++) {

if (test % P[i] == 0) {

loop = false;

}

else if (P[i+1] > sqrt(test)) {

loop = false;

P.push_back(test);

}

}

}

}

void write_primes() {

std:

fstream primes("primes.txt",std::ios:

ut | std::ios::app);

for (int i = prime_pointer; i < P.size(); i++) {

primes << P[i] << std::endl;

}

prime_pointer = P.size();

}

void read_primes() {

std::ifstream primes("primes.txt");

if (!primes && P.size() == 0) {

P.push_back(2);

P.push_back(3);

}

else if (P.size() == 0) {

copy(std::istream_iterator<__int64>(primes),

std::istream_iterator<__int64>(), back_inserter(P));

prime_pointer = P.size();

}

}

void print_calc() {

std::cout << " Reading primes from file... ";

read_primes();

int e = 0;

double prob = 1;

if (P.size() > 2) {

while (pow(P[e],2) < P.back()) {

e++;

}

for (int i = 0; i < e; i++) {

prob -= prob*pow(P[i],-1);

}

}

std::cout << "Done!" << std::endl;

int periode_size;

int p_pointer;

system("cls");

for (bool loop = true; loop == true; e++) {

p_pointer = P.size();

periode_size = pow(P[e+1],2)-pow(P[e],2);

prob -= prob*pow(P[e],-1);

std::cout << "

-----------------------------------------------------------------------------

" << std::endl;

std::cout << " Testing interval: " << P[e] << "^2 -> " <<

P[e+1] << "^2 - 1" << std::endl;

std::cout << " Estimated number of primes: " <<

floor(periode_size*prob) << std::endl;

while (P.back() < pow(P[e+1],2)) {

find_primes();

}

P.pop_back();

std::cout << " Primes found: " << P.size()-p_pointer <<

std::endl;

std::cout << " Uncertainty: " <<

((floor(periode_size*prob)-(P.size()-p_pointer))/periode_size)*100 <<

"%" << std::endl;

write_primes();

}

}

void documentation () {

system("cls");

std::cout << " Primesstat documentation" << std::endl;

std::cout << std::endl;

std::cout << " About" << std::endl;

std::cout << std::endl;

std::cout << " Some time ago i got an idea that, if it worked,

could estimate" << std::endl;

std::cout << " how many primesnumbers could be found in a given

interval." << std::endl;

std::cout << " In order to test if it worked as well as i thought,

i wrote" << std::endl;

std::cout << " this little piece of software." << std::endl;

std::cout << " In my oppinion it works fine and now i want to share

it with" << std::endl;

std::cout << " the world." << std::endl;

std::cout << std::endl;

std::cout << " The math" << std::endl;

std::cout << std::endl;

std::cout << " An interval allways follow the rule:" << std::endl;

std::cout << " An interval allways follow the rule:" << std::endl;

std::cout << " P(n)^2 -> P(n+1)^2 - 1" << std::endl;

std::cout << " eg. 2~2 -> 3~2 - 1" << std::endl;

std::cout << " NB. This interval is the size of 5, not 4." <<

std::endl;

std::cout << std::endl;

std::cout << " The maximum number of primes an interval can contain

is tricky" << std::endl;

std::cout << " but can be explained with a couple of examples." <<

std::endl;

std::cout << " 2^2 -> 3^2 - 1 = (3^2 - 2^2) * (2^-1)" << std::endl;

std::cout << " 3^2 -> 5^2 - 1 = (5^2 - 3^2) * (2^-1 - (2^-1 *

3^-1))" << std::endl;

std::cout << " 5^2 -> 7^2 - 1 = (7^2 - 5^2) * ((2^-1 - (2^-1 *

3^-1)) - (->)" << std::endl;

std::cout << " ((2^-1 - (2^-1 * 3^-1)) * 5^-1))" << std::endl;

std::cout << " The result should be round down to the nearest

integer." << std::endl;

std::cout << " It is somewhat tricky, but it works." << std::endl;

std::cout << std::endl;

std::cout << " The uncertainty in % is simply found:" << std::endl;

std::cout << " ((estimated number of primes - actual number of

primes) / (->)" << std::endl;

std::cout << " periode size) * 100" << std::endl;

std::cout << std::endl;

std::cout << " Troubleshooting" << std::endl;

std::cout << std::endl;

std::cout << " The program will automaticly write the primes found

to the" << std::endl;

std::cout << " file 'primes.txt', this file will grow rapitly as

more and" << std::endl;

std::cout << " more primes are found, otherwise the program will

have to" << std::endl;

std::cout << " start all over every time it is restarted. If it is

a problem" << std::endl;

std::cout << " having such a big file lying around, it can without

any" << std::endl;

std::cout << " problems be deleted." << std::endl;

std::cout << std::endl;

std::cout << " Another problem is that the program writes to the

file every" << std::endl;

std::cout << " time it is done testing an interval, therefor, if

you want to" << std::endl;

std::cout << " quit the program, do so right after it has started

testing a" << std::endl;

std::cout << " new interval." <<std::endl;

std::cout << std::endl;

std::cin.get();

std::cout << " Press enter to return to menu..." << std::endl;

std::cin.get();

}

void menu() {

std::string choice;

system("cls");

std::cout <<

"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄ ÄÄ¿"

<< std::endl;

std::cout << "³1. Start testing³2. View documentation³3. Quit³"

<< std::endl;

std::cout <<

"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄ ÄÄÙ"

<< std::endl;

std::cout << std::endl;

std::cout << " Make a choice: ";

std::cin >> choice;

if (choice == "1") {

print_calc();

}

else if (choice == "2") {

documentation();

menu();

}

else if (choice == "3") {

//quit();

}

else {

std::cout << " Invalid choice!" << std::endl;

std::cout << " Make a new choice: ";

std::cin >> choice;

}

}

int main() {

menu();

}