de.luschny.math.primes
Interface IPrimeCollection

All Superinterfaces:
java.lang.Iterable<java.lang.Integer>, java.util.Iterator<java.lang.Integer>
All Known Implementing Classes:
PrimeSieve.PrimeCollection

public interface IPrimeCollection
extends java.lang.Iterable<java.lang.Integer>, java.util.Iterator<java.lang.Integer>

An interface for iterating over a prime number sieve or over a subrange of the sieve. Typical use is:

IPrimeSieve sieve = new PrimeSieve(sieveLength);
 IPrimeCollection primes = sieve.getCollection();

   // Java 1.5 Tiger-style
   for (int prime : primes)
   {
      System.out.print(prime + ",");
   }
Caveat: IPrimeCollection inherits from java.util.Iterator the method 'remove'. This (optional operation) is usually not supported by an implementation of IPrimeCollection.

Author:
Peter Luschny

Nested Class Summary
static class IPrimeCollection.PrintOption
          Three options for formatting the list of primes when printing them to a file.
 
Method Summary
 int getNumberOfPrimes()
          Counts 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 SieveRange of the iteration.
 PositiveRange getSieveRange()
          Gives the interval [a,b] proceeded by the sieve.
 int[] toArray()
          Extracts the primes in the SieveRange of the collection to an array.
 void toFile(java.lang.String fileName, IPrimeCollection.PrintOption format)
          Saves the prime numbers in the SieveRange of the collection to a file.
 
Methods inherited from interface java.lang.Iterable
iterator
 
Methods inherited from interface java.util.Iterator
hasNext, next, remove
 

Method Detail

getNumberOfPrimes

int getNumberOfPrimes()
Counts the number of primes in the collection.

Returns:
The number of primes in the collection.

getPrimeDensity

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

Returns:
Ratio of the number of primes relative to the number of all integers in this collection.

getPrimeRange

PositiveRange getPrimeRange()
Gives the range of the indices of the prime numbers in the SieveRange of the iteration. We call this range the PrimeRange of the collection. To understand the difference between "SieveRange" and "PrimeRange" better, let us consider the following example:
If the SieveRange is 10..20, then the PrimeRange is 5..8,
 because of the following table

                    <-     SieveRange           ->
 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23
 x,1,2,x,3,x,4,x,x, x, 5, x, 6, x, x, x, 7, x, 8, x, x, x, 9
                    <-     PrimeRange           ->

 Thus the smallest prime in the range 10..20 is the 5-th prime
 and the largest prime in this range is the 8-th prime.

Returns:
The range of indices of the primes.

getSieveRange

PositiveRange getSieveRange()
Gives the interval [a,b] proceeded by the sieve. We call this interval the SieveRange of the collection.

Returns:
The range of integers, in which the prime numbers are searched.
See Also:
getPrimeRange()

toArray

int[] toArray()
Extracts the primes in the SieveRange of the collection to an array.

Returns:
An array containing the prime numbers in the collection.

toFile

void toFile(java.lang.String fileName,
IPrimeCollection.PrintOption format)
            throws java.io.IOException
Saves the prime numbers in the SieveRange of the collection to a file.

Parameters:
fileName - The name of the file the prime numbers are written to.
format - Sets the type of formatting the output.
Throws:
java.io.IOException