author | malenkov |
Fri, 16 May 2014 15:51:57 +0400 | |
changeset 25088 | 8d4b058368f0 |
parent 24272 | 91e947c10ddb |
child 29371 | 6f7f029a6b63 |
permissions | -rw-r--r-- |
24266 | 1 |
/* |
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* @test |
|
28 |
* @bug 8026236 |
|
29 |
* @summary test primality verification methods in BigInteger |
|
30 |
* @author bpb |
|
31 |
*/ |
|
32 |
import java.math.BigInteger; |
|
33 |
import java.util.BitSet; |
|
24272
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
34 |
import java.util.HashSet; |
24266 | 35 |
import java.util.List; |
36 |
import java.util.NavigableSet; |
|
24272
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
37 |
import java.util.Set; |
24266 | 38 |
import java.util.SplittableRandom; |
39 |
import java.util.TreeSet; |
|
40 |
import static java.util.stream.Collectors.toCollection; |
|
41 |
import static java.util.stream.Collectors.toList; |
|
42 |
||
43 |
public class PrimeTest { |
|
44 |
||
45 |
private static final int DEFAULT_UPPER_BOUND = 1299709; // 100000th prime |
|
46 |
private static final int DEFAULT_CERTAINTY = 100; |
|
47 |
private static final int NUM_NON_PRIMES = 10000; |
|
48 |
||
49 |
/** |
|
50 |
* Run the test. |
|
51 |
* |
|
52 |
* @param args The parameters. |
|
53 |
* @throws Exception on failure |
|
54 |
*/ |
|
55 |
public static void main(String[] args) throws Exception { |
|
56 |
// Prepare arguments |
|
57 |
int upperBound = args.length > 0 ? Integer.valueOf(args[0]) : DEFAULT_UPPER_BOUND; |
|
58 |
int certainty = args.length > 1 ? Integer.valueOf(args[1]) : DEFAULT_CERTAINTY; |
|
59 |
boolean parallel = args.length > 2 ? Boolean.valueOf(args[2]) : true; |
|
60 |
||
61 |
// Echo parameter settings |
|
62 |
System.out.println("Upper bound = " + upperBound |
|
63 |
+ "\nCertainty = " + certainty |
|
64 |
+ "\nParallel = " + parallel); |
|
65 |
||
66 |
// Get primes through specified bound (inclusive) and Integer.MAX_VALUE |
|
67 |
NavigableSet<BigInteger> primes = getPrimes(upperBound); |
|
68 |
||
69 |
// Check whether known primes are identified as such |
|
70 |
boolean primeTest = checkPrime(primes, certainty, parallel); |
|
71 |
System.out.println("Prime test result: " + (primeTest ? "SUCCESS" : "FAILURE")); |
|
72 |
if (!primeTest) { |
|
73 |
System.err.println("Prime test failed"); |
|
74 |
} |
|
75 |
||
76 |
// Check whether known non-primes are not identified as primes |
|
77 |
boolean nonPrimeTest = checkNonPrime(primes, certainty); |
|
78 |
System.out.println("Non-prime test result: " + (nonPrimeTest ? "SUCCESS" : "FAILURE")); |
|
79 |
||
24272
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
80 |
boolean mersennePrimeTest = checkMersennePrimes(certainty); |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
81 |
System.out.println("Mersenne test result: " + (mersennePrimeTest ? "SUCCESS" : "FAILURE")); |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
82 |
|
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
83 |
if (!primeTest || !nonPrimeTest || !mersennePrimeTest) { |
24266 | 84 |
throw new Exception("PrimeTest FAILED!"); |
85 |
} |
|
86 |
||
87 |
System.out.println("PrimeTest succeeded!"); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Create a {@code BitSet} wherein a set bit indicates the corresponding |
|
92 |
* index plus 2 is prime. That is, if bit N is set, then the integer N + 2 |
|
93 |
* is prime. The values 0 and 1 are intentionally excluded. See the |
|
94 |
* <a |
|
95 |
* href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithm_description"> |
|
96 |
* Sieve of Eratosthenes</a> algorithm description for more information. |
|
97 |
* |
|
98 |
* @param upperBound The maximum prime to allow |
|
99 |
* @return bits indicating which indexes represent primes |
|
100 |
*/ |
|
101 |
private static BitSet createPrimes(int upperBound) { |
|
102 |
int nbits = upperBound - 1; |
|
103 |
BitSet bs = new BitSet(nbits); |
|
104 |
for (int p = 2; p * p < upperBound;) { |
|
105 |
for (int i = p * p; i < nbits + 2; i += p) { |
|
106 |
bs.set(i - 2, true); |
|
107 |
} |
|
108 |
do { |
|
109 |
++p; |
|
110 |
} while (p > 1 && bs.get(p - 2)); |
|
111 |
} |
|
112 |
bs.flip(0, nbits); |
|
113 |
return bs; |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Load the primes up to the specified bound (inclusive) into a |
|
118 |
* {@code NavigableSet}, appending the prime {@code Integer.MAX_VALUE}. |
|
119 |
* |
|
120 |
* @param upperBound The maximum prime to allow |
|
121 |
* @return a set of primes |
|
122 |
*/ |
|
123 |
private static NavigableSet<BigInteger> getPrimes(int upperBound) { |
|
124 |
BitSet bs = createPrimes(upperBound); |
|
125 |
NavigableSet<BigInteger> primes = bs.stream() |
|
126 |
.mapToObj(p -> BigInteger.valueOf(p + 2)) |
|
127 |
.collect(toCollection(TreeSet::new)); |
|
128 |
primes.add(BigInteger.valueOf(Integer.MAX_VALUE)); |
|
129 |
System.out.println(String.format("Created %d primes", primes.size())); |
|
130 |
return primes; |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Verifies whether the fraction of probable primes detected is at least 1 - |
|
135 |
* 1/2^certainty. |
|
136 |
* |
|
137 |
* @return true if and only if the test succeeds |
|
138 |
*/ |
|
24272
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
139 |
private static boolean checkPrime(Set<BigInteger> primes, |
24266 | 140 |
int certainty, |
141 |
boolean parallel) { |
|
142 |
long probablePrimes = (parallel ? primes.parallelStream() : primes.stream()) |
|
143 |
.filter(bi -> bi.isProbablePrime(certainty)) |
|
144 |
.count(); |
|
145 |
||
146 |
// N = certainty / 2 |
|
147 |
// Success if p/t >= 1 - 1/4^N |
|
148 |
// or (p/t)*4^N >= 4^N - 1 |
|
149 |
// or p*4^N >= t*(4^N - 1) |
|
150 |
BigInteger p = BigInteger.valueOf(probablePrimes); |
|
151 |
BigInteger t = BigInteger.valueOf(primes.size()); |
|
152 |
BigInteger fourToTheC = BigInteger.valueOf(4).pow(certainty / 2); |
|
153 |
BigInteger fourToTheCMinusOne = fourToTheC.subtract(BigInteger.ONE); |
|
154 |
BigInteger left = p.multiply(fourToTheC); |
|
155 |
BigInteger right = t.multiply(fourToTheCMinusOne); |
|
156 |
||
157 |
if (left.compareTo(right) < 0) { |
|
24272
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
158 |
System.err.println("Probable prime certainty test failed"); |
24266 | 159 |
} |
160 |
||
161 |
return left.compareTo(right) >= 0; |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Verifies whether all {@code BigInteger}s in the tested range for which |
|
166 |
* {@code isProbablePrime()} returns {@code false} are <i>not</i> |
|
167 |
* prime numbers. |
|
168 |
* |
|
169 |
* @return true if and only if the test succeeds |
|
170 |
*/ |
|
171 |
private static boolean checkNonPrime(NavigableSet<BigInteger> primes, |
|
172 |
int certainty) { |
|
173 |
int maxPrime = DEFAULT_UPPER_BOUND; |
|
174 |
try { |
|
175 |
maxPrime = primes.last().intValueExact(); |
|
176 |
} catch (ArithmeticException e) { |
|
177 |
// ignore it |
|
178 |
} |
|
179 |
||
180 |
// Create a list of non-prime BigIntegers. |
|
181 |
List<BigInteger> nonPrimeBigInts = (new SplittableRandom()) |
|
182 |
.ints(NUM_NON_PRIMES, 2, maxPrime).mapToObj(BigInteger::valueOf) |
|
183 |
.filter(b -> !b.isProbablePrime(certainty)).collect(toList()); |
|
184 |
||
185 |
// If there are any non-probable primes also in the primes list then fail. |
|
186 |
boolean failed = nonPrimeBigInts.stream().anyMatch(primes::contains); |
|
187 |
||
188 |
// In the event, print which purported non-primes were actually prime. |
|
189 |
if (failed) { |
|
190 |
for (BigInteger bigInt : nonPrimeBigInts) { |
|
191 |
if (primes.contains(bigInt)) { |
|
192 |
System.err.println("Prime value thought to be non-prime: " + bigInt); |
|
193 |
} |
|
194 |
} |
|
195 |
} |
|
196 |
||
197 |
return !failed; |
|
198 |
} |
|
24272
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
199 |
|
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
200 |
/** |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
201 |
* Verifies whether a specified subset of Mersenne primes are correctly |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
202 |
* identified as being prime. See |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
203 |
* <a href="https://en.wikipedia.org/wiki/Mersenne_prime">Mersenne prime</a> |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
204 |
* for more information. |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
205 |
* |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
206 |
* @return true if and only if the test succeeds |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
207 |
*/ |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
208 |
private static boolean checkMersennePrimes(int certainty) { |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
209 |
int[] MERSENNE_EXPONENTS = { |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
210 |
2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
211 |
2281, 3217, 4253, // uncomment remaining array elements to make this test run a long time |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
212 |
/* 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
213 |
86243, 110503, 132049, 216091, 756839, 859433, 1257787, 1398269, |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
214 |
2976221, 3021377, 6972593, 13466917, 20996011, 24036583, 25964951, |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
215 |
30402457, 32582657, 37156667, 42643801, 43112609, 57885161 */ |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
216 |
}; |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
217 |
System.out.println("Checking first "+MERSENNE_EXPONENTS.length+" Mersenne primes"); |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
218 |
|
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
219 |
boolean result = true; |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
220 |
for (int n : MERSENNE_EXPONENTS) { |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
221 |
BigInteger mp = BigInteger.ONE.shiftLeft(n).subtract(BigInteger.ONE); |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
222 |
if (!mp.isProbablePrime(certainty)) { |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
223 |
System.err.println("Mp with p = "+n+" not classified as prime"); |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
224 |
result = false; |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
225 |
} |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
226 |
} |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
227 |
|
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
228 |
return result; |
91e947c10ddb
8042478: Include Mersenne primes in BigInteger primality testing
bpb
parents:
24266
diff
changeset
|
229 |
} |
24266 | 230 |
} |