@@ -1,116 +1,127 @@
|
|
|
|
1 |
#include <QTime>
|
2 |
#include <QtMath>
|
3 |
|
4 |
#include "MainWindow.h"
|
5 |
#include "ui_MainWindow.h"
|
6 |
|
7 |
MainWindow::MainWindow(QWidget *parent)
|
8 |
: QMainWindow(parent)
|
9 |
, ui(new Ui::MainWindow)
|
10 |
{
|
11 |
ui->setupUi(this);
|
|
|
|
|
|
|
|
|
12 |
}
|
13 |
|
14 |
MainWindow::~MainWindow()
|
15 |
{
|
16 |
delete ui;
|
17 |
}
|
18 |
|
19 |
#include <iostream>
|
20 |
void MainWindow::on_lineEditNumber_textEdited(const QString &arg1)
|
21 |
{
|
22 |
// std::cout << qPrintable(arg1) << std::endl;
|
23 |
bool enable = arg1.trimmed().length() > 0;
|
24 |
this->ui->pushButtonCalculate->setEnabled(enable);
|
25 |
}
|
26 |
|
27 |
void MainWindow::on_pushButtonStop_clicked()
|
28 |
{
|
29 |
this->userStopped = true;
|
30 |
}
|
31 |
|
32 |
void MainWindow::on_pushButtonCalculate_clicked()
|
33 |
{
|
34 |
bool isValid = true;
|
35 |
const QString& text = this->ui->lineEditNumber->text();
|
36 |
|
37 |
long long int number = text.toLongLong(&isValid);
|
38 |
|
39 |
if ( isValid )
|
40 |
{
|
41 |
this->ui->plainTextEditResults->clear();
|
|
|
|
|
42 |
this->ui->pushButtonCalculate->setEnabled(false);
|
43 |
this->ui->pushButtonStop->setEnabled(true);
|
44 |
this->userStopped = false;
|
45 |
ui->statusBar->showMessage( tr("Calculating...") );
|
46 |
|
47 |
QTime time;
|
48 |
time.start();
|
49 |
long long sumCount = this->calculate(number);
|
50 |
double seconds = time.elapsed() / 1000.0;
|
51 |
|
52 |
this->ui->pushButtonCalculate->setEnabled(true);
|
53 |
this->ui->pushButtonStop->setEnabled(false);
|
54 |
ui->statusBar->showMessage( tr("%1 sums found in %2 seconds").arg(sumCount).arg(seconds) );
|
55 |
}
|
56 |
else
|
57 |
{
|
58 |
ui->statusBar->showMessage( tr("Invalid number: %1").arg(text) );
|
59 |
}
|
60 |
}
|
61 |
|
62 |
|
63 |
long long MainWindow::calculate(long long number)
|
64 |
{
|
65 |
if ( number < 4 || number == 5 ) return 0;
|
66 |
return number % 2 == 0 ? calculateEvenGoldbach(number) : calculateOddGoldbach(number);
|
67 |
}
|
68 |
|
69 |
long long MainWindow::calculateEvenGoldbach(long long number)
|
70 |
{
|
71 |
long long results = 0;
|
72 |
for ( long long a = 2; a < number && this->userStopped == false; ++a )
|
73 |
{
|
|
|
|
|
74 |
if ( ! isPrime(a) ) continue;
|
75 |
long long b = number - a;
|
76 |
if ( b >= a && isPrime(b) )
|
77 |
this->ui->plainTextEditResults->appendPlainText( tr("%1: %2 + %3").arg(++results).arg(a).arg(b) );
|
78 |
|
79 |
QApplication::processEvents();
|
80 |
}
|
81 |
return results;
|
82 |
}
|
83 |
|
84 |
long long MainWindow::calculateOddGoldbach(long long number)
|
85 |
{
|
86 |
long long results = 0;
|
87 |
for ( long long a = 2; a < number; ++a )
|
88 |
{
|
|
|
|
|
89 |
if ( ! isPrime(a) ) continue;
|
90 |
for ( long long b = a; b < number; ++b )
|
91 |
{
|
92 |
if ( this->userStopped )
|
93 |
return results;
|
94 |
|
95 |
if ( ! isPrime(b) ) continue;
|
96 |
long long c = number - a - b;
|
97 |
if ( c >= b && isPrime(c) )
|
98 |
this->ui->plainTextEditResults->appendPlainText( tr("%1: %2 + %3 + %4").arg(++results).arg(a).arg(b).arg(c) );
|
99 |
|
100 |
QApplication::processEvents();
|
101 |
}
|
102 |
}
|
103 |
return results;
|
104 |
}
|
105 |
|
106 |
bool MainWindow::isPrime(long long number)
|
107 |
{
|
108 |
if ( number < 2 ) return false;
|
109 |
|
110 |
long long last = static_cast<long long>( qSqrt( number) );
|
111 |
for ( long long i = 2; i <= last; ++i )
|
112 |
if ( number % i == 0 )
|
113 |
return false;
|
114 |
|
115 |
return true;
|
116 |
}
|
1 |
+
#include <QProgressBar>
|
2 |
#include <QTime>
|
3 |
#include <QtMath>
|
4 |
|
5 |
#include "MainWindow.h"
|
6 |
#include "ui_MainWindow.h"
|
7 |
|
8 |
MainWindow::MainWindow(QWidget *parent)
|
9 |
: QMainWindow(parent)
|
10 |
, ui(new Ui::MainWindow)
|
11 |
{
|
12 |
ui->setupUi(this);
|
13 |
+
|
14 |
+
this->progressBar = new QProgressBar();
|
15 |
+
this->ui->statusBar->addPermanentWidget(progressBar);
|
16 |
+
this->ui->statusBar->showMessage(tr("Ready"));
|
17 |
}
|
18 |
|
19 |
MainWindow::~MainWindow()
|
20 |
{
|
21 |
delete ui;
|
22 |
}
|
23 |
|
24 |
#include <iostream>
|
25 |
void MainWindow::on_lineEditNumber_textEdited(const QString &arg1)
|
26 |
{
|
27 |
// std::cout << qPrintable(arg1) << std::endl;
|
28 |
bool enable = arg1.trimmed().length() > 0;
|
29 |
this->ui->pushButtonCalculate->setEnabled(enable);
|
30 |
}
|
31 |
|
32 |
void MainWindow::on_pushButtonStop_clicked()
|
33 |
{
|
34 |
this->userStopped = true;
|
35 |
}
|
36 |
|
37 |
void MainWindow::on_pushButtonCalculate_clicked()
|
38 |
{
|
39 |
bool isValid = true;
|
40 |
const QString& text = this->ui->lineEditNumber->text();
|
41 |
|
42 |
long long int number = text.toLongLong(&isValid);
|
43 |
|
44 |
if ( isValid )
|
45 |
{
|
46 |
this->ui->plainTextEditResults->clear();
|
47 |
+
this->progressBar->reset();
|
48 |
+
|
49 |
this->ui->pushButtonCalculate->setEnabled(false);
|
50 |
this->ui->pushButtonStop->setEnabled(true);
|
51 |
this->userStopped = false;
|
52 |
ui->statusBar->showMessage( tr("Calculating...") );
|
53 |
|
54 |
QTime time;
|
55 |
time.start();
|
56 |
long long sumCount = this->calculate(number);
|
57 |
double seconds = time.elapsed() / 1000.0;
|
58 |
|
59 |
this->ui->pushButtonCalculate->setEnabled(true);
|
60 |
this->ui->pushButtonStop->setEnabled(false);
|
61 |
ui->statusBar->showMessage( tr("%1 sums found in %2 seconds").arg(sumCount).arg(seconds) );
|
62 |
}
|
63 |
else
|
64 |
{
|
65 |
ui->statusBar->showMessage( tr("Invalid number: %1").arg(text) );
|
66 |
}
|
67 |
}
|
68 |
|
69 |
|
70 |
long long MainWindow::calculate(long long number)
|
71 |
{
|
72 |
if ( number < 4 || number == 5 ) return 0;
|
73 |
return number % 2 == 0 ? calculateEvenGoldbach(number) : calculateOddGoldbach(number);
|
74 |
}
|
75 |
|
76 |
long long MainWindow::calculateEvenGoldbach(long long number)
|
77 |
{
|
78 |
long long results = 0;
|
79 |
for ( long long a = 2; a < number && this->userStopped == false; ++a )
|
80 |
{
|
81 |
+
this->progressBar->setValue( static_cast<int>(100LL * a / (number - 1)) );
|
82 |
+
|
83 |
if ( ! isPrime(a) ) continue;
|
84 |
long long b = number - a;
|
85 |
if ( b >= a && isPrime(b) )
|
86 |
this->ui->plainTextEditResults->appendPlainText( tr("%1: %2 + %3").arg(++results).arg(a).arg(b) );
|
87 |
|
88 |
QApplication::processEvents();
|
89 |
}
|
90 |
return results;
|
91 |
}
|
92 |
|
93 |
long long MainWindow::calculateOddGoldbach(long long number)
|
94 |
{
|
95 |
long long results = 0;
|
96 |
for ( long long a = 2; a < number; ++a )
|
97 |
{
|
98 |
+
this->progressBar->setValue( static_cast<int>(100LL * a / (number - 1)) );
|
99 |
+
|
100 |
if ( ! isPrime(a) ) continue;
|
101 |
for ( long long b = a; b < number; ++b )
|
102 |
{
|
103 |
if ( this->userStopped )
|
104 |
return results;
|
105 |
|
106 |
if ( ! isPrime(b) ) continue;
|
107 |
long long c = number - a - b;
|
108 |
if ( c >= b && isPrime(c) )
|
109 |
this->ui->plainTextEditResults->appendPlainText( tr("%1: %2 + %3 + %4").arg(++results).arg(a).arg(b).arg(c) );
|
110 |
|
111 |
QApplication::processEvents();
|
112 |
}
|
113 |
}
|
114 |
return results;
|
115 |
}
|
116 |
|
117 |
bool MainWindow::isPrime(long long number)
|
118 |
{
|
119 |
if ( number < 2 ) return false;
|
120 |
|
121 |
long long last = static_cast<long long>( qSqrt( number) );
|
122 |
for ( long long i = 2; i <= last; ++i )
|
123 |
if ( number % i == 0 )
|
124 |
return false;
|
125 |
|
126 |
return true;
|
127 |
}
|