Skip to content

Commit 92d8551

Browse files
Fix exit condition after G.E.
1 parent a39b9cc commit 92d8551

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

FindAFactor/_find_a_factor.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ struct Factorizer {
943943
auto ikit = smoothNumberKeys.begin();
944944
auto ivit = smoothNumberValues.begin();
945945
BigInteger result = 1U;
946-
for (size_t i = 0U; isIncomplete && (i < smoothNumberKeys.size()); ++i) {
946+
for (size_t i = 0U; i < smoothNumberKeys.size(); ++i) {
947947
dispatch.dispatch([this, i, ikit, ivit, &result]() -> bool {
948948
auto jkit = ikit;
949949
auto jvit = ivit;
@@ -960,8 +960,8 @@ struct Factorizer {
960960
// Check congruence of squares
961961
BigInteger factor = gcd(this->toFactor, x + y);
962962
if ((factor != 1U) && (factor != this->toFactor)) {
963-
isIncomplete = false;
964963
std::lock_guard<std::mutex> lock(this->batchMutex);
964+
isIncomplete = false;
965965
result = factor;
966966

967967
return true;
@@ -971,8 +971,8 @@ struct Factorizer {
971971
// Try x - y as well
972972
factor = gcd(this->toFactor, x - y);
973973
if ((factor != 1U) && (factor != this->toFactor)) {
974-
isIncomplete = false;
975974
std::lock_guard<std::mutex> lock(this->batchMutex);
975+
isIncomplete = false;
976976
result = factor;
977977

978978
return true;
@@ -983,6 +983,12 @@ struct Factorizer {
983983
// Next inner-loop row (synchronously).
984984
++jkit;
985985
++jvit;
986+
987+
// If this actually contends, we'll exit now.
988+
std::lock_guard<std::mutex> lock(batchMutex);
989+
if (isIncomplete) {
990+
break;
991+
}
986992
}
987993

988994
return false;

0 commit comments

Comments
 (0)