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