From 028f492c32c4b9de0602282912ae76695637b63b Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 26 Feb 2025 14:18:41 +0100 Subject: [PATCH 01/10] Improve UI --- qml/Main.qml | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/qml/Main.qml b/qml/Main.qml index f0ae300..78dc6fc 100644 --- a/qml/Main.qml +++ b/qml/Main.qml @@ -22,12 +22,14 @@ ApplicationWindow { Layout.fillWidth: true ColumnLayout { + anchors.fill: parent spacing: 10 TextField { id: numberInput placeholderText: "Enter a number" // NOTE: We can't use the IntValidator here, since it doesn't support 64-bit values (long long) + // Entering invalid numbers might cause unexpected behavior (will be fed to parseInt). Layout.fillWidth: true font.pixelSize: 16 onTextChanged: factorizationController.reset() @@ -61,7 +63,9 @@ ApplicationWindow { GroupBox { title: "Settings" Layout.fillWidth: true + ColumnLayout { + anchors.fill: parent spacing: 10 RowLayout { @@ -97,29 +101,29 @@ ApplicationWindow { } } - ProgressBar { - Layout.fillWidth: true - from: 0 - to: 100 - value: factorizationController.progress - } - GroupBox { title: "Current Status" Layout.fillWidth: true - ColumnLayout { - spacing: 5 - Text { - text: "Current Factor: " + factorizationController.currentFactor - font.pixelSize: 14 - Layout.alignment: Qt.AlignHCenter - } + ColumnLayout { + anchors.fill: parent + spacing: 5 Text { text: "Current Number (being factorized): " + factorizationController.curFactNumber font.pixelSize: 14 - Layout.alignment: Qt.AlignHCenter + } + + Text { + text: "Current Factor: " + factorizationController.currentFactor + font.pixelSize: 14 + } + + ProgressBar { + Layout.fillWidth: true + from: 0 + to: 100 + value: factorizationController.progress } } } @@ -137,7 +141,7 @@ ApplicationWindow { } Text { - text: factorizationController.useSqrtOptimization ? "Note: Only factors up to sqrt(n) are searched." : "Note: Searching all factors (slower)." + text: factorizationController.useSqrtOptimization ? "Note: Only factors up to sqrt(n) are searched." : "Warning: Searching all factors (inefficient)." font.pixelSize: 12 Layout.alignment: Qt.AlignHCenter } From c58697b12639120c04ae47ddeb2116e9158d2bbe Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 26 Feb 2025 14:36:24 +0100 Subject: [PATCH 02/10] Make stopFactor accessible to QML --- qml/Main.qml | 5 +++++ src/FactorizationController.cpp | 21 +++++++++++++-------- src/FactorizationController.h | 12 ++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/qml/Main.qml b/qml/Main.qml index 78dc6fc..a7c2a70 100644 --- a/qml/Main.qml +++ b/qml/Main.qml @@ -119,6 +119,11 @@ ApplicationWindow { font.pixelSize: 14 } + Text { + text: "Stop Factor: " + factorizationController.stopFactor + font.pixelSize: 14 + } + ProgressBar { Layout.fillWidth: true from: 0 diff --git a/src/FactorizationController.cpp b/src/FactorizationController.cpp index 803c0ab..88b7dea 100644 --- a/src/FactorizationController.cpp +++ b/src/FactorizationController.cpp @@ -3,7 +3,7 @@ #include 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 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(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(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 diff --git a/src/FactorizationController.h b/src/FactorizationController.h index c3c6ab7..4a86c6f 100644 --- a/src/FactorizationController.h +++ b/src/FactorizationController.h @@ -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 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(); From 12caef0a51bc1e444b8a885a0ddabcfb78a9b9a2 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 26 Feb 2025 14:58:24 +0100 Subject: [PATCH 03/10] Fix resetting logc & progress bar --- src/FactorizationController.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/FactorizationController.cpp b/src/FactorizationController.cpp index 88b7dea..dc66497 100644 --- a/src/FactorizationController.cpp +++ b/src/FactorizationController.cpp @@ -3,7 +3,7 @@ #include FactorizationController::FactorizationController(QObject* parent) : - QObject{parent}, m_isRunning(false), m_useSqrtOptimization(true), m_iterationsPerCycle(1), m_currentFactNumber(0), m_currentFactor(0), m_stopFactor(0) { + QObject{parent}, m_isRunning(false), m_useSqrtOptimization(true), m_iterationsPerCycle(1), m_currentFactNumber(0), m_originalNumber(0), m_currentFactor(0), m_stopFactor(0) { assert(connect(&m_timer, &QTimer::timeout, this, &FactorizationController::onTimerTick)); } @@ -16,6 +16,10 @@ long long FactorizationController::curFactNumber() const { } int FactorizationController::progress() const { + // If the original number is set to 0, the computation wasn't yet initialized / was reset. + if (m_originalNumber == 0) + return 0; + // If current factor is at or below 2 already, we must be done, // stopFactor can never be less than 2, that makes no sense. if (m_currentFactNumber == 1 || m_currentFactor <= 2 || m_currentFactor > m_stopFactor) @@ -138,13 +142,17 @@ void FactorizationController::reset() { return; m_isPaused = false; - m_currentFactNumber = m_originalNumber; - m_currentFactor = 2; + m_originalNumber = 0; + m_currentFactNumber = 0; + m_currentFactor = 0; + m_stopFactor = 0; m_factors.clear(); emit isPausedChanged(); emit currentFactorChanged(); - emit progressChanged(); + emit curFactNumberChanged(); emit factorsChanged(); + emit stopFactorChanged(); + emit progressChanged(); } void FactorizationController::onTimerTick() { From 18b6e69e9fba13560259fb34a39d6a5ce86de1cb Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 26 Feb 2025 14:59:07 +0100 Subject: [PATCH 04/10] Add support for auto-pausing when factor is found --- qml/Main.qml | 18 ++++++++++++++---- src/FactorizationController.cpp | 18 +++++++++++++++++- src/FactorizationController.h | 16 ++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/qml/Main.qml b/qml/Main.qml index a7c2a70..5213209 100644 --- a/qml/Main.qml +++ b/qml/Main.qml @@ -93,10 +93,20 @@ ApplicationWindow { } } - CheckBox { - text: "Stop at sqrt(n)" - checked: true - onCheckedChanged: factorizationController.useSqrtOptimization = checked + RowLayout { + Layout.fillWidth: true + + 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 + } } } } diff --git a/src/FactorizationController.cpp b/src/FactorizationController.cpp index dc66497..de4e1b8 100644 --- a/src/FactorizationController.cpp +++ b/src/FactorizationController.cpp @@ -3,7 +3,8 @@ #include 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)); } @@ -58,6 +59,18 @@ void FactorizationController::setUseSqrtOptimization(bool value) { 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 { return m_iterationsPerCycle; } @@ -208,6 +221,9 @@ void FactorizationController::factorize() { emit stopFactorChanged(); emit progressChanged(); + if (m_pauseOnFound) + stop(); + // Don't increase current factor, keep dividing by it until no longer divisible } else { m_currentFactor++; diff --git a/src/FactorizationController.h b/src/FactorizationController.h index 4a86c6f..dd41f38 100644 --- a/src/FactorizationController.h +++ b/src/FactorizationController.h @@ -26,6 +26,7 @@ class FactorizationController : public QObject { Q_PROPERTY(bool isPaused READ isPaused NOTIFY isPausedChanged) Q_PROPERTY(int iterationsPerCycle READ iterationsPerCycle WRITE setIterationsPerCycle NOTIFY iterationsPerCycleChanged) 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 curFactNumber READ curFactNumber NOTIFY curFactNumberChanged) Q_PROPERTY(long long currentFactor READ currentFactor NOTIFY currentFactorChanged) @@ -88,6 +89,12 @@ class FactorizationController : public QObject { */ 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. * @return The number being factorized. @@ -165,6 +172,13 @@ class FactorizationController : public QObject { */ 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 // region: Functions @@ -208,6 +222,7 @@ class FactorizationController : public QObject { void isFinishedChanged(); void isPausedChanged(); void useSqrtOptimizationChanged(); + void pauseOnFoundChanged(); void iterationsPerCycleChanged(); void numberChanged(); void currentFactorChanged(); @@ -224,6 +239,7 @@ class FactorizationController : public QObject { 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_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. long long m_currentFactNumber; ///< The number currently being factorized. long long m_originalNumber; ///< The original input number. From def77613c6b293e223c0cf4c819e396cb0bd4599 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 26 Feb 2025 15:03:34 +0100 Subject: [PATCH 05/10] Support resetting with button --- qml/Main.qml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/qml/Main.qml b/qml/Main.qml index 5213209..13f5a69 100644 --- a/qml/Main.qml +++ b/qml/Main.qml @@ -51,10 +51,16 @@ ApplicationWindow { } Button { - text: "Pause" + text: factorizationController.isPaused ? "Reset" : factorizationController.isRunning ? "Pause" : "Reset" Layout.fillWidth: true - enabled: factorizationController.isRunning - onClicked: factorizationController.stop() + enabled: factorizationController.isRunning || factorizationController.isPaused + onClicked: { + if (factorizationController.isPaused) { + factorizationController.reset(); + } else { + factorizationController.stop(); + } + } } } } From c4d48e982a2cbfb767916afa6f08ea5b587c9b76 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 26 Feb 2025 15:13:45 +0100 Subject: [PATCH 06/10] Fix minor bug in progress calculation --- src/FactorizationController.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/FactorizationController.cpp b/src/FactorizationController.cpp index de4e1b8..fca7ce8 100644 --- a/src/FactorizationController.cpp +++ b/src/FactorizationController.cpp @@ -18,12 +18,14 @@ long long FactorizationController::curFactNumber() const { int FactorizationController::progress() const { // If the original number is set to 0, the computation wasn't yet initialized / was reset. - if (m_originalNumber == 0) + // If the current factor is at 2, the computation has just started, return early to avoid + // zero division issues. + if (m_originalNumber == 0 || m_currentFactor <= 2) return 0; - // If current factor is at or below 2 already, we must be done, - // stopFactor can never be less than 2, that makes no sense. - if (m_currentFactNumber == 1 || m_currentFactor <= 2 || m_currentFactor > m_stopFactor) + // If the current number being factorized is 1, we must be done, all factors were found + // Alternatively, we're also done when the current factor passes the stop factor. + if (m_currentFactNumber == 1 || m_currentFactor > m_stopFactor) return 100; // Determine the progress based on the current factor, in comparison to stop factor. From 767f706fea1db0059d0e2086154f24bb4e3c8b10 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 26 Feb 2025 15:20:35 +0100 Subject: [PATCH 07/10] Use unsigned long longs --- src/FactorizationController.cpp | 16 ++++++++-------- src/FactorizationController.h | 32 ++++++++++++++++---------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/FactorizationController.cpp b/src/FactorizationController.cpp index fca7ce8..96e9cfb 100644 --- a/src/FactorizationController.cpp +++ b/src/FactorizationController.cpp @@ -8,11 +8,11 @@ FactorizationController::FactorizationController(QObject* parent) : assert(connect(&m_timer, &QTimer::timeout, this, &FactorizationController::onTimerTick)); } -long long FactorizationController::number() const { +unsigned long long FactorizationController::number() const { return m_originalNumber; } -long long FactorizationController::curFactNumber() const { +unsigned long long FactorizationController::curFactNumber() const { return m_currentFactNumber; } @@ -85,19 +85,19 @@ void FactorizationController::setIterationsPerCycle(int number) { emit iterationsPerCycleChanged(); } -long long FactorizationController::currentFactor() const { +unsigned long long FactorizationController::currentFactor() const { return m_currentFactor; } -long long FactorizationController::stopFactor() const { +unsigned long long FactorizationController::stopFactor() const { return m_stopFactor; } -QList FactorizationController::factors() const { +QList FactorizationController::factors() const { return m_factors; } -void FactorizationController::start(long long number) { +void FactorizationController::start(unsigned long long number) { m_originalNumber = number; m_currentFactNumber = number; m_currentFactor = 2; @@ -106,7 +106,7 @@ void FactorizationController::start(long long number) { m_factors.clear(); if (m_useSqrtOptimization) { // we could also just compute this every time, but sqrt is pretty expensive - m_stopFactor = static_cast(std::sqrt(m_currentFactNumber)); + m_stopFactor = static_cast(std::sqrt(m_currentFactNumber)); } else { m_stopFactor = m_currentFactNumber - 1; } @@ -216,7 +216,7 @@ void FactorizationController::factorize() { // the new number being factorized, rather than keeping it at the original number. // (This might make the progress bar jump radically) if (m_useSqrtOptimization) { - m_stopFactor = static_cast(std::sqrt(m_currentFactNumber)); + m_stopFactor = static_cast(std::sqrt(m_currentFactNumber)); } else { m_stopFactor = m_currentFactNumber - 1; } diff --git a/src/FactorizationController.h b/src/FactorizationController.h index dd41f38..f9d5171 100644 --- a/src/FactorizationController.h +++ b/src/FactorizationController.h @@ -27,12 +27,12 @@ class FactorizationController : public QObject { Q_PROPERTY(int iterationsPerCycle READ iterationsPerCycle WRITE setIterationsPerCycle NOTIFY iterationsPerCycleChanged) 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 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(unsigned long long number READ number NOTIFY numberChanged) + Q_PROPERTY(unsigned long long curFactNumber READ curFactNumber NOTIFY curFactNumberChanged) + Q_PROPERTY(unsigned long long currentFactor READ currentFactor NOTIFY currentFactorChanged) + Q_PROPERTY(unsigned long long stopFactor READ stopFactor NOTIFY stopFactorChanged) Q_PROPERTY(int progress READ progress NOTIFY progressChanged) - Q_PROPERTY(QList factors READ factors NOTIFY factorsChanged) + Q_PROPERTY(QList factors READ factors NOTIFY factorsChanged) public: explicit FactorizationController(QObject* parent = nullptr); @@ -99,7 +99,7 @@ class FactorizationController : public QObject { * @brief Gets the original number being factorized. * @return The number being factorized. */ - long long number() const; + unsigned long long number() const; /** * @brief The current number being factorized. @@ -108,7 +108,7 @@ class FactorizationController : public QObject { * As the factorization process is going on, the number being factorized might change * from the original number, as we're dividing it by the found factors. */ - long long curFactNumber() const; + unsigned long long curFactNumber() const; /** * @brief Gets the current factor being tested. @@ -118,7 +118,7 @@ class FactorizationController : public QObject { * is a factor, going one by one. This returns the current factor (to be checked in the * next iteration). */ - long long currentFactor() const; + unsigned long long currentFactor() const; /** * @brief Get the number at which the factorization process will stop @@ -128,7 +128,7 @@ class FactorizationController : public QObject { * 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; + unsigned long long stopFactor() const; /** * @brief Gets the list of discovered factors (may not be complete). @@ -138,7 +138,7 @@ class FactorizationController : public QObject { * be complete (if the computation is still running). If you need to know whether you * can trust this result, check isFinished first. */ - QList factors() const; + QList factors() const; // endregion // region: Setters @@ -189,7 +189,7 @@ class FactorizationController : public QObject { * This will start a 0-tick timer, which triggers a partial computation on each run, * to let the event loop cycle in between. This implementation does NOT rely on threads. */ - Q_INVOKABLE void start(long long number); + Q_INVOKABLE void start(unsigned long long number); /** * @brief Stops the ongoing factorization process. @@ -241,11 +241,11 @@ class FactorizationController : public QObject { 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. - long long m_currentFactNumber; ///< The number currently being factorized. - long long m_originalNumber; ///< The original input number. - long long m_currentFactor; ///< The current divisor being checked. - long long m_stopFactor; ///< The stopping limit (square root of original number, saved for efficiency). - QList m_factors; ///< List of discovered prime factors. + unsigned long long m_currentFactNumber; ///< The number currently being factorized. + unsigned long long m_originalNumber; ///< The original input number. + unsigned long long m_currentFactor; ///< The current divisor being checked. + unsigned long long m_stopFactor; ///< The stopping limit (square root of original number, saved for efficiency). + QList m_factors; ///< List of discovered prime factors. QTimer m_timer; ///< Timer for stepwise execution. /** From 48ddf3d5ae39894227bec35a39fb602f6a6e9180 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 26 Feb 2025 15:24:58 +0100 Subject: [PATCH 08/10] Show progress bar percentage --- qml/Main.qml | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/qml/Main.qml b/qml/Main.qml index 13f5a69..c8dc9a6 100644 --- a/qml/Main.qml +++ b/qml/Main.qml @@ -140,11 +140,23 @@ ApplicationWindow { font.pixelSize: 14 } - ProgressBar { + Item { Layout.fillWidth: true - from: 0 - to: 100 - value: factorizationController.progress + Layout.preferredHeight: 25 + + ProgressBar { + anchors.fill: parent + to: 100 + value: factorizationController.progress + } + + Text { + text: factorizationController.progress.toFixed(0) + "%" + anchors.centerIn: parent + font.pixelSize: 16 + color: "black" + font.bold: true + } } } } From d54c6743e7c2c0fe4313586d138a4682b672fb01 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 26 Feb 2025 16:21:33 +0100 Subject: [PATCH 09/10] Return progress as double, not int --- qml/Main.qml | 2 +- src/FactorizationController.cpp | 6 +++--- src/FactorizationController.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/qml/Main.qml b/qml/Main.qml index c8dc9a6..389f9fe 100644 --- a/qml/Main.qml +++ b/qml/Main.qml @@ -151,7 +151,7 @@ ApplicationWindow { } Text { - text: factorizationController.progress.toFixed(0) + "%" + text: factorizationController.progress.toFixed(1) + "%" anchors.centerIn: parent font.pixelSize: 16 color: "black" diff --git a/src/FactorizationController.cpp b/src/FactorizationController.cpp index 96e9cfb..1300f19 100644 --- a/src/FactorizationController.cpp +++ b/src/FactorizationController.cpp @@ -16,7 +16,7 @@ unsigned long long FactorizationController::curFactNumber() const { return m_currentFactNumber; } -int FactorizationController::progress() const { +double FactorizationController::progress() const { // If the original number is set to 0, the computation wasn't yet initialized / was reset. // If the current factor is at 2, the computation has just started, return early to avoid // zero division issues. @@ -32,8 +32,8 @@ int FactorizationController::progress() const { // since factor being <= 2 means 100%, we sub 2 here. double progress = static_cast(m_currentFactor - 2) / static_cast(m_stopFactor - 2); - // Return the value as int percentage - return static_cast(std::clamp(progress * 100, 0.0, 100.0)); + // Return the value as percentage + return std::clamp(progress * 100, 0.0, 100.0); } bool FactorizationController::isFinished() const { diff --git a/src/FactorizationController.h b/src/FactorizationController.h index f9d5171..a2f3835 100644 --- a/src/FactorizationController.h +++ b/src/FactorizationController.h @@ -31,7 +31,7 @@ class FactorizationController : public QObject { Q_PROPERTY(unsigned long long curFactNumber READ curFactNumber NOTIFY curFactNumberChanged) Q_PROPERTY(unsigned long long currentFactor READ currentFactor NOTIFY currentFactorChanged) Q_PROPERTY(unsigned long long stopFactor READ stopFactor NOTIFY stopFactorChanged) - Q_PROPERTY(int progress READ progress NOTIFY progressChanged) + Q_PROPERTY(double progress READ progress NOTIFY progressChanged) Q_PROPERTY(QList factors READ factors NOTIFY factorsChanged) public: @@ -43,7 +43,7 @@ class FactorizationController : public QObject { * @brief Gets the current progress of factorization. * @return The progress percentage (0-100). */ - int progress() const; + double progress() const; /** * @brief Checks if the factorization process is running. From c62f0276567d61dee383db35de2a5bfdb42ca3c6 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 26 Feb 2025 16:21:52 +0100 Subject: [PATCH 10/10] Allow enabling sqrt optimizations while computing --- src/FactorizationController.cpp | 12 +++++++++++- src/FactorizationController.h | 6 ++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/FactorizationController.cpp b/src/FactorizationController.cpp index 1300f19..dc9cf79 100644 --- a/src/FactorizationController.cpp +++ b/src/FactorizationController.cpp @@ -58,7 +58,17 @@ void FactorizationController::setUseSqrtOptimization(bool value) { m_useSqrtOptimization = value; emit useSqrtOptimizationChanged(); - reset(); + + // We're in a reset state + if (m_originalNumber == 0) + return; + + if (m_useSqrtOptimization) + m_stopFactor = static_cast(std::sqrt(m_currentFactNumber)); + else + m_stopFactor = m_currentFactNumber - 1; + emit stopFactorChanged(); + emit progressChanged(); } bool FactorizationController::pauseOnFound() const { diff --git a/src/FactorizationController.h b/src/FactorizationController.h index a2f3835..af4d3e9 100644 --- a/src/FactorizationController.h +++ b/src/FactorizationController.h @@ -167,8 +167,10 @@ class FactorizationController : public QObject { * Generally, there's no reason to disable this optimization, however, if you wish to * perform a slower search, it is a way to achieve that. * - * Note that changing this value while the computation is in progress will reset the - * computation. + * This value can be changed even as a computation is ongoing; if you do so, the stop factor + * will change immediately, which will very likely cause the progress bar to rapidly change. + * It is possible that after enabling, the computation will immediately finish, as the stop + * factor will be less than the sqrt(x). */ void setUseSqrtOptimization(bool value);