-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
133 lines (115 loc) · 3.21 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <chrono>
#include <thread>
#include <algorithm>
#include <numeric>
#include <mutex>
using namespace std;
using namespace std::chrono;
using namespace std::this_thread;
uint64_t total;
mutex mtx;
/*void write(std::vector<int> v) {
std::ofstream file;
file.open("../text.txt");
for(int i=0;i<v.size();++i) {
file<<v[i]<<std::endl;
}
file.close();
}*/
/*
vector<int> read() {
ifstream fin("../primes.txt");
vector<int> data;
int element;
while (fin >> element){
data.push_back(element);
}
return data;
}
*/
vector<uint64_t> squareGen(uint64_t n) {
vector<uint64_t> returnSquares;
for (uint64_t i = 1; i*i-1 < n; i++) {
returnSquares.push_back(i*i);
}
return returnSquares;
}
uint64_t maxDivisiblePerfectSquare(uint64_t n, vector<uint64_t> squaresList) {
long double sqrtOfN;
sqrtOfN = sqrt(n);
vector<uint64_t> workingSquares;
for (uint64_t i : squaresList) {
if (n % i == 0) {
workingSquares.push_back(i);
}
}
return workingSquares.back();
}
void threadObject(uint64_t threadI, uint64_t n, vector<uint64_t> squares) {
for(uint64_t i = threadI; i < n+1; i++) {
auto start = high_resolution_clock::now();
uint64_t b = maxDivisiblePerfectSquare(i, squares);
auto end = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(end - start);
mtx.lock();
cout << i << " " << duration.count() << endl;
total += b;
mtx.unlock();
}
}
int main() {
auto start = high_resolution_clock::now();
uint64_t threadN;
uint64_t extra;
uint64_t bigN;
vector<uint64_t> squares;
bigN = 1000000;
// generate squares
squares = squareGen(bigN);
// divide by 12 for the twelve threads
threadN = bigN / 12;
extra = bigN % 12;
// start the threads
thread t0 (threadObject, threadN*0 +1, threadN, squares);
thread t1 (threadObject, threadN*1 +1, threadN*2, squares);
thread t2 (threadObject, threadN*2 +1, threadN*3, squares);
thread t3 (threadObject, threadN*3 +1, threadN*4, squares);
thread t4 (threadObject, threadN*4 +1, threadN*5, squares);
thread t5 (threadObject, threadN*5 +1, threadN*6, squares);
thread t6 (threadObject, threadN*6 +1, threadN*7, squares);
thread t7 (threadObject, threadN*7 +1, threadN*8, squares);
thread t8 (threadObject, threadN*8 +1, threadN*9, squares);
thread t9 (threadObject, threadN*9 +1, threadN*10, squares);
thread t10 (threadObject, threadN*10+1, threadN*11, squares);
thread t11 (threadObject, threadN*11+1, threadN*12, squares);
// wait for them to finish
t0 .join();
t1 .join();
t2 .join();
t3 .join();
t4 .join();
t5 .join();
t6 .join();
t7 .join();
t8 .join();
t9 .join();
t10.join();
t11.join();
// left overs
for(uint64_t i = threadN*12+1; i < threadN*12+extra+1; i++) {
uint64_t b = maxDivisiblePerfectSquare(i, squares);
total += b;
}
auto end = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(end - start);
cout << "total = " << total << endl;
cout << "total duration " << duration.count() << endl;
return 0;
}