Skip to content

Commit adf1101

Browse files
committed
improve tests for quadratic residues computations
1 parent 8a5a0f8 commit adf1101

6 files changed

+119
-106
lines changed

src/main/java/de/tilman_neumann/jml/quadraticResidues/QuadraticResiduesMod3PowNTest.java

-71
This file was deleted.

src/test/java/de/tilman_neumann/jml/quadraticResidues/QuadraticResiduesMod2PowNPerformanceTest.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,30 @@ public static void main(String[] args) {
4242

4343
if (n<25) { // otherwise too slow
4444
t0 = System.currentTimeMillis();
45-
List<BigInteger> quadraticResiduesMod2PowN_v0 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_big(n);
45+
List<BigInteger> quadraticResidues = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_big(n);
4646
t1 = System.currentTimeMillis();
47-
LOG.info("v0: n = " + n + ": Computed " + quadraticResiduesMod2PowN_v0.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
47+
LOG.info("v0: n = " + n + ": Computed " + quadraticResidues.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
4848
}
4949

5050
if (n<29) { // avoid OutOfMemoryError
5151
t0 = System.currentTimeMillis();
52-
List<Long> quadraticResiduesMod2PowN_v1 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll(n);
52+
List<Long> quadraticResidues = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll(n);
5353
t1 = System.currentTimeMillis();
54-
LOG.info("v1: n = " + n + ": Computed " + quadraticResiduesMod2PowN_v1.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
54+
LOG.info("v1: n = " + n + ": Computed " + quadraticResidues.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
5555
}
5656

5757
if (n<31) { // avoid OutOfMemoryError
5858
t0 = System.currentTimeMillis();
59-
List<Long> quadraticResiduesMod2PowN_v2 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_v2(n);
59+
List<Long> quadraticResidues = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_v2(n);
6060
t1 = System.currentTimeMillis();
61-
LOG.info("v2: n = " + n + ": Computed " + quadraticResiduesMod2PowN_v2.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
61+
LOG.info("v2: n = " + n + ": Computed " + quadraticResidues.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
6262
}
6363

6464
if (n<30) { // avoid OutOfMemoryError
6565
t0 = System.currentTimeMillis();
66-
List<Long> quadraticResiduesMod2PowN_v3 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n);
66+
List<Long> quadraticResidues = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n);
6767
t1 = System.currentTimeMillis();
68-
LOG.info("v3: n = " + n + ": Computed " + quadraticResiduesMod2PowN_v3.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
68+
LOG.info("v3: n = " + n + ": Computed " + quadraticResidues.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
6969
}
7070

7171
if (n<33) { // avoid OutOfMemoryError

src/test/java/de/tilman_neumann/jml/quadraticResidues/QuadraticResiduesMod2PowNTest.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public static void setup() {
5151
// reference computation
5252
correctCounts = new ArrayList<Integer>();
5353
for (int n=0; n<NCOUNT; n++) {
54-
List<BigInteger> quadraticResiduesMod2PowN = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_big(n);
55-
LOG.info("n = " + n + " has " + quadraticResiduesMod2PowN.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN : ""));
56-
correctCounts.add(quadraticResiduesMod2PowN.size());
54+
List<BigInteger> quadraticResidues = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_big(n);
55+
LOG.info("n = " + n + " has " + quadraticResidues.size() + " quadratic residues modulo 2^" + n + (SHOW_ELEMENTS ? ": " + quadraticResidues : ""));
56+
correctCounts.add(quadraticResidues.size());
5757
}
5858
LOG.info("v0 counts = " + correctCounts);
5959
}
@@ -62,9 +62,9 @@ public static void setup() {
6262
public void testV1() {
6363
ArrayList<Integer> counts = new ArrayList<Integer>();
6464
for (int n=0; n<NCOUNT; n++) {
65-
List<Long> quadraticResiduesMod2PowN_v1 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll(n);
66-
if (DEBUG) LOG.debug("v1: n = " + n + " has " + quadraticResiduesMod2PowN_v1.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v1 : ""));
67-
counts.add(quadraticResiduesMod2PowN_v1.size());
65+
List<Long> quadraticResidues = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll(n);
66+
if (DEBUG) LOG.debug("v1: n = " + n + " has " + quadraticResidues.size() + " quadratic residues modulo 2^" + n + (SHOW_ELEMENTS ? ": " + quadraticResidues : ""));
67+
counts.add(quadraticResidues.size());
6868
}
6969
LOG.info("v1 counts = " + counts);
7070
assertEquals(correctCounts, counts);
@@ -74,9 +74,9 @@ public void testV1() {
7474
public void testV2() {
7575
ArrayList<Integer> counts = new ArrayList<Integer>();
7676
for (int n=0; n<NCOUNT; n++) {
77-
List<Long> quadraticResiduesMod2PowN_v1 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_v2(n);
78-
if (DEBUG) LOG.debug("v2: n = " + n + " has " + quadraticResiduesMod2PowN_v1.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v1 : ""));
79-
counts.add(quadraticResiduesMod2PowN_v1.size());
77+
List<Long> quadraticResidues = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_v2(n);
78+
if (DEBUG) LOG.debug("v2: n = " + n + " has " + quadraticResidues.size() + " quadratic residues modulo 2^" + n + (SHOW_ELEMENTS ? ": " + quadraticResidues : ""));
79+
counts.add(quadraticResidues.size());
8080
}
8181
LOG.info("v2 counts = " + counts);
8282
assertEquals(correctCounts, counts);
@@ -86,9 +86,9 @@ public void testV2() {
8686
public void testV3() {
8787
ArrayList<Integer> counts = new ArrayList<Integer>();
8888
for (int n=0; n<NCOUNT; n++) {
89-
List<Long> quadraticResiduesMod2PowN_v1 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n);
90-
if (DEBUG) LOG.debug("v3: n = " + n + " has " + quadraticResiduesMod2PowN_v1.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v1 : ""));
91-
counts.add(quadraticResiduesMod2PowN_v1.size());
89+
List<Long> quadraticResidues = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n);
90+
if (DEBUG) LOG.debug("v3: n = " + n + " has " + quadraticResidues.size() + " quadratic residues modulo 2^" + n + (SHOW_ELEMENTS ? ": " + quadraticResidues : ""));
91+
counts.add(quadraticResidues.size());
9292
}
9393
LOG.info("v3 counts = " + counts);
9494
assertEquals(correctCounts, counts);
@@ -100,7 +100,7 @@ public void testV4() {
100100
for (int n=0; n<NCOUNT; n++) {
101101
long[] array = new long[((1<<n) / 6) + 6];
102102
int count = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n, array);
103-
if (DEBUG) LOG.debug("v4: n = " + n + " has " + count + " quadratic residues" + (SHOW_ELEMENTS ? ": " + Arrays.toString(array) : ""));
103+
if (DEBUG) LOG.debug("v4: n = " + n + " has " + count + " quadratic residues modulo 2^" + n + (SHOW_ELEMENTS ? ": " + Arrays.toString(array) : ""));
104104
counts.add(count);
105105
}
106106
LOG.info("v4 counts = " + counts);

src/test/java/de/tilman_neumann/jml/quadraticResidues/QuadraticResiduesMod3PowNPerformanceTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ public static void main(String[] args) {
4242
long m = (long) Math.pow(3, n);
4343

4444
t0 = System.currentTimeMillis();
45-
TreeSet<Long> quadraticResiduesModPow = QuadraticResidues.getQuadraticResidues(m);
45+
TreeSet<Long> quadraticResidues_v1 = QuadraticResidues.getQuadraticResidues(m);
4646
t1 = System.currentTimeMillis();
47-
LOG.info("v1: n = " + n + ": Computed " + quadraticResiduesModPow.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");
47+
LOG.info("v1: n = " + n + ": Computed " + quadraticResidues_v1.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");
4848

4949
t0 = System.currentTimeMillis();
50-
List<Long> quadraticResiduesModPow_v2 = QuadraticResiduesMod3PowN.getQuadraticResiduesMod3PowN(n);
50+
List<Long> quadraticResidues_v2 = QuadraticResiduesMod3PowN.getQuadraticResiduesMod3PowN(n);
5151
t1 = System.currentTimeMillis();
52-
LOG.info("v2: n = " + n + ": Computed " + quadraticResiduesModPow_v2.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");
52+
LOG.info("v2: n = " + n + ": Computed " + quadraticResidues_v2.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");
5353

5454
t0 = System.currentTimeMillis();
55-
List<Long> quadraticResiduesModPow_v3 = QuadraticResiduesMod3PowN.getQuadraticResiduesMod3PowN_testAll(n);
55+
List<Long> quadraticResidues_v3 = QuadraticResiduesMod3PowN.getQuadraticResiduesMod3PowN_testAll(n);
5656
t1 = System.currentTimeMillis();
57-
LOG.info("v3: n = " + n + ": Computed " + quadraticResiduesModPow_v3.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");
57+
LOG.info("v3: n = " + n + ": Computed " + quadraticResidues_v3.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");
5858
}
5959
}
6060
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* 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]
4+
*
5+
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
10+
*
11+
* You should have received a copy of the GNU General Public License along with this program;
12+
* if not, see <http://www.gnu.org/licenses/>.
13+
*/
14+
package de.tilman_neumann.jml.quadraticResidues;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.TreeSet;
21+
22+
import org.apache.logging.log4j.Logger;
23+
import org.junit.BeforeClass;
24+
import org.junit.Test;
25+
import org.apache.logging.log4j.LogManager;
26+
27+
import de.tilman_neumann.util.ConfigUtil;
28+
29+
/**
30+
* QA tests for quadratic residue computations modulo 3^n.
31+
* The counts give A039300(n) = 1, 2, 4, 11, 31, 92, 274, 821, 2461, 7382, 22144, 66431, 199291, 597872, 1793614, 5380841, 16142521...
32+
*
33+
* @author Tilman Neumann
34+
*/
35+
public class QuadraticResiduesMod3PowNTest {
36+
37+
private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod3PowNTest.class);
38+
39+
private static final int NCOUNT = 15;
40+
41+
private static final boolean DEBUG = false;
42+
private static final boolean SHOW_ELEMENTS = false;
43+
44+
private static ArrayList<Integer> correctCounts;
45+
46+
@BeforeClass
47+
public static void setup() {
48+
ConfigUtil.initProject();
49+
50+
// reference computation is brute force
51+
correctCounts = new ArrayList<Integer>();
52+
for (int n=0; n<NCOUNT; n++) {
53+
long m = (long) Math.pow(3, n);
54+
TreeSet<Long> quadraticResidue = QuadraticResidues.getQuadraticResidues(m);
55+
LOG.info("n = " + n + " has " + quadraticResidue.size() + " quadratic residues modulo 3^" + n + (SHOW_ELEMENTS ? ": " + quadraticResidue : ""));
56+
correctCounts.add(quadraticResidue.size());
57+
}
58+
LOG.info("correctCounts = " + correctCounts);
59+
}
60+
61+
@Test
62+
public void testV2() {
63+
ArrayList<Integer> counts = new ArrayList<Integer>();
64+
for (int n=0; n<NCOUNT; n++) {
65+
List<Long> quadraticResidue = QuadraticResiduesMod3PowN.getQuadraticResiduesMod3PowN(n);
66+
if (DEBUG) LOG.debug("v2: n = " + n + " has " + quadraticResidue.size() + " quadratic residues modulo 3^" + n + (SHOW_ELEMENTS ? ": " + quadraticResidue : ""));
67+
counts.add(quadraticResidue.size());
68+
}
69+
LOG.info("v2 counts = " + counts);
70+
assertEquals(correctCounts, counts);
71+
}
72+
73+
@Test
74+
public void testV3() {
75+
ArrayList<Integer> counts = new ArrayList<Integer>();
76+
for (int n=0; n<NCOUNT; n++) {
77+
List<Long> quadraticResidue = QuadraticResiduesMod3PowN.getQuadraticResiduesMod3PowN_testAll(n);
78+
if (DEBUG) LOG.debug("v3: n = " + n + " has " + quadraticResidue.size() + " quadratic residues modulo 3^" + n+ (SHOW_ELEMENTS ? ": " + quadraticResidue : ""));
79+
counts.add(quadraticResidue.size());
80+
}
81+
LOG.info("v3 counts = " + counts);
82+
assertEquals(correctCounts, counts);
83+
}
84+
}

src/test/java/de/tilman_neumann/jml/quadraticResidues/QuadraticResiduesModBPowNPerformanceTest.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,24 @@ private static void testPerformance(int p, int nMax) {
4242

4343
long t0, t1;
4444
t0 = System.currentTimeMillis();
45-
TreeSet<Long> quadraticResiduesModPow = QuadraticResidues.getQuadraticResidues(m);
45+
TreeSet<Long> quadraticResidues_v1 = QuadraticResidues.getQuadraticResidues(m);
4646
t1 = System.currentTimeMillis();
47-
LOG.info("v1: n = " + n + ": Computed " + quadraticResiduesModPow.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
47+
LOG.info("v1: n = " + n + ": Computed " + quadraticResidues_v1.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
4848

4949
t0 = System.currentTimeMillis();
50-
List<Long> quadraticResiduesModPow_v2 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN(p, n);
50+
List<Long> quadraticResidues_v2 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN(p, n);
5151
t1 = System.currentTimeMillis();
52-
LOG.info("v2: n = " + n + ": Computed " + quadraticResiduesModPow_v2.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
52+
LOG.info("v2: n = " + n + ": Computed " + quadraticResidues_v2.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
5353

5454
t0 = System.currentTimeMillis();
55-
List<Long> quadraticResiduesModPow_v3 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN_testAll(p, n);
55+
List<Long> quadraticResidues_v3 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN_testAll(p, n);
5656
t1 = System.currentTimeMillis();
57-
LOG.info("v3: n = " + n + ": Computed " + quadraticResiduesModPow_v3.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
57+
LOG.info("v3: n = " + n + ": Computed " + quadraticResidues_v3.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
5858

5959
t0 = System.currentTimeMillis();
60-
List<Long> quadraticResiduesModPow_v4 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN_testAll_v2(p, n);
60+
List<Long> quadraticResidues_v4 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN_testAll_v2(p, n);
6161
t1 = System.currentTimeMillis();
62-
LOG.info("v4: n = " + n + ": Computed " + quadraticResiduesModPow_v4.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
62+
LOG.info("v4: n = " + n + ": Computed " + quadraticResidues_v4.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
6363
}
6464
}
6565

0 commit comments

Comments
 (0)