public interface IPrimeCollection
An interface for iterating over a prime number sieve or over a subrange of the sieve. Typical use is:
Caveat: IPrimeCollection inherits from java.util.Iterator the method 'remove'. This (optional operation) is usually not supported by an implementation of IPrimeCollection.IPrimeSieve sieve = new PrimeSieve(sieveLength); IPrimeCollection primes = sieve.getCollection(); // Java 1.5 Tiger-style for (int prime : primes) { System.out.print(prime + ","); }
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 |
---|
int getNumberOfPrimes()
double getPrimeDensity()
PositiveRange getPrimeRange()
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.
PositiveRange getSieveRange()
getPrimeRange()
int[] toArray()
void toFile(java.lang.String fileName, IPrimeCollection.PrintOption format) throws java.io.IOException
fileName
- The name of the file the prime numbers are written
to.format
- Sets the type of formatting the output. java.io.IOException