|
@@ -1,112 +1,116 @@
|
|
| 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 |
return results;
|
| 80 |
}
|
| 81 |
|
| 82 |
long long MainWindow::calculateOddGoldbach(long long number)
|
| 83 |
{
|
| 84 |
long long results = 0;
|
| 85 |
for ( long long a = 2; a < number; ++a )
|
| 86 |
{
|
| 87 |
if ( ! isPrime(a) ) continue;
|
| 88 |
for ( long long b = a; b < number; ++b )
|
| 89 |
{
|
| 90 |
if ( this->userStopped )
|
| 91 |
return results;
|
| 92 |
|
| 93 |
if ( ! isPrime(b) ) continue;
|
| 94 |
long long c = number - a - b;
|
| 95 |
if ( c >= b && isPrime(c) )
|
| 96 |
this->ui->plainTextEditResults->appendPlainText( tr("%1: %2 + %3 + %4").arg(++results).arg(a).arg(b).arg(c) );
|
|
|
|
|
|
|
| 97 |
}
|
| 98 |
}
|
| 99 |
return results;
|
| 100 |
}
|
| 101 |
|
| 102 |
bool MainWindow::isPrime(long long number)
|
| 103 |
{
|
| 104 |
if ( number < 2 ) return false;
|
| 105 |
|
| 106 |
long long last = static_cast<long long>( qSqrt( number) );
|
| 107 |
for ( long long i = 2; i <= last; ++i )
|
| 108 |
if ( number % i == 0 )
|
| 109 |
return false;
|
| 110 |
|
| 111 |
return true;
|
| 112 |
}
|
| 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 |
}
|