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