Skip to content

Commit 98a96ea

Browse files
Terminate sieving early in other threads
1 parent f968baf commit 98a96ea

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

FindAFactor/_find_a_factor.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ struct Factorizer {
822822
// Sieving function
823823
void sievePolynomials(const BigInteger& low, const BigInteger& high) {
824824
const BigInteger maxLcv = toFactorSqrt + high;
825-
for (BigInteger y = toFactorSqrt + 1U + low; y < maxLcv; ++y) {
825+
for (BigInteger y = toFactorSqrt + 1U + low; isIncomplete && (y < maxLcv); ++y) {
826826
// Make the candidate NOT a multiple on the wheels.
827827
BigInteger candidate = forwardFn(backwardFn(y * y - toFactor));
828828
// This actually just goes ahead and FORCES
@@ -850,6 +850,7 @@ struct Factorizer {
850850
// If we have enough rows for Gaussian elimination already,
851851
// there's no reason to sieve any further.
852852
if (smoothNumberKeys.size() > rowLimit) {
853+
isIncomplete = false;
853854
smoothNumberSet.clear();
854855

855856
return;
@@ -933,7 +934,7 @@ struct Factorizer {
933934
// Find a congruence of squares:
934935
auto ikit = smoothNumberKeys.begin();
935936
auto ivit = smoothNumberValues.begin();
936-
BigInteger result;
937+
BigInteger result = 1U;
937938
for (size_t i = 0U; i < smoothNumberKeys.size(); ++i) {
938939
dispatch.dispatch([this, i, ikit, ivit, &result]() -> bool {
939940
auto jkit = ikit;
@@ -954,6 +955,7 @@ struct Factorizer {
954955
// Check congruence of squares
955956
BigInteger factor = gcd(this->toFactor, x + y);
956957
if ((factor != 1U) && (factor != this->toFactor)) {
958+
std::lock_guard<std::mutex> lock(this->batchMutex);
957959
result = factor;
958960

959961
return true;
@@ -966,6 +968,7 @@ struct Factorizer {
966968
// Try x - y as well
967969
factor = gcd(this->toFactor, x - y);
968970
if ((factor != 1U) && (factor != this->toFactor)) {
971+
std::lock_guard<std::mutex> lock(this->batchMutex);
969972
result = factor;
970973

971974
return true;
@@ -978,7 +981,9 @@ struct Factorizer {
978981
++ivit;
979982
}
980983

981-
return 1U;
984+
dispatch.finish();
985+
986+
return result;
982987
}
983988

984989
// Produce a smooth number with its factorization vector.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"
88

99
[project]
1010
name = "FindAFactor"
11-
version = "5.0.0"
11+
version = "5.0.1"
1212
requires-python = ">=3.8"
1313
description = "Find any nontrivial factor of a number"
1414
readme = {file = "README.txt", content-type = "text/markdown"}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def build_extension(self, ext):
4040

4141
setup(
4242
name='FindAFactor',
43-
version='5.0.0',
43+
version='5.0.1',
4444
author='Dan Strano',
4545
author_email='stranoj@gmail.com',
4646
description='Find any nontrivial factor of a number',

0 commit comments

Comments
 (0)