Skip to content

Commit 5ee006b

Browse files
Just getNextAltBatch() in general
1 parent d0aed82 commit 5ee006b

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

FindAFactor/_find_a_factor.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -788,30 +788,33 @@ struct Factorizer {
788788
BigInteger batchRange;
789789
BigInteger batchNumber;
790790
BigInteger batchOffset;
791+
BigInteger batchTotal;
791792
size_t wheelEntryCount;
792793
size_t smoothPartsLimit;
793794
bool isIncomplete;
794795
std::vector<uint16_t> primes;
795796
ForwardFn forwardFn;
796797

797-
Factorizer(const BigInteger &tfsqr, const BigInteger &tf, const BigInteger &tfsqrt, const BigInteger &range, size_t nodeId, size_t w, size_t spl, const std::vector<uint16_t> &p,
798-
ForwardFn fn)
799-
: rng({}), toFactorSqr(tfsqr), toFactor(tf), toFactorSqrt(tfsqrt), batchRange(range), batchNumber(0U), batchOffset(nodeId * range), wheelEntryCount(w),
800-
smoothPartsLimit(spl), isIncomplete(true), primes(p), forwardFn(fn) {}
798+
Factorizer(const BigInteger &tfsqr, const BigInteger &tf, const BigInteger &tfsqrt, const BigInteger &range, size_t nodeCount, size_t nodeId, size_t w, size_t spl,
799+
const std::vector<uint16_t> &p, ForwardFn fn)
800+
: rng({}), toFactorSqr(tfsqr), toFactor(tf), toFactorSqrt(tfsqrt), batchRange(range), batchNumber(0U), batchOffset(nodeId * range), batchTotal(nodeCount * range),
801+
wheelEntryCount(w), smoothPartsLimit(spl), isIncomplete(true), primes(p), forwardFn(fn) {}
801802

802-
BigInteger getNextBatch() {
803+
BigInteger getNextAltBatch() {
803804
std::lock_guard<std::mutex> lock(batchMutex);
804805

805806
if (batchNumber >= batchRange) {
806807
isIncomplete = false;
807808
}
808809

809-
return batchOffset + batchRange - ++batchNumber;
810+
const BigInteger halfIndex = batchOffset + (batchNumber++ >> 1U) + 1U;
811+
812+
return ((batchNumber & 1U) ? batchTotal - halfIndex : halfIndex);
810813
}
811814

812815
BigInteger bruteForce(std::vector<boost::dynamic_bitset<size_t>> *inc_seqs) {
813816
// Up to wheel factorization, try all batches up to the square root of toFactor.
814-
for (BigInteger batchNum = getNextBatch(); isIncomplete; batchNum = getNextBatch()) {
817+
for (BigInteger batchNum = getNextAltBatch(); isIncomplete; batchNum = getNextAltBatch()) {
815818
const BigInteger batchStart = batchNum * wheelEntryCount;
816819
const BigInteger batchEnd = batchStart + wheelEntryCount;
817820
for (BigInteger p = batchStart; p < batchEnd;) {
@@ -832,7 +835,7 @@ struct Factorizer {
832835
// Up to wheel factorization, try all batches up to the square root of toFactor.
833836
// Since the largest prime factors of these numbers is relatively small,
834837
// use the "exhaust" of brute force to produce smooth numbers for Quadratic Sieve.
835-
for (BigInteger batchNum = getNextBatch(); isIncomplete; batchNum = getNextBatch()) {
838+
for (BigInteger batchNum = getNextAltBatch(); isIncomplete; batchNum = getNextAltBatch()) {
836839
const BigInteger batchStart = batchNum * wheelEntryCount;
837840
const BigInteger batchEnd = batchStart + wheelEntryCount;
838841
for (BigInteger p = batchStart; p < batchEnd;) {
@@ -1077,7 +1080,7 @@ std::string find_a_factor(const std::string &toFactorStr, const bool &isConOfSqr
10771080
batchSize = 1U;
10781081
std::cout << "Warning: batch multiplier would lead to a batch size of 0, but it must be at least 1. (Defaulting to 1.)";
10791082
}
1080-
Factorizer worker(toFactor * toFactor, toFactor, fullMaxBase, nodeRange, nodeId, wheelEntryCount, batchSize, primes, forward(SMALLEST_WHEEL));
1083+
Factorizer worker(toFactor * toFactor, toFactor, fullMaxBase, nodeRange, nodeCount, nodeId, wheelEntryCount, batchSize, primes, forward(SMALLEST_WHEEL));
10811084

10821085
const auto workerFn = [&toFactor, &inc_seqs, &isConOfSqr, &worker] {
10831086
// inc_seq needs to be independent per thread.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
setup(
2222
name='FindAFactor',
23-
version='1.11.0',
23+
version='1.11.1',
2424
author='Dan Strano',
2525
author_email='dan@unitary.fund',
2626
description='Find any nontrivial factor of a number',

0 commit comments

Comments
 (0)