1-goldbach/{v2.0 → v2.1}/GoldbachWorker.cpp RENAMED
@@ -1,69 +1,69 @@
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->userStopped == 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->userStopped )
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
 
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
  }