| @@ -1,72 +1,82 @@ | |
| 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 | 
            -
                     | 
| 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 | 
            -
                     | 
| 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 | 
             
            }
         | 
| 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 | 
            +
            void GoldbachWorker::updateProgress(int newPercent)
         | 
| 20 | 
            +
            {
         | 
| 21 | 
            +
                if ( this->progressPercent != newPercent )
         | 
| 22 | 
            +
                {
         | 
| 23 | 
            +
                    this->progressPercent = newPercent;
         | 
| 24 | 
            +
                    emit this->progressUpdated( this->progressPercent );
         | 
| 25 | 
            +
                }
         | 
| 26 | 
            +
            }
         | 
| 27 | 
            +
             | 
| 28 | 
             
            long long GoldbachWorker::calculate(long long number)
         | 
| 29 | 
             
            {
         | 
| 30 | 
             
                if ( number < 4 || number == 5 ) return 0;
         | 
| 31 | 
            +
                this->progressPercent = 0;
         | 
| 32 | 
             
                return number % 2 == 0 ? calculateEvenGoldbach(number) : calculateOddGoldbach(number);
         | 
| 33 | 
             
            }
         | 
| 34 |  | 
| 35 | 
             
            long long GoldbachWorker::calculateEvenGoldbach(long long number)
         | 
| 36 | 
             
            {
         | 
| 37 | 
             
                long long results = 0;
         | 
| 38 | 
             
                for ( long long a = 2; a < number && this->isInterruptionRequested() == false; ++a )
         | 
| 39 | 
             
                {
         | 
| 40 | 
            +
                    this->updateProgress(static_cast<int>(100LL * a / (number - 1)));
         | 
| 41 |  | 
| 42 | 
             
                    if ( ! isPrime(a) ) continue;
         | 
| 43 | 
             
                    long long b = number - a;
         | 
| 44 | 
             
                    if ( b >= a && isPrime(b) )
         | 
| 45 | 
             
                        this->results.append( tr("%1: %2 + %3").arg(++results).arg(a).arg(b) );
         | 
| 46 | 
             
                }
         | 
| 47 | 
             
                return results;
         | 
| 48 | 
             
            }
         | 
| 49 |  | 
| 50 | 
             
            long long GoldbachWorker::calculateOddGoldbach(long long number)
         | 
| 51 | 
             
            {
         | 
| 52 | 
             
                long long results = 0;
         | 
| 53 | 
             
                for ( long long a = 2; a < number; ++a )
         | 
| 54 | 
             
                {
         | 
| 55 | 
            +
                    this->updateProgress( static_cast<int>(100LL * a / (number - 1)) );
         | 
| 56 |  | 
| 57 | 
             
                    if ( ! isPrime(a) ) continue;
         | 
| 58 | 
             
                    for ( long long b = a; b < number; ++b )
         | 
| 59 | 
             
                    {
         | 
| 60 | 
             
                        if ( this->isInterruptionRequested() )
         | 
| 61 | 
             
                            return results;
         | 
| 62 |  | 
| 63 | 
             
                        if ( ! isPrime(b) ) continue;
         | 
| 64 | 
             
                        long long c = number - a - b;
         | 
| 65 | 
             
                        if ( c >= b && isPrime(c) )
         | 
| 66 | 
             
                            this->results.append( tr("%1: %2 + %3 + %4").arg(++results).arg(a).arg(b).arg(c) );
         | 
| 67 | 
             
                    }
         | 
| 68 | 
             
                }
         | 
| 69 | 
             
                return results;
         | 
| 70 | 
             
            }
         | 
| 71 |  | 
| 72 | 
             
            bool GoldbachWorker::isPrime(long long number)
         | 
| 73 | 
             
            {
         | 
| 74 | 
             
                if ( number < 2 ) return false;
         | 
| 75 |  | 
| 76 | 
             
                long long last = static_cast<long long>( qSqrt( number) );
         | 
| 77 | 
             
                for ( long long i = 2; i <= last; ++i )
         | 
| 78 | 
             
                    if ( number % i == 0 )
         | 
| 79 | 
             
                        return false;
         | 
| 80 |  | 
| 81 | 
             
                return true;
         | 
| 82 | 
             
            }
         |