Skip to content

Commit 42404b7

Browse files
Refactor
1 parent c68e572 commit 42404b7

3 files changed

Lines changed: 41 additions & 24 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ else (DEFINED ENV{BOOST_ROOT})
1414
endif (DEFINED ENV{BOOST_ROOT})
1515

1616
if (MSVC)
17-
target_compile_options(_find_a_factor PUBLIC /O2 /std:c++14)
17+
target_compile_options(_find_a_factor PUBLIC /O2 /std:c++17)
1818
else (MSVC)
19-
target_compile_options(_find_a_factor PUBLIC -O3 -std=c++14 -lpthread)
19+
target_compile_options(_find_a_factor PUBLIC -O3 -std=c++17 -lpthread)
2020
endif (MSVC)
2121

22-
install(TARGETS _find_a_factor DESTINATION .)
22+
install(TARGETS _find_a_factor DESTINATION .)

FindAFactor/_find_a_factor.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ struct Factorizer {
937937
BigInteger addFullRelation(const BigInteger& x, const boost::dynamic_bitset<size_t>& vec) {
938938
std::lock_guard<std::mutex> lock(batchMutex);
939939

940-
std::cout << x << ", ";
940+
std::cout << x << std::endl;
941941

942942
// Check for duplicate
943943
const auto& snvIt = std::find(smoothNumberValues.begin(), smoothNumberValues.end(), vec);
@@ -1129,17 +1129,18 @@ std::string find_a_factor(std::string toFactorStr, size_t method, size_t nodeCou
11291129
std::vector<size_t> wheelPrimesExcluded)
11301130
{
11311131
// Validation
1132-
if (method > 3U) {
1132+
if (method > 4U) {
11331133
std::cout << "Mode " << method << " not implemented. Defaulting to FACTOR_FINDER." << std::endl;
11341134
method = 1U;
11351135
}
11361136
// method: 0 = PRIME_PROVER (brute force)
11371137
// 1 = FACTOR_FINDER (ECM → Pollard Rho → MPQS)
11381138
// 2 = POLLARD_RHO only
11391139
// 3 = ECM only
1140-
const bool isPollardRho = (method == 2U);
1141-
const bool isEcmOnly = (method == 3U);
1142-
const bool isFactorFinder = (method == 1U);
1140+
// 4 = QUADRATIC_SIEVE and ECM only
1141+
const bool isPollardRho = (method == 1U) || (method == 2U);
1142+
const bool isEcm = (method == 1U) || (method == 3U) || (method == 4U);
1143+
const bool isQuadSieve = (method == 1U) || (method == 4U);
11431144

11441145
if (!wheelFactorizationLevel) wheelFactorizationLevel = 1U;
11451146
else if (!method && (wheelFactorizationLevel > 17U)) {
@@ -1198,31 +1199,42 @@ std::string find_a_factor(std::string toFactorStr, size_t method, size_t nodeCou
11981199
}
11991200

12001201
// ECM pre-check (fast for small factors, fills the gap between Rho and QS)
1201-
if (isEcmOnly || isFactorFinder) {
1202-
std::cout << "Running ECM pre-check..." << std::endl;
1202+
if (isEcm) {
1203+
std::cout << "Running ECM pre-check...";
12031204
const BigInteger ecmResult = ecm(toFactor, primes);
12041205
if (ecmResult > 1U && ecmResult < toFactor) {
1206+
std::cout << std::endl;
12051207
return boost::lexical_cast<std::string>(ecmResult);
12061208
}
1207-
if (isEcmOnly) return std::to_string(1);
1209+
if (method == 3U) {
1210+
std::cout << std::endl;
1211+
return std::to_string(1);
1212+
}
1213+
std::cout << " Done." << std::endl;
12081214
}
12091215

12101216
// Pollard's Rho
1211-
if (isPollardRho || isFactorFinder) {
1212-
std::cout << "Running Pollard's Rho..." << std::endl;
1217+
if (isPollardRho) {
1218+
std::cout << "Running Pollard's Rho...";
12131219
const BigInteger rhoResult = pollardRho(toFactor, sqrtN);
12141220
if (rhoResult > 1U && rhoResult < toFactor) {
1221+
std::cout << std::endl;
12151222
return boost::lexical_cast<std::string>(rhoResult);
12161223
}
1217-
if (isPollardRho) return std::to_string(1);
1224+
if (method == 2U) {
1225+
std::cout << std::endl;
1226+
return std::to_string(1);
1227+
}
1228+
std::cout << " Done." << std::endl;
12181229
}
12191230

12201231
// Set up wheel and gear factorization
12211232
std::vector<size_t> gearFactorizationPrimes(primes.begin(), itg);
12221233
std::vector<size_t> wheelFactorizationPrimes(primes.begin(), itw);
12231234
std::vector<size_t> smoothPrimes;
12241235

1225-
if (isFactorFinder) {
1236+
if (isQuadSieve) {
1237+
std::cout << "Selecting factor base...";
12261238
smoothPrimes = selectFactorBase(toFactor, primes);
12271239
if (smoothPrimes.empty()) {
12281240
throw std::runtime_error("No smooth primes found. Increase smoothness bound multiplier.");
@@ -1233,8 +1245,10 @@ std::string find_a_factor(std::string toFactorStr, size_t method, size_t nodeCou
12331245
const auto wit = std::find(wheelFactorizationPrimes.begin(), wheelFactorizationPrimes.end(), wpe);
12341246
if (wit != wheelFactorizationPrimes.end()) wheelFactorizationPrimes.erase(wit);
12351247
}
1248+
std::cout << " Done." << std::endl;
12361249
}
12371250

1251+
std::cout << "Setting up wheels (and 'gears')...";
12381252
BigInteger biggestWheelBigInt = 1U;
12391253
for (const size_t &wp : gearFactorizationPrimes) biggestWheelBigInt *= (size_t)wp;
12401254
biggestWheel = (size_t)biggestWheelBigInt;
@@ -1271,26 +1285,28 @@ std::string find_a_factor(std::string toFactorStr, size_t method, size_t nodeCou
12711285
const BigInteger qsNodeRange = ((((smoothBackwardFn(sqrtN + (BigInteger)((toFactor - sqrtN).convert_to<double>() * sievingBoundMultiplier + 0.5)) - qsBackwardLowBound)
12721286
+ batchItemCount - 1U) / batchItemCount) + nodeCount - 1U) / nodeCount;
12731287

1288+
std::cout << " Done." << std::endl;
1289+
12741290
Factorizer worker(toFactor, sqrtN, qsBackwardLowBound,
1275-
isFactorFinder ? qsNodeRange : ppNodeRange,
1291+
isQuadSieve ? qsNodeRange : ppNodeRange,
12761292
nodeCount, nodeId, batchItemCount, rowLimit,
1277-
isFactorFinder ? 0U : ppStartingBatch,
1293+
isQuadSieve ? 0U : ppStartingBatch,
12781294
smoothPrimes, wheelFactorizationLevel,
1279-
isFactorFinder ? ((wheel.size() > 1U) ? smoothForwardFn : forward(WHEEL1)) : ppForwardFn,
1280-
isFactorFinder ? ((wheel.size() > 1U) ? smoothBackwardFn : backward(WHEEL1)) : ppBackwardFn);
1295+
isQuadSieve ? ((wheel.size() > 1U) ? smoothForwardFn : forward(WHEEL1)) : ppForwardFn,
1296+
isQuadSieve ? ((wheel.size() > 1U) ? smoothBackwardFn : backward(WHEEL1)) : ppBackwardFn);
12811297

1282-
if (isFactorFinder) {
1283-
std::cout << "MPQS sieving. Smooth numbers: ";
1298+
if (isQuadSieve) {
1299+
std::cout << "MPQS sieving. Smooth numbers:" << std::endl;
12841300
}
12851301

12861302
std::vector<std::future<BigInteger>> futures;
12871303
futures.reserve(CpuCount);
12881304

1289-
const auto workerFn = [&inc_seqs, &worker, &isFactorFinder] {
1305+
const auto workerFn = [&inc_seqs, &worker, &isQuadSieve] {
12901306
std::vector<boost::dynamic_bitset<size_t>> inc_seqs_clone;
12911307
inc_seqs_clone.reserve(inc_seqs.size());
12921308
for (const boost::dynamic_bitset<size_t> &b : inc_seqs) inc_seqs_clone.emplace_back(b);
1293-
return isFactorFinder ? worker.mpqsSieve(&inc_seqs_clone) : worker.bruteForce(&inc_seqs_clone);
1309+
return isQuadSieve ? worker.mpqsSieve(&inc_seqs_clone) : worker.bruteForce(&inc_seqs_clone);
12941310
};
12951311

12961312
for (unsigned cpu = 0U; cpu < CpuCount; ++cpu) {
@@ -1302,7 +1318,7 @@ std::string find_a_factor(std::string toFactorStr, size_t method, size_t nodeCou
13021318
if ((r > 1U) && (r < toFactor)) return boost::lexical_cast<std::string>(r);
13031319
}
13041320

1305-
if (isFactorFinder) return boost::lexical_cast<std::string>(worker.solveForFactor());
1321+
if (isQuadSieve) return boost::lexical_cast<std::string>(worker.solveForFactor());
13061322

13071323
return std::to_string(1);
13081324
}

FindAFactor/find_a_factor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class FactoringMethod(IntEnum):
88
FACTOR_FINDER = 1
99
POLLARD_RHO = 2
1010
ECM_ONLY = 3
11+
QS_ECM = 4
1112

1213

1314
def find_a_factor(n,

0 commit comments

Comments
 (0)