Skip to content

Commit 0a132d4

Browse files
committed
improve readability
1 parent 95e818f commit 0a132d4

File tree

6 files changed

+69
-48
lines changed

6 files changed

+69
-48
lines changed

src/main/java/de/tilman_neumann/jml/factor/siqs/tdiv/TDiv_QS_2LP.java

+13-11
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 - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
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.
@@ -252,17 +252,15 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
252252
final long m = pinvArrayL[pIndex];
253253
final long q = ((x*m)>>>32);
254254
xModP = (int) (x - q * p);
255-
if (DEBUG) Ensure.ensureSmaller(xModP, p);
256255
if (xModP<0) xModP += p;
257256
if (DEBUG) {
258257
// 0 <= xModP < p
259258
Ensure.ensureSmallerEquals(0, xModP);
260259
Ensure.ensureSmaller(xModP, p);
261260

262-
int xModP2 = x % p;
263-
if (xModP2<0) xModP2 += p;
264-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
265-
Ensure.ensureEquals(xModP2, xModP);
261+
int correctMod = correctMod(x, p);
262+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
263+
Ensure.ensureEquals(correctMod, xModP);
266264
}
267265
}
268266
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
@@ -291,17 +289,15 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
291289
final long m = pinvArrayL[pIndex];
292290
final long q = ((x*m)>>>32);
293291
xModP = (int) (x - q * p);
294-
if (DEBUG) Ensure.ensureGreaterEquals(xModP, 0);
295292
if (xModP>=p) xModP -= p;
296293
if (DEBUG) {
297294
// 0 <= xModP < p
298295
Ensure.ensureSmallerEquals(0, xModP);
299296
Ensure.ensureSmaller(xModP, p);
300297

301-
int xModP2 = x % p;
302-
if (xModP2<0) xModP2 += p;
303-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
304-
Ensure.ensureEquals(xModP2, xModP);
298+
int correctMod = correctMod(x, p);
299+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
300+
Ensure.ensureEquals(correctMod, xModP);
305301
}
306302
}
307303
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
@@ -397,6 +393,12 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
397393
return new Partial2Large(A, smallFactors, factor1.longValue(), factor2.longValue());
398394
}
399395

