Skip to content

Commit 44274b0

Browse files
committed
fix test and work out Squ
1 parent 85b462c commit 44274b0

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/main/java/de/tilman_neumann/jml/factor/squfof/SquFoF63.java

Lines changed: 3 additions & 4 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 - 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.
@@ -22,14 +22,13 @@
2222
import de.tilman_neumann.jml.sequence.SquarefreeSequence;
2323

2424
/**
25-
* Shanks' SQUFOF algorithm, 63-bit version.<br/>
25+
* Shanks' SQUFOF algorithm, "63-bit version", which means that the core is implemented in signed longs.<br/>
2626
* Implemented according to <link>http://en.wikipedia.org/wiki/Shanks'_square_forms_factorization</link>.
2727
*
2828
* Final choice with self-initialization of parameters.
2929
* Stopping criterion: after a maximum number of iterations.
3030
*
31-
* Works for all numbers <= 63 bit.
32-
* Starting from 64 bit, there may be numbers the algorithm can't factor.
31+
* Works stable for all numbers <= 87 bit.
3332
*
3433
* @author Tilman Neumann
3534
*/

src/test/java/de/tilman_neumann/jml/factor/squfof/SquFoF63Test.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import de.tilman_neumann.util.ConfigUtil;
3030
import de.tilman_neumann.util.SortedMultiset;
3131

32+
import static de.tilman_neumann.jml.base.BigIntConstants.I_1;
33+
3234
public class SquFoF63Test {
3335

3436
private static final Logger LOG = LogManager.getLogger(SquFoF63Test.class);
@@ -49,30 +51,41 @@ public void testSomeParticularNumbers() {
4951
}
5052

5153
/**
52-
* Tests random composite numbers in the range where SquFoF63 should work correctly, i.e. up to 63 bit.
54+
* Tests smaller random composite numbers.
55+
*/
56+
@Test
57+
public void testSmallRandomComposites() {
58+
testRange(50, 69, 1000);
59+
}
60+
61+
/**
62+
* Tests random composite numbers in the upper range where SquFoF63 works stable, i.e. up to 87 bit.
5363
*/
5464
@Test
55-
public void testRandomComposites() {
56-
int count = 10000;
57-
for (int bits=50; bits<64; bits++) { // 64 bit numbers are not completely safe
65+
public void testLargerRandomComposites() {
66+
testRange(70, 86, 100);
67+
testRange(87, 87, 1000);
68+
}
69+
70+
private void testRange(int minBits, int maxBits, int count) {
71+
for (int bits=minBits; bits<=maxBits; bits++) {
5872
BigInteger[] testNumbers = TestsetGenerator.generate(count, bits, TestNumberNature.RANDOM_ODD_COMPOSITES);
5973
LOG.info("Testing " + count + " random numbers with " + bits + " bit...");
6074
int failCount = 0;
6175
for (int i=0; i<count; i++) {
62-
BigInteger NBig = testNumbers[i];
63-
long N = NBig.longValue();
64-
long squfofFactor = squfof63.findSingleFactor(N);
65-
if (squfofFactor < 2) {
66-
long correctFactor = testFactorizer.findSingleFactor(NBig).longValue();
67-
if (correctFactor > 1 && correctFactor<N) {
76+
BigInteger N = testNumbers[i];
77+
BigInteger squfofFactor = squfof63.findSingleFactor(N);
78+
if (squfofFactor.compareTo(I_1) <= 0) {
79+
BigInteger correctFactor = testFactorizer.findSingleFactor(N);
80+
if (correctFactor.compareTo(I_1)>0 && correctFactor.compareTo(N)<0) {
6881
LOG.debug("N=" + N + ": SquFoF31 failed to find factor " + correctFactor);
6982
failCount++;
7083
} else {
7184
LOG.error("The reference factorizer failed to factor N=" + N + " !");
7285
fail();
7386
}
7487
} else {
75-
if (N % squfofFactor != 0) {
88+
if (N.mod(squfofFactor).signum() != 0) {
7689
failCount++;
7790
}
7891
}

0 commit comments

Comments
 (0)