0N/A/*
2362N/A * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.math;
0N/A
0N/A/**
0N/A * A simple bit sieve used for finding prime number candidates. Allows setting
0N/A * and clearing of bits in a storage array. The size of the sieve is assumed to
0N/A * be constant to reduce overhead. All the bits of a new bitSieve are zero, and
0N/A * bits are removed from it by setting them.
0N/A *
0N/A * To reduce storage space and increase efficiency, no even numbers are
0N/A * represented in the sieve (each bit in the sieve represents an odd number).
0N/A * The relationship between the index of a bit and the number it represents is
0N/A * given by
0N/A * N = offset + (2*index + 1);
0N/A * Where N is the integer represented by a bit in the sieve, offset is some
0N/A * even integer offset indicating where the sieve begins, and index is the
0N/A * index of a bit in the sieve array.
0N/A *
0N/A * @see BigInteger
0N/A * @author Michael McCloskey
0N/A * @since 1.3
0N/A */
0N/Aclass BitSieve {
0N/A /**
0N/A * Stores the bits in this bitSieve.
0N/A */
0N/A private long bits[];
0N/A
0N/A /**
0N/A * Length is how many bits this sieve holds.
0N/A */
0N/A private int length;
0N/A
0N/A /**
0N/A * A small sieve used to filter out multiples of small primes in a search
0N/A * sieve.
0N/A */
0N/A private static BitSieve smallSieve = new BitSieve();
0N/A
0N/A /**
0N/A * Construct a "small sieve" with a base of 0. This constructor is
0N/A * used internally to generate the set of "small primes" whose multiples
0N/A * are excluded from sieves generated by the main (package private)
0N/A * constructor, BitSieve(BigInteger base, int searchLen). The length
0N/A * of the sieve generated by this constructor was chosen for performance;
0N/A * it controls a tradeoff between how much time is spent constructing
0N/A * other sieves, and how much time is wasted testing composite candidates
0N/A * for primality. The length was chosen experimentally to yield good
0N/A * performance.
0N/A */
0N/A private BitSieve() {
0N/A length = 150 * 64;
0N/A bits = new long[(unitIndex(length - 1) + 1)];
0N/A
0N/A // Mark 1 as composite
0N/A set(0);
0N/A int nextIndex = 1;
0N/A int nextPrime = 3;
0N/A
0N/A // Find primes and remove their multiples from sieve
0N/A do {
0N/A sieveSingle(length, nextIndex + nextPrime, nextPrime);
0N/A nextIndex = sieveSearch(length, nextIndex + 1);
0N/A nextPrime = 2*nextIndex + 1;
0N/A } while((nextIndex > 0) && (nextPrime < length));
0N/A }
0N/A
0N/A /**
0N/A * Construct a bit sieve of searchLen bits used for finding prime number
0N/A * candidates. The new sieve begins at the specified base, which must
0N/A * be even.
0N/A */
0N/A BitSieve(BigInteger base, int searchLen) {
0N/A /*
0N/A * Candidates are indicated by clear bits in the sieve. As a candidates
0N/A * nonprimality is calculated, a bit is set in the sieve to eliminate
0N/A * it. To reduce storage space and increase efficiency, no even numbers
0N/A * are represented in the sieve (each bit in the sieve represents an
0N/A * odd number).
0N/A */
0N/A bits = new long[(unitIndex(searchLen-1) + 1)];
0N/A length = searchLen;
0N/A int start = 0;
0N/A
0N/A int step = smallSieve.sieveSearch(smallSieve.length, start);
0N/A int convertedStep = (step *2) + 1;
0N/A
0N/A // Construct the large sieve at an even offset specified by base
1246N/A MutableBigInteger b = new MutableBigInteger(base);
0N/A MutableBigInteger q = new MutableBigInteger();
0N/A do {
0N/A // Calculate base mod convertedStep
1246N/A start = b.divideOneWord(convertedStep, q);
0N/A
0N/A // Take each multiple of step out of sieve
0N/A start = convertedStep - start;
0N/A if (start%2 == 0)
0N/A start += convertedStep;
0N/A sieveSingle(searchLen, (start-1)/2, convertedStep);
0N/A
0N/A // Find next prime from small sieve
0N/A step = smallSieve.sieveSearch(smallSieve.length, step+1);
0N/A convertedStep = (step *2) + 1;
0N/A } while (step > 0);
0N/A }
0N/A
0N/A /**
0N/A * Given a bit index return unit index containing it.
0N/A */
0N/A private static int unitIndex(int bitIndex) {
0N/A return bitIndex >>> 6;
0N/A }
0N/A
0N/A /**
0N/A * Return a unit that masks the specified bit in its unit.
0N/A */
0N/A private static long bit(int bitIndex) {
0N/A return 1L << (bitIndex & ((1<<6) - 1));
0N/A }
0N/A
0N/A /**
0N/A * Get the value of the bit at the specified index.
0N/A */
0N/A private boolean get(int bitIndex) {
0N/A int unitIndex = unitIndex(bitIndex);
0N/A return ((bits[unitIndex] & bit(bitIndex)) != 0);
0N/A }
0N/A
0N/A /**
0N/A * Set the bit at the specified index.
0N/A */
0N/A private void set(int bitIndex) {
0N/A int unitIndex = unitIndex(bitIndex);
0N/A bits[unitIndex] |= bit(bitIndex);
0N/A }
0N/A
0N/A /**
0N/A * This method returns the index of the first clear bit in the search
0N/A * array that occurs at or after start. It will not search past the
0N/A * specified limit. It returns -1 if there is no such clear bit.
0N/A */
0N/A private int sieveSearch(int limit, int start) {
0N/A if (start >= limit)
0N/A return -1;
0N/A
0N/A int index = start;
0N/A do {
0N/A if (!get(index))
0N/A return index;
0N/A index++;
0N/A } while(index < limit-1);
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Sieve a single set of multiples out of the sieve. Begin to remove
0N/A * multiples of the specified step starting at the specified start index,
0N/A * up to the specified limit.
0N/A */
0N/A private void sieveSingle(int limit, int start, int step) {
0N/A while(start < limit) {
0N/A set(start);
0N/A start += step;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Test probable primes in the sieve and return successful candidates.
0N/A */
0N/A BigInteger retrieve(BigInteger initValue, int certainty, java.util.Random random) {
0N/A // Examine the sieve one long at a time to find possible primes
0N/A int offset = 1;
0N/A for (int i=0; i<bits.length; i++) {
0N/A long nextLong = ~bits[i];
0N/A for (int j=0; j<64; j++) {
0N/A if ((nextLong & 1) == 1) {
0N/A BigInteger candidate = initValue.add(
0N/A BigInteger.valueOf(offset));
0N/A if (candidate.primeToCertainty(certainty, random))
0N/A return candidate;
0N/A }
0N/A nextLong >>>= 1;
0N/A offset+=2;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A}