Make stopFactor accessible to QML

This commit is contained in:
ItsDrike 2025-02-26 14:36:24 +01:00
parent 028f492c32
commit c58697b126
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
3 changed files with 30 additions and 8 deletions

View file

@ -119,6 +119,11 @@ ApplicationWindow {
font.pixelSize: 14
}
Text {
text: "Stop Factor: " + factorizationController.stopFactor
font.pixelSize: 14
}
ProgressBar {
Layout.fillWidth: true
from: 0

View file

@ -3,7 +3,7 @@
#include <cmath>
FactorizationController::FactorizationController(QObject* parent) :
QObject{parent}, m_isRunning(false), m_useSqrtOptimization(true), m_iterationsPerCycle(1), m_currentFactNumber(0), m_currentFactor(2) {
QObject{parent}, m_isRunning(false), m_useSqrtOptimization(true), m_iterationsPerCycle(1), m_currentFactNumber(0), m_currentFactor(0), m_stopFactor(0) {
assert(connect(&m_timer, &QTimer::timeout, this, &FactorizationController::onTimerTick));
}
@ -70,6 +70,10 @@ long long FactorizationController::currentFactor() const {
return m_currentFactor;
}
long long FactorizationController::stopFactor() const {
return m_stopFactor;
}
QList<long long> FactorizationController::factors() const {
return m_factors;
}
@ -81,6 +85,12 @@ void FactorizationController::start(long long number) {
m_isRunning = true;
m_isFinished = false;
m_factors.clear();
if (m_useSqrtOptimization) {
// we could also just compute this every time, but sqrt is pretty expensive
m_stopFactor = static_cast<long long>(std::sqrt(m_currentFactNumber));
} else {
m_stopFactor = m_currentFactNumber - 1;
}
emit isRunningChanged();
emit isFinishedChanged();
emit numberChanged();
@ -88,13 +98,7 @@ void FactorizationController::start(long long number) {
emit progressChanged();
emit factorsChanged();
emit curFactNumberChanged();
if (m_useSqrtOptimization) {
// we could also just compute this every time, but sqrt is pretty expensive
m_stopFactor = static_cast<long long>(std::sqrt(m_currentFactNumber));
} else {
m_stopFactor = m_currentFactNumber - 1;
}
emit stopFactorChanged();
m_timer.start(0);
}
@ -193,6 +197,7 @@ void FactorizationController::factorize() {
} else {
m_stopFactor = m_currentFactNumber - 1;
}
emit stopFactorChanged();
emit progressChanged();
// Don't increase current factor, keep dividing by it until no longer divisible

View file

@ -29,6 +29,7 @@ class FactorizationController : public QObject {
Q_PROPERTY(long long number READ number NOTIFY numberChanged)
Q_PROPERTY(long long curFactNumber READ curFactNumber NOTIFY curFactNumberChanged)
Q_PROPERTY(long long currentFactor READ currentFactor NOTIFY currentFactorChanged)
Q_PROPERTY(long long stopFactor READ stopFactor NOTIFY stopFactorChanged)
Q_PROPERTY(int progress READ progress NOTIFY progressChanged)
Q_PROPERTY(QList<long long> factors READ factors NOTIFY factorsChanged)
@ -112,6 +113,16 @@ class FactorizationController : public QObject {
*/
long long currentFactor() const;
/**
* @brief Get the number at which the factorization process will stop
* @return The current stop factor.
*
* The stop factor determines at which factor will the factorization process be stopped.
* If sqrt optimizations are enabled, this will generally be the square root of the number
* currently being factorized. If not, it will be the number being factorized - 1.
*/
long long stopFactor() const;
/**
* @brief Gets the list of discovered factors (may not be complete).
* @return A QList of extracted prime factors.
@ -203,6 +214,7 @@ class FactorizationController : public QObject {
void factorsChanged();
void progressChanged();
void curFactNumberChanged();
void stopFactorChanged();
private slots:
void onTimerTick();