Skip to content

Commit 95e818f

Browse files
committed
abstract sieve result, improve logging sieve hits
1 parent d48d40e commit 95e818f

14 files changed

+207
-154
lines changed

src/main/java/de/tilman_neumann/jml/factor/psiqs/PSIQSThreadBase.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - [email protected]
3+
* Copyright (C) 2018-2025 Tilman Neumann - [email protected]
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -26,9 +26,9 @@
2626
import de.tilman_neumann.jml.factor.siqs.poly.SIQSPolyGenerator;
2727
import de.tilman_neumann.jml.factor.siqs.poly.PolyReport;
2828
import de.tilman_neumann.jml.factor.siqs.sieve.Sieve;
29-
import de.tilman_neumann.jml.factor.siqs.sieve.SmoothCandidate;
3029
import de.tilman_neumann.jml.factor.siqs.sieve.SieveParams;
3130
import de.tilman_neumann.jml.factor.siqs.sieve.SieveReport;
31+
import de.tilman_neumann.jml.factor.siqs.sieve.SieveResult;
3232
import de.tilman_neumann.jml.factor.siqs.tdiv.TDivReport;
3333
import de.tilman_neumann.jml.factor.siqs.tdiv.TDiv_QS;
3434

@@ -86,23 +86,25 @@ public void run() {
8686
polyGenerator.nextPolynomial();
8787

8888
// run sieve and get the sieve locations x where Q(x) is sufficiently smooth
89-
Iterable<SmoothCandidate> smoothCandidates = sieve.sieve();
90-
//LOG.debug("Sieve found " + smoothXList.size() + " Q(x) smooth enough to be passed to trial division.");
91-
92-
// trial division stage: produce AQ-pairs
93-
List<AQPair> aqPairs = auxFactorizer.testList(smoothCandidates);
94-
//LOG.debug("Trial division found " + aqPairs.size() + " Q(x) smooth enough for a congruence.");
95-
96-
if (aqPairs.size()>0) {
97-
// add all congruences synchronized and notify control thread
98-
synchronized (congruenceCollector) {
99-
if (congruenceCollector.getFactor() == null) {
100-
congruenceCollector.collectAndProcessAQPairs(aqPairs);
101-
}
102-
if (congruenceCollector.getFactor() != null) {
103-
finishNow = true;
104-
if (DEBUG) LOG.debug("Thread " + getName() + " found factor " + congruenceCollector.getFactor() + " and is done.");
105-
congruenceCollector.notify();
89+
SieveResult smoothCandidates = sieve.sieve();
90+
if (DEBUG) LOG.debug("Sieve found " + smoothCandidates.size() + " Q(x) smooth enough to be passed to trial division: " + smoothCandidates);
91+
92+
if (smoothCandidates.size() > 0) {
93+
// trial division stage: produce AQ-pairs
94+
List<AQPair> aqPairs = auxFactorizer.testList(smoothCandidates);
95+
if (DEBUG) LOG.debug("Trial division found " + aqPairs.size() + " Q(x) smooth enough for a congruence.");
96+
97+
if (aqPairs.size() > 0) {
98+
// add all congruences synchronized and notify control thread
99+
synchronized (congruenceCollector) {
100+
if (congruenceCollector.getFactor() == null) {
101+
congruenceCollector.collectAndProcessAQPairs(aqPairs);
102+
}
103+
if (congruenceCollector.getFactor() != null) {
104+
finishNow = true;
105+
if (DEBUG) LOG.debug("Thread " + getName() + " found factor " + congruenceCollector.getFactor() + " and is done.");
106+
congruenceCollector.notify();
107+
}
106108
}
107109
}
108110
}

src/main/java/de/tilman_neumann/jml/factor/siqs/SIQS.java

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import de.tilman_neumann.jml.factor.siqs.sieve.SieveParams;
4747
import de.tilman_neumann.jml.factor.siqs.sieve.SieveParamsFactory02;
4848
import de.tilman_neumann.jml.factor.siqs.sieve.SieveReport;
49+
import de.tilman_neumann.jml.factor.siqs.sieve.SieveResult;
4950
import de.tilman_neumann.jml.factor.siqs.tdiv.TDivReport;
5051
import de.tilman_neumann.jml.factor.siqs.tdiv.TDiv_QS;
5152
import de.tilman_neumann.jml.powers.PurePowerTest;
@@ -266,37 +267,39 @@ private BigInteger findSingleFactorInternal(BigInteger N) {
266267
polyGenerator.nextPolynomial(); // sets filtered prime base in SIQS
267268

268269
// run sieve and get the sieve locations x where Q(x) is sufficiently smooth
269-
Iterable<SmoothCandidate> smoothXList = sieve.sieve();
270-
//LOG.debug("Sieve found " + smoothXList.size() + " Q(x) smooth enough to be passed to trial division.");
271-
270+
SieveResult smoothCandidates = sieve.sieve();
271+
if (DEBUG) LOG.debug("Sieve found " + smoothCandidates.size() + " Q(x) smooth enough to be passed to trial division: " + smoothCandidates);
272+
272273
// trial division stage: produce AQ-pairs
273-
List<AQPair> aqPairs = this.auxFactorizer.testList(smoothXList);
274-
//LOG.debug("Trial division found " + aqPairs.size() + " Q(x) smooth enough for a congruence.");
274+
List<AQPair> aqPairs = this.auxFactorizer.testList(smoothCandidates);
275+
if (DEBUG) LOG.debug("Trial division found " + aqPairs.size() + " Q(x) smooth enough for a congruence.");
275276
if (TEST_SIEVE) testSieve(aqPairs, adjustedSieveArraySize, kN);
276277

277-
// add all congruences
278-
congruenceCollector.collectAndProcessAQPairs(aqPairs);
279-
BigInteger factor = congruenceCollector.getFactor();
280-
if (factor != null) {
281-
if (ANALYZE) logResults(N, k, kN, factor, primeBaseSize, sieveParams);
282-
283-
if (TEST_SIEVE) {
284-
float perfectSmoothPercentage = foundPerfectSmoothCount*100 / (float) allPerfectSmoothCount;
285-
LOG.debug("foundAQPairsCount = " + foundAQPairsCount + ", foundPerfectSmoothCount = " + foundPerfectSmoothCount);
286-
LOG.debug("allAQPairsCount = " + allAQPairsCount + ", allPerfectSmoothCount = " + allPerfectSmoothCount);
287-
int allPartialsCount = allAQPairsCount-allPerfectSmoothCount;
288-
if (allPartialsCount > 0) {
289-
float partialPercentage = (foundAQPairsCount-foundPerfectSmoothCount)*100 / (float) (allAQPairsCount-allPerfectSmoothCount);
290-
LOG.debug("Sieve found " + perfectSmoothPercentage + " % of perfectly smooth and " + partialPercentage + " % of partial congruences");
291-
} else {
292-
LOG.debug("Sieve found " + perfectSmoothPercentage + " % of perfectly smooth; there were no partial congruences because N is too small");
278+
if (aqPairs.size() > 0) {
279+
// add all congruences
280+
congruenceCollector.collectAndProcessAQPairs(aqPairs);
281+
BigInteger factor = congruenceCollector.getFactor();
282+
if (factor != null) {
283+
if (ANALYZE) logResults(N, k, kN, factor, primeBaseSize, sieveParams);
284+
285+
if (TEST_SIEVE) {
286+
float perfectSmoothPercentage = foundPerfectSmoothCount*100 / (float) allPerfectSmoothCount;
287+
LOG.debug("foundAQPairsCount = " + foundAQPairsCount + ", foundPerfectSmoothCount = " + foundPerfectSmoothCount);
288+
LOG.debug("allAQPairsCount = " + allAQPairsCount + ", allPerfectSmoothCount = " + allPerfectSmoothCount);
289+
int allPartialsCount = allAQPairsCount-allPerfectSmoothCount;
290+
if (allPartialsCount > 0) {
291+
float partialPercentage = (foundAQPairsCount-foundPerfectSmoothCount)*100 / (float) (allAQPairsCount-allPerfectSmoothCount);
292+
LOG.debug("Sieve found " + perfectSmoothPercentage + " % of perfectly smooth and " + partialPercentage + " % of partial congruences");
293+
} else {
294+
LOG.debug("Sieve found " + perfectSmoothPercentage + " % of perfectly smooth; there were no partial congruences because N is too small");
295+
}
293296
}
297+
298+
// release memory after a factorization; this improves the accuracy of timings when several algorithms are tested in parallel
299+
this.cleanUp();
300+
// done
301+
return factor;
294302
}
295-
296-
// release memory after a factorization; this improves the accuracy of timings when several algorithms are tested in parallel
297-
this.cleanUp();
298-
// done
299-
return factor;
300303
}
301304
}
302305
}

src/main/java/de/tilman_neumann/jml/factor/siqs/SIQSSmall.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - [email protected]
3+
* Copyright (C) 2018-2025 Tilman Neumann - [email protected]
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -43,10 +43,10 @@
4343
import de.tilman_neumann.jml.factor.siqs.sieve.Sieve;
4444
import de.tilman_neumann.jml.factor.siqs.sieve.Sieve03g;
4545
import de.tilman_neumann.jml.factor.siqs.sieve.Sieve03gU;
46-
import de.tilman_neumann.jml.factor.siqs.sieve.SmoothCandidate;
4746
import de.tilman_neumann.jml.factor.siqs.sieve.SieveParams;
4847
import de.tilman_neumann.jml.factor.siqs.sieve.SieveParamsFactory01;
4948
import de.tilman_neumann.jml.factor.siqs.sieve.SieveReport;
49+
import de.tilman_neumann.jml.factor.siqs.sieve.SieveResult;
5050
import de.tilman_neumann.jml.factor.siqs.tdiv.TDivReport;
5151
import de.tilman_neumann.jml.factor.siqs.tdiv.TDiv_QS;
5252
import de.tilman_neumann.jml.factor.siqs.tdiv.TDiv_QS_Small;
@@ -267,25 +267,27 @@ private BigInteger findSingleFactorInternal(BigInteger N) {
267267
polyGenerator.nextPolynomial(); // sets filtered prime base in SIQS
268268

269269
// run sieve and get the sieve locations x where Q(x) is sufficiently smooth
270-
Iterable<SmoothCandidate> smoothCandidates = sieve.sieve();
271-
//LOG.debug("Sieve found " + smoothXList.size() + " Q(x) smooth enough to be passed to trial division.");
272-
273-
// trial division stage: produce AQ-pairs
274-
List<AQPair> aqPairs = this.auxFactorizer.testList(smoothCandidates);
275-
//LOG.debug("Trial division found " + aqPairs.size() + " Q(x) smooth enough for a congruence.");
276-
277-
// add all congruences
278-
congruenceCollector.collectAndProcessAQPairs(aqPairs);
279-
BigInteger factor = congruenceCollector.getFactor();
280-
if (factor != null) {
281-
if (ANALYZE) logResults(N, k, kN, factor, primeBaseSize, sieveParams);
282-
283-
// ATTENTION: SIQSSmall is only used to factor auxiliary Q-numbers for N with 320 bit or more.
284-
// ATTENTION: After a Q-factorization we only want to clean up the sieve, in case it allocated native memory!
285-
// ATTENTION: This behavior is different from SIQS or PSIQS.
286-
this.sieve.cleanUp();
287-
// done
288-
return factor;
270+
SieveResult smoothCandidates = sieve.sieve();
271+
if (DEBUG) LOG.debug("Sieve found " + smoothCandidates.size() + " Q(x) smooth enough to be passed to trial division: " + smoothCandidates);
272+
273+
if (smoothCandidates.size() > 0) {
274+
// trial division stage: produce AQ-pairs
275+
List<AQPair> aqPairs = this.auxFactorizer.testList(smoothCandidates);
276+
//LOG.debug("Trial division found " + aqPairs.size() + " Q(x) smooth enough for a congruence.");
277+
278+
// add all congruences
279+
congruenceCollector.collectAndProcessAQPairs(aqPairs);
280+
BigInteger factor = congruenceCollector.getFactor();
281+
if (factor != null) {
282+
if (ANALYZE) logResults(N, k, kN, factor, primeBaseSize, sieveParams);
283+
284+
// ATTENTION: SIQSSmall is only used to factor auxiliary Q-numbers for N with 320 bit or more.
285+
// ATTENTION: After a Q-factorization we only want to clean up the sieve, in case it allocated native memory!
286+
// ATTENTION: This behavior is different from SIQS or PSIQS.
287+
this.sieve.cleanUp();
288+
// done
289+
return factor;
290+
}
289291
}
290292
}
291293
}

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/Sieve.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018 Tilman Neumann - [email protected]
3+
* Copyright (C) 2018-2025 Tilman Neumann - [email protected]
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -57,7 +57,7 @@ public interface Sieve {
5757
* Sieve for a new set of x1, x2 solutions.
5858
* @return (something like a) list of sieve locations x where Q(x) is smooth enough to be passed to trial division
5959
*/
60-
Iterable<SmoothCandidate> sieve();
60+
SieveResult sieve();
6161

6262
/**
6363
* @return description of the durations of the individual sub-phases

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/Sieve03g.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - [email protected]
3+
* Copyright (C) 2018-2025 Tilman Neumann - [email protected]
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -88,7 +88,7 @@ public class Sieve03g implements Sieve {
8888
/** the array holding logP sums for all x */
8989
private byte[] sieveArray;
9090

91-
private SieveResult sieveResult = new SieveResult(10);
91+
private SieveResultDefaultImpl sieveResult = new SieveResultDefaultImpl(10);
9292

9393
private BinarySearch binarySearch = new BinarySearch();
9494

@@ -153,7 +153,7 @@ public void setBParameter(BigInteger b) {
153153
}
154154

155155
@Override
156-
public Iterable<SmoothCandidate> sieve() {
156+
public SieveResult sieve() {
157157
if (ANALYZE) timer.capture();
158158
this.initializeSieveArray(sieveArraySize);
159159
sieveResult.reset();

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/Sieve03gU.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - [email protected]
3+
* Copyright (C) 2018-2025 Tilman Neumann - [email protected]
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -66,7 +66,7 @@ public class Sieve03gU implements Sieve {
6666
/** base address of the sieve array holding logP sums for all x */
6767
private long sieveArrayAddress;
6868

69-
private SieveResult sieveResult = new SieveResult(10);
69+
private SieveResultDefaultImpl sieveResult = new SieveResultDefaultImpl(10);
7070

7171
private BinarySearch binarySearch = new BinarySearch();
7272

@@ -136,7 +136,7 @@ public void setBParameter(BigInteger b) {
136136
}
137137

138138
@Override
139-
public Iterable<SmoothCandidate> sieve() {
139+
public SieveResult sieve() {
140140
if (ANALYZE) timer.capture();
141141
this.initializeSieveArray(sieveArraySize);
142142
sieveResult.reset();

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/Sieve03h.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - [email protected]
3+
* Copyright (C) 2018-2025 Tilman Neumann - [email protected]
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -116,7 +116,7 @@ public class Sieve03h implements Sieve {
116116
/** buffers for trial division engine. */
117117
private UnsignedBigInt Q_rest_UBI = new UnsignedBigInt(new int[50]);
118118
private UnsignedBigInt quotient_UBI = new UnsignedBigInt(new int[50]);
119-
private SieveResult sieveResult = new SieveResult(10);
119+
private SieveResultDefaultImpl sieveResult = new SieveResultDefaultImpl(10);
120120

121121
/** the primes found to divide Q in pass 1 */
122122
private int[] pass2Primes = new int[100];
@@ -225,7 +225,7 @@ public void setBParameter(BigInteger b) {
225225
}
226226

227227
@Override
228-
public Iterable<SmoothCandidate> sieve() {
228+
public SieveResult sieve() {
229229
if (ANALYZE) timer.capture();
230230
this.initializeSieveArray(sieveArraySize);
231231
sieveResult.reset();

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/Sieve03hU.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - [email protected]
3+
* Copyright (C) 2018-2025 Tilman Neumann - [email protected]
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -118,7 +118,7 @@ public class Sieve03hU implements Sieve {
118118
/** buffers for trial division engine. */
119119
private UnsignedBigInt Q_rest_UBI = new UnsignedBigInt(new int[50]);
120120
private UnsignedBigInt quotient_UBI = new UnsignedBigInt(new int[50]);
121-
private SieveResult sieveResult = new SieveResult(10);
121+
private SieveResultDefaultImpl sieveResult = new SieveResultDefaultImpl(10);
122122

123123
/** the primes found to divide Q in pass 1 */
124124
private int[] pass2Primes = new int[100];
@@ -226,7 +226,7 @@ public void setBParameter(BigInteger b) {
226226
}
227227

228228
@Override
229-
public Iterable<SmoothCandidate> sieve() {
229+
public SieveResult sieve() {
230230
if (ANALYZE) timer.capture();
231231
this.initializeSieveArray(sieveArraySize);
232232
sieveResult.reset();

0 commit comments

Comments
 (0)