@@ -1,82 +1,84 @@
|
|
1 |
#include <QtMath>
|
2 |
#include <QVector>
|
3 |
|
4 |
#include "GoldbachWorker.h"
|
5 |
|
6 |
-
GoldbachWorker::GoldbachWorker(long long number, QVector<QString>& results, QObject *parent)
|
7 |
: QThread{parent}
|
8 |
, number{number}
|
|
|
|
|
9 |
, results{results}
|
10 |
{
|
11 |
}
|
12 |
|
13 |
void GoldbachWorker::run()
|
14 |
{
|
15 |
long long sumCount = this->calculate(this->number);
|
16 |
emit calculationDone(sumCount);
|
17 |
}
|
18 |
|
19 |
void GoldbachWorker::updateProgress(int newPercent)
|
20 |
{
|
21 |
if ( this->progressPercent != newPercent )
|
22 |
{
|
23 |
this->progressPercent = newPercent;
|
24 |
emit this->progressUpdated( this->progressPercent );
|
25 |
}
|
26 |
}
|
27 |
|
28 |
long long GoldbachWorker::calculate(long long number)
|
29 |
{
|
30 |
if ( number < 4 || number == 5 ) return 0;
|
31 |
this->progressPercent = 0;
|
32 |
return number % 2 == 0 ? calculateEvenGoldbach(number) : calculateOddGoldbach(number);
|
33 |
}
|
34 |
|
35 |
long long GoldbachWorker::calculateEvenGoldbach(long long number)
|
36 |
{
|
37 |
long long results = 0;
|
38 |
for ( long long a = 2; a < number && this->isInterruptionRequested() == false; ++a )
|
39 |
{
|
40 |
this->updateProgress(static_cast<int>(100LL * a / (number - 1)));
|
41 |
|
42 |
if ( ! isPrime(a) ) continue;
|
43 |
long long b = number - a;
|
44 |
if ( b >= a && isPrime(b) )
|
45 |
this->results.append( tr("%1: %2 + %3").arg(++results).arg(a).arg(b) );
|
46 |
}
|
47 |
return results;
|
48 |
}
|
49 |
|
50 |
long long GoldbachWorker::calculateOddGoldbach(long long number)
|
51 |
{
|
52 |
long long results = 0;
|
53 |
for ( long long a = 2; a < number; ++a )
|
54 |
{
|
55 |
this->updateProgress( static_cast<int>(100LL * a / (number - 1)) );
|
56 |
|
57 |
if ( ! isPrime(a) ) continue;
|
58 |
for ( long long b = a; b < number; ++b )
|
59 |
{
|
60 |
if ( this->isInterruptionRequested() )
|
61 |
return results;
|
62 |
|
63 |
if ( ! isPrime(b) ) continue;
|
64 |
long long c = number - a - b;
|
65 |
if ( c >= b && isPrime(c) )
|
66 |
this->results.append( tr("%1: %2 + %3 + %4").arg(++results).arg(a).arg(b).arg(c) );
|
67 |
}
|
68 |
}
|
69 |
return results;
|
70 |
}
|
71 |
|
72 |
bool GoldbachWorker::isPrime(long long number)
|
73 |
{
|
74 |
if ( number < 2 ) return false;
|
75 |
|
76 |
long long last = static_cast<long long>( qSqrt( number) );
|
77 |
for ( long long i = 2; i <= last; ++i )
|
78 |
if ( number % i == 0 )
|
79 |
return false;
|
80 |
|
81 |
return true;
|
82 |
}
|
1 |
#include <QtMath>
|
2 |
#include <QVector>
|
3 |
|
4 |
#include "GoldbachWorker.h"
|
5 |
|
6 |
+
GoldbachWorker::GoldbachWorker(long long number, int workerId, int workerCount, QVector<QString>& results, QObject *parent)
|
7 |
: QThread{parent}
|
8 |
, number{number}
|
9 |
+
, workerId{workerId}
|
10 |
+
, workerCount{workerCount}
|
11 |
, results{results}
|
12 |
{
|
13 |
}
|
14 |
|
15 |
void GoldbachWorker::run()
|
16 |
{
|
17 |
long long sumCount = this->calculate(this->number);
|
18 |
emit calculationDone(sumCount);
|
19 |
}
|
20 |
|
21 |
void GoldbachWorker::updateProgress(int newPercent)
|
22 |
{
|
23 |
if ( this->progressPercent != newPercent )
|
24 |
{
|
25 |
this->progressPercent = newPercent;
|
26 |
emit this->progressUpdated( this->progressPercent );
|
27 |
}
|
28 |
}
|
29 |
|
30 |
long long GoldbachWorker::calculate(long long number)
|
31 |
{
|
32 |
if ( number < 4 || number == 5 ) return 0;
|
33 |
this->progressPercent = 0;
|
34 |
return number % 2 == 0 ? calculateEvenGoldbach(number) : calculateOddGoldbach(number);
|
35 |
}
|
36 |
|
37 |
long long GoldbachWorker::calculateEvenGoldbach(long long number)
|
38 |
{
|
39 |
long long results = 0;
|
40 |
for ( long long a = 2; a < number && this->isInterruptionRequested() == false; ++a )
|
41 |
{
|
42 |
this->updateProgress(static_cast<int>(100LL * a / (number - 1)));
|
43 |
|
44 |
if ( ! isPrime(a) ) continue;
|
45 |
long long b = number - a;
|
46 |
if ( b >= a && isPrime(b) )
|
47 |
this->results.append( tr("%1: %2 + %3").arg(++results).arg(a).arg(b) );
|
48 |
}
|
49 |
return results;
|
50 |
}
|
51 |
|
52 |
long long GoldbachWorker::calculateOddGoldbach(long long number)
|
53 |
{
|
54 |
long long results = 0;
|
55 |
for ( long long a = 2; a < number; ++a )
|
56 |
{
|
57 |
this->updateProgress( static_cast<int>(100LL * a / (number - 1)) );
|
58 |
|
59 |
if ( ! isPrime(a) ) continue;
|
60 |
for ( long long b = a; b < number; ++b )
|
61 |
{
|
62 |
if ( this->isInterruptionRequested() )
|
63 |
return results;
|
64 |
|
65 |
if ( ! isPrime(b) ) continue;
|
66 |
long long c = number - a - b;
|
67 |
if ( c >= b && isPrime(c) )
|
68 |
this->results.append( tr("%1: %2 + %3 + %4").arg(++results).arg(a).arg(b).arg(c) );
|
69 |
}
|
70 |
}
|
71 |
return results;
|
72 |
}
|
73 |
|
74 |
bool GoldbachWorker::isPrime(long long number)
|
75 |
{
|
76 |
if ( number < 2 ) return false;
|
77 |
|
78 |
long long last = static_cast<long long>( qSqrt( number) );
|
79 |
for ( long long i = 2; i <= last; ++i )
|
80 |
if ( number % i == 0 )
|
81 |
return false;
|
82 |
|
83 |
return true;
|
84 |
}
|