-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
89 lines (72 loc) · 1.98 KB
/
main.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
82
83
84
85
86
87
88
89
/*
* Notes
* Non-primes:
* - 6750399
*/
#include <iostream>
#include <chrono>
#include <vector>
#include <fstream>
#include "Factorer.h"
using namespace std;
using namespace chrono;
/*
* Generate random numbers that have from digit_min to digit_max
* digits, one number per digit-class. Filter prime numbers and those
* divisible by two.
*/
std::vector<mpz_class> gen_number_set() {
vector<mpz_class> set;
int digit_min = 5;
int digit_max = 50;
for (int i = digit_min; i <= digit_max; ) {
mpz_class candidate = Factorer::rand_digits(i);
if (!Factorer(candidate).is_prime() && !(candidate % 2 == 0)) {
set.push_back(candidate);
i++;
}
}
return set;
}
void print_number_set() {
std::vector<mpz_class> nums = gen_number_set();
for(auto& n : nums)
cout << n << ' ';
}
void time_fac_func(Factorer& factorer, mpz_class (Factorer::*function)() const) {
auto start = high_resolution_clock::now();
cout << (factorer.*function)();
cout << '\t';
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << duration.count() << '\t';
}
void profile(const vector<mpz_class>& nums) {
cout << "Number\tNaive Factor\tNaive microseconds\tPollard Factor\tPollard microseconds" << endl;
for(const mpz_class& n : nums) {
cout << n << '\t';
Factorer fac(n);
time_fac_func(fac, &Factorer::naive);
time_fac_func(fac, &Factorer::pollard);
cout << endl;
}
}
std::vector<mpz_class> load_nums() {
mpz_class to_add;
string read;
std::vector<mpz_class> ret;
ifstream infile("list2.txt");
if(!infile.is_open())
throw std::runtime_error("Could not open file.");
while (infile >> read) {
to_add.set_str(read, 10);
ret.push_back(to_add);
}
return ret;
}
int main() {
vector<mpz_class> nums = load_nums();
profile(nums);
// print_number_set();
return 0;
}