Skip to content

Commit 8e420b1

Browse files
committed
rename some methods, do not expose slow implementations
1 parent 67f3a82 commit 8e420b1

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class QuadraticResiduesModPPowN {
4747
* @param n exponent of the modulus m=p^n
4848
* @return true if 'a' is a quadratic residue modulo p^n
4949
*/
50-
public static boolean isQuadraticResidueModBPowN(long a, int p, int n) {
50+
private static boolean isQuadraticResidueModPPowN_v1(long a, int p, int n) {
5151
long m = (long) Math.pow(p, n);
5252
if (a >= m) a %= m; // now 0 <= a < m
5353

@@ -73,14 +73,14 @@ public static boolean isQuadraticResidueModBPowN(long a, int p, int n) {
7373
}
7474

7575
/**
76-
* Implementation following https://en.wikipedia.org/wiki/Quadratic_residue.
76+
* Implementation following https://en.wikipedia.org/wiki/Quadratic_residue. Much faster than the first approach.
7777
* @param a
7878
* @param p
7979
* @param n
8080
* @return true if 'a' is a quadratic residue modulo p^n
8181
*/
8282
// TODO return -1, 0, 1 to check for non-residues, too ?
83-
public static boolean isQuadraticResidueModBPowN_v2(long a, int p, int n) {
83+
public static boolean isQuadraticResidueModPPowN/*_v2*/(long a, int p, int n) {
8484
long m = (long) Math.pow(p, n);
8585
if (a >= m) a %= m; // now 0 <= a < m
8686

@@ -114,11 +114,11 @@ public static boolean isQuadraticResidueModBPowN_v2(long a, int p, int n) {
114114
* @param n
115115
* @return list of quadratic residue modulus p^n
116116
*/
117-
public static List<Long> getQuadraticResiduesModBPowN_testAll(int p, int n) {
117+
static List<Long> getQuadraticResiduesModPPowN_testAll(int p, int n) {
118118
List<Long> list = new ArrayList<>();
119119
long m = (long) Math.pow(p, n);
120120
for (long a=0; a<m; a++) {
121-
if (isQuadraticResidueModBPowN(a, p, n)) {
121+
if (isQuadraticResidueModPPowN_v1(a, p, n)) {
122122
list.add(a);
123123
}
124124
}
@@ -132,24 +132,24 @@ public static List<Long> getQuadraticResiduesModBPowN_testAll(int p, int n) {
132132
* @param n
133133
* @return list of quadratic residue modulus p^n
134134
*/
135-
public static List<Long> getQuadraticResiduesModBPowN_testAll_v2(int p, int n) {
135+
static List<Long> getQuadraticResiduesModPPowN_testAll_v2(int p, int n) {
136136
List<Long> list = new ArrayList<>();
137137
long m = (long) Math.pow(p, n);
138138
for (long a=0; a<m; a++) {
139-
if (isQuadraticResidueModBPowN_v2(a, p, n)) {
139+
if (isQuadraticResidueModPPowN/*_v2*/(a, p, n)) {
140140
list.add(a);
141141
}
142142
}
143143
return list;
144144
}
145145

146146
/**
147-
* Compute all quadratic residues modulus p^n.
147+
* Compute all quadratic residues modulus p^n. This is the fastest implementation yet.
148148
* @param p
149149
* @param n
150150
* @return list of quadratic residues
151151
*/
152-
public static List<Long> getQuadraticResiduesModBPowN(int p, int n) {
152+
public static List<Long> getQuadraticResiduesModPPowN(int p, int n) {
153153
if (n == 0) return Arrays.asList(new Long[] {0L});
154154

155155
List<Long> pResidues = getBaseResidues(p);
@@ -186,7 +186,7 @@ public static List<Long> getQuadraticResiduesModBPowN(int p, int n) {
186186
}
187187

188188
private static List<Long> getBaseResidues(int p) {
189-
// We use the Jacobi symbol to compute the quaratic residues modulo p only because it is faster than the Legendre symbol.
189+
// We use the Jacobi symbol to compute the quadratic residues modulo p only because it is faster than the Legendre symbol.
190190
// Note that the result will be correct only for p being an odd prime.
191191
// For some reason, Arrays.asList(new Long[] {0L, 1L}) would throw an UnsupportedOperationException if we add Longs to the list...
192192
ArrayList<Long> result = new ArrayList<>();
@@ -198,7 +198,7 @@ private static List<Long> getBaseResidues(int p) {
198198
result.add(Long.valueOf(i));
199199
}
200200
}
201-
// LOG.debug("p=" + p + ": result = " + result);
201+
if (DEBUG) LOG.debug("p=" + p + ": result = " + result);
202202
return result;
203203
}
204204

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ private static void testPerformance(int p, int nMax) {
4747
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> quadraticResidues_v2 = QuadraticResiduesModPPowN.getQuadraticResiduesModBPowN(p, n);
50+
List<Long> quadraticResidues_v2 = QuadraticResiduesModPPowN.getQuadraticResiduesModPPowN(p, n);
5151
t1 = System.currentTimeMillis();
5252
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> quadraticResidues_v3 = QuadraticResiduesModPPowN.getQuadraticResiduesModBPowN_testAll(p, n);
55+
List<Long> quadraticResidues_v3 = QuadraticResiduesModPPowN.getQuadraticResiduesModPPowN_testAll(p, n);
5656
t1 = System.currentTimeMillis();
5757
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> quadraticResidues_v4 = QuadraticResiduesModPPowN.getQuadraticResiduesModBPowN_testAll_v2(p, n);
60+
List<Long> quadraticResidues_v4 = QuadraticResiduesModPPowN.getQuadraticResiduesModPPowN_testAll_v2(p, n);
6161
t1 = System.currentTimeMillis();
6262
LOG.info("v4: n = " + n + ": Computed " + quadraticResidues_v4.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
6363
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void testV2() {
8484
for (int p : pArray) {
8585
ArrayList<Integer> counts = new ArrayList<Integer>();
8686
for (int n=0; n<=nMaxForP[p]; n++) {
87-
List<Long> quadraticResidue = QuadraticResiduesModPPowN.getQuadraticResiduesModBPowN(p, n);
87+
List<Long> quadraticResidue = QuadraticResiduesModPPowN.getQuadraticResiduesModPPowN(p, n);
8888
if (DEBUG) LOG.debug("v2: n = " + n + " has " + quadraticResidue.size() + " quadratic residues modulo " + p + "^" + n + (SHOW_ELEMENTS ? ": " + quadraticResidue : ""));
8989
counts.add(quadraticResidue.size());
9090
}
@@ -98,7 +98,7 @@ public void testV3() {
9898
for (int p : pArray) {
9999
ArrayList<Integer> counts = new ArrayList<Integer>();
100100
for (int n=0; n<=nMaxForP[p]; n++) {
101-
List<Long> quadraticResidue = QuadraticResiduesModPPowN.getQuadraticResiduesModBPowN_testAll(p, n);
101+
List<Long> quadraticResidue = QuadraticResiduesModPPowN.getQuadraticResiduesModPPowN_testAll(p, n);
102102
if (DEBUG) LOG.debug("v3: n = " + n + " has " + quadraticResidue.size() + " quadratic residues modulo " + p + "^" + n + (SHOW_ELEMENTS ? ": " + quadraticResidue : ""));
103103
counts.add(quadraticResidue.size());
104104
}
@@ -112,7 +112,7 @@ public void testV4() {
112112
for (int p : pArray) {
113113
ArrayList<Integer> counts = new ArrayList<Integer>();
114114
for (int n=0; n<=nMaxForP[p]; n++) {
115-
List<Long> quadraticResidue = QuadraticResiduesModPPowN.getQuadraticResiduesModBPowN_testAll_v2(p, n);
115+
List<Long> quadraticResidue = QuadraticResiduesModPPowN.getQuadraticResiduesModPPowN_testAll_v2(p, n);
116116
if (DEBUG) LOG.debug("v3: n = " + n + " has " + quadraticResidue.size() + " quadratic residues modulo 3^" + n+ (SHOW_ELEMENTS ? ": " + quadraticResidue : ""));
117117
counts.add(quadraticResidue.size());
118118
}

0 commit comments

Comments
 (0)