-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollatzthread.cpp
More file actions
40 lines (32 loc) · 1.11 KB
/
collatzthread.cpp
File metadata and controls
40 lines (32 loc) · 1.11 KB
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
#include "collatzthread.h"
#include <QDebug>
#include <chrono>
CollatzThread::CollatzThread(int start, int end, std::atomic<int64_t>* maxSteps, std::atomic<int64_t>* maxPos)
: startNumber(start), endNumber(end), maxsteps(maxSteps), maxpos(maxPos)
{
}
void CollatzThread::run()
{
int numberWithMaxChain = startNumber;
int maxChainLength = 0;
auto startTime = std::chrono::high_resolution_clock::now();
for (int number = startNumber; number <= endNumber; ++number) {
int steps = 1;
int64_t n = number;
while (n != 1) {
if (n % 2 == 0)
n /= 2;
else
n = 3 * n + 1;
++steps;
}
if (steps > maxChainLength) {
maxChainLength = steps;
numberWithMaxChain = number;
}
}
auto endTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = endTime - startTime;
long long timeInMillis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
emit resultReady(numberWithMaxChain, maxChainLength, timeInMillis);
}