de.luschny.math.primes
Class PrimeSieve.PrimeCollection

java.lang.Object
de.luschny.math.primes.PrimeSieve.PrimeCollection
All Implemented Interfaces:
IPrimeCollection, java.lang.Iterable<java.lang.Integer>, java.util.Iterator<java.lang.Integer>
Enclosing class:
PrimeSieve

private static class PrimeSieve.PrimeCollection
extends java.lang.Object
implements IPrimeCollection

PrimeCollection.

Author:
Peter Luschny

Nested Class Summary
 
Nested classes/interfaces inherited from interface de.luschny.math.primes.IPrimeCollection
IPrimeCollection.PrintOption
 
Field Summary
private  int current
          
private  int end
          
private  PositiveRange primeRange
          
private  PrimeSieve sieve
          
private  PositiveRange sieveRange
          
private  int start
          
private  java.util.concurrent.atomic.AtomicInteger state
          
 
Constructor Summary
PrimeSieve.PrimeCollection(PrimeSieve sieve)
          Constructs the collection for the passed sieve.
PrimeSieve.PrimeCollection(PrimeSieve sieve, PositiveRange sieveRange)
          Constructs a collection over a subrange of the range of the sieve.
 
Method Summary
 int getNumberOfPrimes()
          Computes the number of primes in the collection.
 double getPrimeDensity()
          Computes the density of primes in the collection.
 PositiveRange getPrimeRange()
          Gives the range of the indices of the prime numbers in the collection.
 PositiveRange getSieveRange()
          Gives the interval [a,b] of the sieve.
 boolean hasNext()
          Checks the current status of the finite collection.
(package private) static int indexOf(int[] data, int value, int low, int high)
          Identifies the index of a prime number.
 java.util.Iterator<java.lang.Integer> iterator()
          Provides an iterator over the current prime number collection.
 java.lang.Integer next()
          Returns The next prime number in the collection.
 de.luschny.math.arithmetic.Xint primorial()
          Computes the product of all primes in the range of this collection.
 void remove()
          The (optional operation) to remove from the underlying collection the last element returned by the iterator is not supported.
 int[] toArray()
          Gives the prime numbers in the collection as an array.
 void toFile(java.lang.String fileName, IPrimeCollection.PrintOption format)
          Writes the primes collection to a file.
private  void writeComma(java.io.PrintWriter file)
          Prints the list of primes in a comma separated format to a file.
private  void writeFormatted(java.io.PrintWriter file)
          Prints the list of prime numbers in a formatted way to a file.
private  void writeXml(java.io.PrintWriter file)
          Prints the list of prime numbers in XML format to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

current

private int current

end

private final int end

primeRange

private final PositiveRange primeRange

sieve

private final PrimeSieve sieve

sieveRange

private final PositiveRange sieveRange

start

private final int start

state

private final java.util.concurrent.atomic.AtomicInteger state
Constructor Detail

PrimeSieve.PrimeCollection

PrimeSieve.PrimeCollection(PrimeSieve sieve)
Constructs the collection for the passed sieve.

Parameters:
sieve - The sieve which is to be enumerated.

PrimeSieve.PrimeCollection

PrimeSieve.PrimeCollection(PrimeSieve sieve,
PositiveRange sieveRange)
Constructs a collection over a subrange of the range of the sieve.

Parameters:
sieve - Prime number sieve to be used.
sieveRange - Range of collection.
Method Detail

getNumberOfPrimes

public int getNumberOfPrimes()
Computes the number of primes in the collection.

Specified by:
getNumberOfPrimes in interface IPrimeCollection
Returns:
Cardinality of primes in collection.

getPrimeDensity

public double getPrimeDensity()
Computes the density of primes in the collection.

Specified by:
getPrimeDensity in interface IPrimeCollection
Returns:
Ratio of the number of primes relativ to the number of integers in this collection.

getPrimeRange

public PositiveRange getPrimeRange()
Gives the range of the indices of the prime numbers in the collection.

Specified by:
getPrimeRange in interface IPrimeCollection
Returns:
Range of indices.

getSieveRange

public PositiveRange getSieveRange()
Gives the interval [a,b] of the sieve.

Specified by:
getSieveRange in interface IPrimeCollection
Returns:
Sieved interval.
See Also:
IPrimeCollection.getPrimeRange()

hasNext

public boolean hasNext()
Checks the current status of the finite collection.

Specified by:
hasNext in interface java.util.Iterator<java.lang.Integer>
Returns:
True iff there are more prime numbers to be enumerated.

indexOf

static final int indexOf(int[] data,
                         int value,
                         int low,
                         int high)
Identifies the index of a prime number. Uses a (modified!) binary search.

Parameters:
data - List of prime numbers.
value - The prime number given.
low - Lower bound for the index.
high - Upper bound for the index.
Returns:
The index of the prime number.

iterator

public java.util.Iterator<java.lang.Integer> iterator()
Provides an iterator over the current prime number collection. This iterator is thread save.

Specified by:
iterator in interface java.lang.Iterable<java.lang.Integer>
Returns:
An iterator over the current prime number collection.

next

public java.lang.Integer next()
Returns The next prime number in the collection.

Specified by:
next in interface java.util.Iterator<java.lang.Integer>
Returns:
The next prime number in the collection.

primorial

public de.luschny.math.arithmetic.Xint primorial()
Computes the product of all primes in the range of this collection.

Returns:
The product of all primes in the range of the collection.

remove

public void remove()
The (optional operation) to remove from the underlying collection the last element returned by the iterator is not supported.

Specified by:
remove in interface java.util.Iterator<java.lang.Integer>
Throws:
java.lang.UnsupportedOperationException

toArray

public int[] toArray()
Gives the prime numbers in the collection as an array.

Specified by:
toArray in interface IPrimeCollection
Returns:
An array of prime numbers representing the collection.

toFile

public void toFile(java.lang.String fileName,
IPrimeCollection.PrintOption format)
            throws java.io.IOException
Writes the primes collection to a file.

Specified by:
toFile in interface IPrimeCollection
Parameters:
fileName - Name of the file to write to.
format - The print-format requested.
Throws:
java.io.IOException

writeComma

private void writeComma(java.io.PrintWriter file)
Prints the list of primes in a comma separated format to a file.

Parameters:
file - The file to be printed to.

writeFormatted

private void writeFormatted(java.io.PrintWriter file)
Prints the list of prime numbers in a formatted way to a file.

Parameters:
file - The file to be printed to.

writeXml

private void writeXml(java.io.PrintWriter file)
Prints the list of prime numbers in XML format to a file.

Parameters:
file - The file to be printed to.