Skip to content

Commit 38da903

Browse files
Make perfect square when smooth number is >N
1 parent 48eda6f commit 38da903

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

FindAFactor/_find_a_factor.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,8 @@ struct Factorizer {
827827
const BigInteger bIndex = dis(gen) % threadRange;
828828
const BigInteger halfBIndex = threadOffset + (bIndex >> 1U) + 1U;
829829
const BigInteger bNum = (bIndex & 1U) ? halfBIndex : (batchTotal - halfBIndex);
830-
BigInteger n = forwardFn((bNum * wheelEntryCount) + (dis(gen) % wheelEntryCount));
831-
const std::vector<size_t> pfv = factorizationVector(&n);
830+
const BigInteger n = forwardFn((bNum * wheelEntryCount) + (dis(gen) % wheelEntryCount));
831+
const std::vector<size_t> pfv = factorizationVector(n);
832832
if (!pfv.size()) {
833833
continue;
834834
}
@@ -837,6 +837,20 @@ struct Factorizer {
837837
fv[pi] += pfv[pi];
838838
}
839839
}
840+
// We actually want not just a smooth number,
841+
// but a smooth perfect square.
842+
for (size_t pi = 0U; pi < primes.size(); ++pi) {
843+
size_t& vp = fv[pi];
844+
if (vp & 1U) {
845+
// If the prime factor component parity is odd,
846+
// multiply by the prime once to make it even.
847+
perfectSquare *= primes[pi];
848+
++vp;
849+
}
850+
// The parity is necessarily even in this factor, by now.
851+
// Divide by 2 to get the count of square prime factors.
852+
vp >>= 1U;
853+
}
840854

841855
const size_t batchLimit = smoothBatchLimit * (1ULL << batchPower);
842856
batchPower = (batchPower + 1U) % batchSizeVariance;
@@ -957,8 +971,7 @@ struct Factorizer {
957971
}
958972

959973
// Compute the prime factorization.
960-
std::vector<size_t> factorizationVector(BigInteger* numPtr) {
961-
BigInteger& num = *numPtr;
974+
std::vector<size_t> factorizationVector(BigInteger num) {
962975
std::vector<size_t> vec(primes.size(), 0);
963976
while (true) {
964977
// Proceed in steps of the GCD with the smooth prime wheel radius.
@@ -990,20 +1003,6 @@ struct Factorizer {
9901003
// because it is not smooth.
9911004
return std::vector<size_t>();
9921005
}
993-
// We actually want not just a smooth number,
994-
// but a smooth perfect square.
995-
for (size_t pi = 0U; pi < primes.size(); ++pi) {
996-
size_t& vp = vec[pi];
997-
if (vp & 1U) {
998-
// If the prime factor component parity is odd,
999-
// multiply by the prime once to make it even.
1000-
num *= primes[pi];
1001-
++vp;
1002-
}
1003-
// The parity is necessarily even in this factor, by now.
1004-
// Divide by 2 to get the count of square prime factors.
1005-
vp >>= 1U;
1006-
}
10071006

10081007
// This number is necessarily a smooth perfect square.
10091008
return vec;

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 = "4.7.9"
11+
version = "4.7.10"
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='4.7.9',
43+
version='4.7.10',
4444
author='Dan Strano',
4545
author_email='stranoj@gmail.com',
4646
description='Find any nontrivial factor of a number',

0 commit comments

Comments
 (0)