BitSieve.java revision 1246
1653N/A * Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved. 1653N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1653N/A * This code is free software; you can redistribute it and/or modify it 1653N/A * under the terms of the GNU General Public License version 2 only, as 1653N/A * published by the Free Software Foundation. Sun designates this 1653N/A * particular file as subject to the "Classpath" exception as provided 1653N/A * by Sun in the LICENSE file that accompanied this code. 1653N/A * This code is distributed in the hope that it will be useful, but WITHOUT 1653N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1653N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1653N/A * version 2 for more details (a copy is included in the LICENSE file that 1653N/A * You should have received a copy of the GNU General Public License version 1653N/A * 2 along with this work; if not, write to the Free Software Foundation, 1653N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1653N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1653N/A * CA 95054 USA or visit www.sun.com if you need additional information or 1653N/A * A simple bit sieve used for finding prime number candidates. Allows setting 1653N/A * and clearing of bits in a storage array. The size of the sieve is assumed to 1653N/A * be constant to reduce overhead. All the bits of a new bitSieve are zero, and 1653N/A * bits are removed from it by setting them. 1653N/A * To reduce storage space and increase efficiency, no even numbers are 1653N/A * represented in the sieve (each bit in the sieve represents an odd number). 1653N/A * The relationship between the index of a bit and the number it represents is * N = offset + (2*index + 1); * Where N is the integer represented by a bit in the sieve, offset is some * even integer offset indicating where the sieve begins, and index is the * index of a bit in the sieve array. * @author Michael McCloskey * Stores the bits in this bitSieve. * Length is how many bits this sieve holds. * A small sieve used to filter out multiples of small primes in a search * Construct a "small sieve" with a base of 0. This constructor is * used internally to generate the set of "small primes" whose multiples * are excluded from sieves generated by the main (package private) * constructor, BitSieve(BigInteger base, int searchLen). The length * of the sieve generated by this constructor was chosen for performance; * it controls a tradeoff between how much time is spent constructing * other sieves, and how much time is wasted testing composite candidates * for primality. The length was chosen experimentally to yield good // Find primes and remove their multiples from sieve * Construct a bit sieve of searchLen bits used for finding prime number * candidates. The new sieve begins at the specified base, which must * Candidates are indicated by clear bits in the sieve. As a candidates * nonprimality is calculated, a bit is set in the sieve to eliminate * it. To reduce storage space and increase efficiency, no even numbers * are represented in the sieve (each bit in the sieve represents an // Construct the large sieve at an even offset specified by base // Calculate base mod convertedStep // Take each multiple of step out of sieve // Find next prime from small sieve * Given a bit index return unit index containing it. * Return a unit that masks the specified bit in its unit. * Get the value of the bit at the specified index. * Set the bit at the specified index. * This method returns the index of the first clear bit in the search * array that occurs at or after start. It will not search past the * specified limit. It returns -1 if there is no such clear bit. * Sieve a single set of multiples out of the sieve. Begin to remove * multiples of the specified step starting at the specified start index, * up to the specified limit. * Test probable primes in the sieve and return successful candidates. // Examine the sieve one long at a time to find possible primes for (
int j=
0; j<
64; j++) {