-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiCalc.cpp
81 lines (73 loc) · 3.05 KB
/
PiCalc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <chrono>
#include "GMP/gmpxx.h"
#include "ChudnovskyPiBS.h"
#include "SubstringKMP.h"
#define OUTPUT_TXT_FILEPATH "./calculated_pi.txt"
int main()
{
int modeIntent = -1;
while (modeIntent != 1 && modeIntent != 2)
{
std::cout << "Available options:" << std::endl;
std::cout << "1 = Calculate Pi Decimal Expansion." << std::endl;
std::cout << "2 = Pattern Match (numbers from 1 to 10000)." << std::endl;
std::cout << "What would you like to do? [Enter 1 or 2]: ";
std::cin >> modeIntent;
}
if (modeIntent == 2)
{
return kmpMain();
}
///Configuration
std::cout << "Configuration started." << std::endl;
unsigned long digits; //= 100000000; //apart from 3. number of decimal places.
std::cout << "Enter the number of digits to calculate (excluding initial 3): ";
std::cin >> digits;
std::cout << "Recieved " << digits << "." << std::endl;
///Computing Pi
std::cout << "Starting computation." << std::endl;
auto start = std::chrono::high_resolution_clock::now();
ChudnovskyPiBS* piCalc = new ChudnovskyPiBS(digits);
mpz_class calculatedPi = piCalc->calculatePi();
auto stop = std::chrono::high_resolution_clock::now();
auto duration_MS = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
auto duration_S = std::chrono::duration_cast<std::chrono::seconds>(stop - start);
std::chrono::duration<double> elapsed_time = stop - start;
float execution_time = (float)elapsed_time.count();
size_t duration;
std::string suffix = "miliseconds";
if (duration_S.count() != 0)
{
duration = duration_S.count();
suffix = "seconds";
}
else
duration = duration_MS.count();
//std::cout << "Computed " << digits <<" (10^"<<log10l(digits) << ") digits (excluding the first digit ('3')) in " << duration << suffix << "." << std::endl << std::endl;
std::cout << "Computed " << digits << " (10^" << log10l(digits) << ") digits (excluding the first digit ('3')) in " << execution_time << "seconds" << "." << std::endl << std::endl;
///Writing to file:
std::cout << "Writing computed value to output file." << std::endl;
std::string calculatedPiStr = calculatedPi.get_str();
std::ofstream out(OUTPUT_TXT_FILEPATH);
out << calculatedPiStr;
out.close();
std::cout << "Wrote computed values to :" << OUTPUT_TXT_FILEPATH << "!" << std::endl << std::endl;
///Running simple tests
std::cout << "Running simple tests. You may press \"ctrl+c\" to cancel them at any time." << std::endl;
bool doesWork = (calculatedPiStr.length() == 1 + digits);
std::string testStatus = "FAILED";
if (doesWork)
{
testStatus = "passed (remember, tests are not exhaustive)";
}
std::cout << "Initial tests have " + testStatus + "." << std::endl << std::endl;
///Finished.
std::cout << "Program finished. Press any key to exit. ";
getch();
std::cout << std::endl;
return EXIT_SUCCESS;
}