396+
private static final int correctMod(int x, int p) {
397+
int mod = x % p;
398+
// x < 0 then mod < 0, fix that
399+
return mod < 0 ? mod + p : mod;
400+
}
401+
400402
/**
401403
* Add factors that all Q(x) for the same a-parameter have in common.
402404
* These are the q-values whose product gives the a-parameter and 2 if d==2.

src/main/java/de/tilman_neumann/jml/factor/siqs/tdiv/TDiv_QS_2LP_Full.java

+10-5
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 - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
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.
@@ -237,10 +237,9 @@ private AQPair test(BigInteger A, BigInteger Q, int x) {
237237
Ensure.ensureSmallerEquals(0, xModP);
238238
Ensure.ensureSmaller(xModP, p);
239239

240-
int xModP2 = x % p;
241-
if (xModP2<0) xModP2 += p;
242-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
243-
Ensure.ensureEquals(xModP2, xModP);
240+
int correctMod = correctMod(x, p);
241+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
242+
Ensure.ensureEquals(correctMod, xModP);
244243
}
245244
}
246245
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
@@ -336,6 +335,12 @@ private AQPair test(BigInteger A, BigInteger Q, int x) {
336335
return new Partial2Large(A, smallFactors, factor1.longValue(), factor2.longValue());
337336
}
338337

338+
private static final int correctMod(int x, int p) {
339+
int mod = x % p;
340+
// x < 0 then mod < 0, fix that
341+
return mod < 0 ? mod + p : mod;
342+
}
343+
339344
/**
340345
* Add factors that all Q(x) for the same a-parameter have in common.
341346
* These are the q-values whose product gives the a-parameter and 2 if d==2.

src/main/java/de/tilman_neumann/jml/factor/siqs/tdiv/TDiv_QS_3LP.java

+13-11
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 - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
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.
@@ -255,17 +255,15 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
255255
final long m = pinvArrayL[pIndex];
256256
final long q = ((x*m)>>>32);
257257
xModP = (int) (x - q * p);
258-
if (DEBUG) Ensure.ensureSmaller(xModP, p);
259258
if (xModP<0) xModP += p;
260259
if (DEBUG) {
261260
// 0 <= xModP < p
262261
Ensure.ensureSmallerEquals(0, xModP);
263262
Ensure.ensureSmaller(xModP, p);
264263

265-
int xModP2 = x % p;
266-
if (xModP2<0) xModP2 += p;
267-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
268-
Ensure.ensureEquals(xModP2, xModP);
264+
int correctMod = correctMod(x, p);
265+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
266+
Ensure.ensureEquals(correctMod, xModP);
269267
}
270268
}
271269
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
@@ -294,17 +292,15 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
294292
final long m = pinvArrayL[pIndex];
295293
final long q = ((x*m)>>>32);
296294
xModP = (int) (x - q * p);
297-
if (DEBUG) Ensure.ensureGreaterEquals(xModP, 0);
298295
if (xModP>=p) xModP -= p;
299296
if (DEBUG) {
300297
// 0 <= xModP < p
301298
Ensure.ensureSmallerEquals(0, xModP);
302299
Ensure.ensureSmaller(xModP, p);
303300

304-
int xModP2 = x % p;
305-
if (xModP2<0) xModP2 += p;
306-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
307-
Ensure.ensureEquals(xModP2, xModP);
301+
int correctMod = correctMod(x, p);
302+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
303+
Ensure.ensureEquals(correctMod, xModP);
308304
}
309305
}
310306
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
@@ -433,6 +429,12 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
433429
return aqPairFactory.create(A, smallFactors, bigFactors);
434430
}
435431

432+
private static final int correctMod(int x, int p) {
433+
int mod = x % p;
434+
// x < 0 then mod < 0, fix that
435+
return mod < 0 ? mod + p : mod;
436+
}
437+
436438
/**
437439
* Add factors that all Q(x) for the same a-parameter have in common.
438440
* These are the q-values whose product gives the a-parameter and 2 if d==2.

src/main/java/de/tilman_neumann/jml/factor/siqs/tdiv/TDiv_QS_Small.java

+10-5
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 - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
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.
@@ -213,10 +213,9 @@ private AQPair test(BigInteger A, BigInteger Q, int x) {
213213
Ensure.ensureSmallerEquals(0, xModP);
214214
Ensure.ensureSmaller(xModP, p);
215215

216-
int xModP2 = x % p;
217-
if (xModP2<0) xModP2 += p;
218-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
219-
Ensure.ensureEquals(xModP2, xModP);
216+
int correctMod = correctMod(x, p);
217+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
218+
Ensure.ensureEquals(correctMod, xModP);
220219
}
221220
}
222221
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
@@ -265,6 +264,12 @@ private AQPair test(BigInteger A, BigInteger Q, int x) {
265264
return new Partial1Large(A, smallFactors, QRest.longValue());
266265
}
267266

267+
private static final int correctMod(int x, int p) {
268+
int mod = x % p;
269+
// x < 0 then mod < 0, fix that
270+
return mod < 0 ? mod + p : mod;
271+
}
272+
268273
/**
269274
* Add factors that all Q(x) for the same a-parameter have in common.
270275
* These are the q-values whose product gives the a-parameter and 2 if d==2.

src/main/java/de/tilman_neumann/jml/factor/siqs/tdiv/TDiv_QS_nLP.java

+13-11
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 - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
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.
@@ -254,17 +254,15 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
254254
final long m = pinvArrayL[pIndex];
255255
final long q = ((x*m)>>>32);
256256
xModP = (int) (x - q * p);
257-
if (DEBUG) Ensure.ensureSmaller(xModP, p);
258257
if (xModP<0) xModP += p;
259258
if (DEBUG) {
260259
// 0 <= xModP < p
261260
Ensure.ensureSmallerEquals(0, xModP);
262261
Ensure.ensureSmaller(xModP, p);
263262

264-
int xModP2 = x % p;
265-
if (xModP2<0) xModP2 += p;
266-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
267-
Ensure.ensureEquals(xModP2, xModP);
263+
int correctMod = correctMod(x, p);
264+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
265+
Ensure.ensureEquals(correctMod, xModP);
268266
}
269267
}
270268
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
@@ -293,17 +291,15 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
293291
final long m = pinvArrayL[pIndex];
294292
final long q = ((x*m)>>>32);
295293
xModP = (int) (x - q * p);
296-
if (DEBUG) Ensure.ensureGreaterEquals(xModP, 0);
297294
if (xModP>=p) xModP -= p;
298295
if (DEBUG) {
299296
// 0 <= xModP < p
300297
Ensure.ensureSmallerEquals(0, xModP);
301298
Ensure.ensureSmaller(xModP, p);
302299

303-
int xModP2 = x % p;
304-
if (xModP2<0) xModP2 += p;
305-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
306-
Ensure.ensureEquals(xModP2, xModP);
300+
int correctMod = correctMod(x, p);
301+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
302+
Ensure.ensureEquals(correctMod, xModP);
307303
}
308304
}
309305
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
@@ -395,6 +391,12 @@ private boolean factor_recurrent(BigInteger QRest) {
395391
return factor_recurrent(factor1) && factor_recurrent(factor2);
396392
}
397393

394+
private static final int correctMod(int x, int p) {
395+
int mod = x % p;
396+
// x < 0 then mod < 0, fix that
397+
return mod < 0 ? mod + p : mod;
398+
}
399+
398400
/**
399401
* Add factors that all Q(x) for the same a-parameter have in common.
400402
* These are the q-values whose product gives the a-parameter and 2 if d==2.

src/main/java/de/tilman_neumann/jml/factor/siqs/tdiv/TDiv_QS_nLP_Full.java

+10-5
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 - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
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.
@@ -239,10 +239,9 @@ private AQPair test(BigInteger A, BigInteger Q, int x) {
239239
Ensure.ensureSmallerEquals(0, xModP);
240240
Ensure.ensureSmaller(xModP, p);
241241

242-
int xModP2 = x % p;
243-
if (xModP2<0) xModP2 += p;
244-
if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2);
245-
Ensure.ensureEquals(xModP2, xModP);
242+
int correctMod = correctMod(x, p);
243+
if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod);
244+
Ensure.ensureEquals(correctMod, xModP);
246245
}
247246
}
248247
if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
@@ -333,6 +332,12 @@ private boolean factor_recurrent(BigInteger QRest) {
333332
return factor_recurrent(factor1) && factor_recurrent(factor2);
334333
}
335334

335+
private static final int correctMod(int x, int p) {
336+
int mod = x % p;
337+
// x < 0 then mod < 0, fix that
338+
return mod < 0 ? mod + p : mod;
339+
}
340+
336341
/**
337342
* Add factors that all Q(x) for the same a-parameter have in common.
338343
* These are the q-values whose product gives the a-parameter and 2 if d==2.

0 commit comments

Comments
 (0)