Add support for auto-pausing when factor is found

This commit is contained in:
ItsDrike 2025-02-26 14:59:07 +01:00
parent 12caef0a51
commit 18b6e69e9f
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
3 changed files with 47 additions and 5 deletions

View file

@ -93,10 +93,20 @@ ApplicationWindow {
} }
} }
CheckBox { RowLayout {
text: "Stop at sqrt(n)" Layout.fillWidth: true
checked: true
onCheckedChanged: factorizationController.useSqrtOptimization = checked CheckBox {
text: "Stop at sqrt(n)"
checked: factorizationController.useSqrtOptimization
onCheckedChanged: factorizationController.useSqrtOptimization = checked
}
CheckBox {
text: "Pause when factor found"
checked: factorizationController.pauseOnFound
onCheckedChanged: factorizationController.pauseOnFound = checked
}
} }
} }
} }

View file

@ -3,7 +3,8 @@
#include <cmath> #include <cmath>
FactorizationController::FactorizationController(QObject* parent) : FactorizationController::FactorizationController(QObject* parent) :
QObject{parent}, m_isRunning(false), m_useSqrtOptimization(true), m_iterationsPerCycle(1), m_currentFactNumber(0), m_originalNumber(0), m_currentFactor(0), m_stopFactor(0) { QObject{parent}, m_isRunning(false), m_useSqrtOptimization(true), m_pauseOnFound(false), m_iterationsPerCycle(1), m_currentFactNumber(0), m_originalNumber(0),
m_currentFactor(0), m_stopFactor(0) {
assert(connect(&m_timer, &QTimer::timeout, this, &FactorizationController::onTimerTick)); assert(connect(&m_timer, &QTimer::timeout, this, &FactorizationController::onTimerTick));
} }
@ -58,6 +59,18 @@ void FactorizationController::setUseSqrtOptimization(bool value) {
reset(); reset();
} }
bool FactorizationController::pauseOnFound() const {
return m_pauseOnFound;
}
void FactorizationController::setPauseOnFound(bool value) {
if (value == m_pauseOnFound)
return;
m_pauseOnFound = value;
emit pauseOnFoundChanged();
}
int FactorizationController::iterationsPerCycle() const { int FactorizationController::iterationsPerCycle() const {
return m_iterationsPerCycle; return m_iterationsPerCycle;
} }
@ -208,6 +221,9 @@ void FactorizationController::factorize() {
emit stopFactorChanged(); emit stopFactorChanged();
emit progressChanged(); emit progressChanged();
if (m_pauseOnFound)
stop();
// Don't increase current factor, keep dividing by it until no longer divisible // Don't increase current factor, keep dividing by it until no longer divisible
} else { } else {
m_currentFactor++; m_currentFactor++;

View file

@ -26,6 +26,7 @@ class FactorizationController : public QObject {
Q_PROPERTY(bool isPaused READ isPaused NOTIFY isPausedChanged) Q_PROPERTY(bool isPaused READ isPaused NOTIFY isPausedChanged)
Q_PROPERTY(int iterationsPerCycle READ iterationsPerCycle WRITE setIterationsPerCycle NOTIFY iterationsPerCycleChanged) Q_PROPERTY(int iterationsPerCycle READ iterationsPerCycle WRITE setIterationsPerCycle NOTIFY iterationsPerCycleChanged)
Q_PROPERTY(bool useSqrtOptimization READ useSqrtOptimization WRITE setUseSqrtOptimization NOTIFY useSqrtOptimizationChanged) Q_PROPERTY(bool useSqrtOptimization READ useSqrtOptimization WRITE setUseSqrtOptimization NOTIFY useSqrtOptimizationChanged)
Q_PROPERTY(bool pauseOnFound READ pauseOnFound WRITE setPauseOnFound NOTIFY pauseOnFoundChanged)
Q_PROPERTY(long long number READ number NOTIFY numberChanged) Q_PROPERTY(long long number READ number NOTIFY numberChanged)
Q_PROPERTY(long long curFactNumber READ curFactNumber NOTIFY curFactNumberChanged) Q_PROPERTY(long long curFactNumber READ curFactNumber NOTIFY curFactNumberChanged)
Q_PROPERTY(long long currentFactor READ currentFactor NOTIFY currentFactorChanged) Q_PROPERTY(long long currentFactor READ currentFactor NOTIFY currentFactorChanged)
@ -88,6 +89,12 @@ class FactorizationController : public QObject {
*/ */
bool useSqrtOptimization() const; bool useSqrtOptimization() const;
/**
* @brief Checks whether the computation should be paused once a new factor is found.
* @return True if the computation will get paused on new factor being found, otherwise False.
*/
bool pauseOnFound() const;
/** /**
* @brief Gets the original number being factorized. * @brief Gets the original number being factorized.
* @return The number being factorized. * @return The number being factorized.
@ -165,6 +172,13 @@ class FactorizationController : public QObject {
*/ */
void setUseSqrtOptimization(bool value); void setUseSqrtOptimization(bool value);
/**
* @brief Enable or disable auto-pausing on new factor being found.
*
* This value can be changed even as a computation is ongoing.
*/
void setPauseOnFound(bool value);
// endregion // endregion
// region: Functions // region: Functions
@ -208,6 +222,7 @@ class FactorizationController : public QObject {
void isFinishedChanged(); void isFinishedChanged();
void isPausedChanged(); void isPausedChanged();
void useSqrtOptimizationChanged(); void useSqrtOptimizationChanged();
void pauseOnFoundChanged();
void iterationsPerCycleChanged(); void iterationsPerCycleChanged();
void numberChanged(); void numberChanged();
void currentFactorChanged(); void currentFactorChanged();
@ -224,6 +239,7 @@ class FactorizationController : public QObject {
bool m_isFinished; ///< Indicates whether the factorization process is done. bool m_isFinished; ///< Indicates whether the factorization process is done.
bool m_isPaused; ///< Indicates whether the factorization process is paused (can be resumed). bool m_isPaused; ///< Indicates whether the factorization process is paused (can be resumed).
bool m_useSqrtOptimization; ///< Indicates whether to use the sqrt optimization bool m_useSqrtOptimization; ///< Indicates whether to use the sqrt optimization
bool m_pauseOnFound; ///< Indicates whether the computation should be paused when a new factor is found.
int m_iterationsPerCycle; ///< The number of iterations to perform per cycle. int m_iterationsPerCycle; ///< The number of iterations to perform per cycle.
long long m_currentFactNumber; ///< The number currently being factorized. long long m_currentFactNumber; ///< The number currently being factorized.
long long m_originalNumber; ///< The original input number. long long m_originalNumber; ///< The original input number.