|
13 | 13 | */
|
14 | 14 | package de.tilman_neumann.jml.quadraticResidues;
|
15 | 15 |
|
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | + |
16 | 18 | import java.util.ArrayList;
|
17 | 19 | import java.util.TreeSet;
|
18 | 20 |
|
19 | 21 | import org.apache.logging.log4j.Logger;
|
| 22 | +import org.junit.BeforeClass; |
| 23 | +import org.junit.Test; |
20 | 24 | import org.apache.logging.log4j.LogManager;
|
21 | 25 |
|
22 | 26 | import de.tilman_neumann.util.ConfigUtil;
|
23 | 27 |
|
24 | 28 | /**
|
25 |
| - * Tests of quadratic residue computations modulo general m. |
| 29 | + * Test quadratic residue computations modulo 2^n. |
26 | 30 | *
|
27 | 31 | * @author Tilman Neumann
|
28 | 32 | */
|
29 |
| -public class QuadraticResiduesMod2PowNTest01 { |
| 33 | +public class QuadraticResiduesMod2PowNTest { |
30 | 34 |
|
31 |
| - private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod2PowNTest01.class); |
| 35 | + private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod2PowNTest.class); |
32 | 36 |
|
33 |
| - /** |
34 |
| - * Test. |
35 |
| - * @param args ignored |
36 |
| - */ |
37 |
| - public static void main(String[] args) { |
| 37 | + private static final boolean DEBUG = false; |
| 38 | + |
| 39 | + @BeforeClass |
| 40 | + public static void setup() { |
38 | 41 | ConfigUtil.initProject();
|
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testSetOfQuadraticResidues() { |
39 | 46 | TreeSet<Long> quadraticResiduesMod100 = QuadraticResidues.getQuadraticResidues(100);
|
40 | 47 | LOG.info("m = 100 has " + quadraticResiduesMod100.size() + " quadratic residues: " + quadraticResiduesMod100);
|
41 |
| - |
| 48 | + assertEquals("[0, 1, 4, 9, 16, 21, 24, 25, 29, 36, 41, 44, 49, 56, 61, 64, 69, 76, 81, 84, 89, 96]", quadraticResiduesMod100.toString()); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test the number of quadratic residues modulo 2^n for n = 0, 1, 2, ... |
| 53 | + * This gives number sequences |
| 54 | + * A023105(n) = 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, ... for general quadratic residues modulo 2^n, and |
| 55 | + * a(n) = {1, 1} + A023105(n-2) = 1, 1, 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, 684, 1367, 2732, 5463, 10924, 21847, ... for "even" quadratic residues modulo 2^n. |
| 56 | + */ |
| 57 | + @Test |
| 58 | + public void testQuadraticResidueCounts() { |
42 | 59 | ArrayList<Integer> counts = new ArrayList<Integer>();
|
43 | 60 | ArrayList<Integer> evenCounts = new ArrayList<Integer>();
|
44 | 61 |
|
45 | 62 | for (int n=0; n<20; n++) {
|
46 | 63 | int m = 1<<n;
|
47 | 64 |
|
48 | 65 | TreeSet<Long> quadraticResiduesMod2PowN = QuadraticResidues.getQuadraticResidues(m);
|
49 |
| - LOG.info("m = " + m + " has " + quadraticResiduesMod2PowN.size() + " quadratic residues: " + quadraticResiduesMod2PowN); |
| 66 | + if (DEBUG) LOG.debug("m = " + m + " has " + quadraticResiduesMod2PowN.size() + " quadratic residues: " + quadraticResiduesMod2PowN); |
50 | 67 | counts.add(quadraticResiduesMod2PowN.size());
|
51 | 68 |
|
52 | 69 | TreeSet<Long> evenQuadraticResiduesMod2PowN = QuadraticResidues.getEvenQuadraticResidues(m);
|
53 |
| - LOG.info("m = " + m + " has " + evenQuadraticResiduesMod2PowN.size() + " 'even' quadratic residues: " + evenQuadraticResiduesMod2PowN); |
| 70 | + if (DEBUG) LOG.debug("m = " + m + " has " + evenQuadraticResiduesMod2PowN.size() + " 'even' quadratic residues: " + evenQuadraticResiduesMod2PowN); |
54 | 71 | evenCounts.add(evenQuadraticResiduesMod2PowN.size());
|
55 | 72 | }
|
56 | 73 |
|
57 | 74 | LOG.info("counts = " + counts);
|
58 |
| - // A023105(n) = 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, ... |
| 75 | + assertEquals("[1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, 684, 1367, 2732, 5463, 10924, 21847, 43692, 87383]", counts.toString()); |
59 | 76 | LOG.info("evenCounts = " + evenCounts);
|
60 |
| - // a(n) = {1, 1} + A023105(n-2) = 1, 1, 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, 684, 1367, 2732, 5463, 10924, 21847 |
| 77 | + assertEquals("[1, 1, 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, 684, 1367, 2732, 5463, 10924, 21847]", evenCounts.toString()); |
61 | 78 | }
|
62 | 79 | }
|
0 commit comments