1-goldbach/{v4.0 → v4.1}/GoldbachWorker.cpp RENAMED
@@ -1,84 +1,84 @@
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
  }
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->workerId, 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
  }