Package net.n1da.dev.euler.helper
Class Sieve
- java.lang.Object
-
- net.n1da.dev.euler.helper.Sieve
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Integer>
,java.util.Enumeration<java.lang.Integer>
,java.util.Iterator<java.lang.Integer>
public class Sieve extends java.lang.Object implements java.lang.Iterable<java.lang.Integer>, java.util.Iterator<java.lang.Integer>, java.util.Enumeration<java.lang.Integer>
This class is a generator of primes using the "Sieve of Eratosthenes".- Since:
- 19.07.2015 22:36:14
- Author:
- Nico Danneberg
- See Also:
- Sieve of Eratosthenes @ Wikipedia,
Enumeration
,Iterable
,Iterator
-
-
Constructor Summary
Constructors Constructor Description Sieve(int max)
Creates a new sieve with the given maximum range.
-
Method Summary
Modifier and Type Method Description java.util.Enumeration<java.lang.Integer>
elements()
Resets
the current sieve and returns a reference asEnumeration
.boolean
hasMoreElements()
Checks if there are more primes in the sieve in relation to the current position in the collection of all primes.boolean
hasNext()
Checks if there are more primes in the sieve in relation to the current position in the collection of all primes.java.util.Iterator<java.lang.Integer>
iterator()
Resets
the current sieve and returns a reference asIterator
.java.lang.Integer
next()
Tries to find the next prime in the sieve.java.lang.Integer
nextElement()
Tries to find the next prime in the sieve.void
reset()
Resets the sieve to its initial values.
-
-
-
Method Detail
-
elements
public java.util.Enumeration<java.lang.Integer> elements()
Resets
the current sieve and returns a reference asEnumeration
.- Returns:
- An enumeration over all primes in this sieve.
- See Also:
Enumeration
-
hasMoreElements
public boolean hasMoreElements()
Checks if there are more primes in the sieve in relation to the current position in the collection of all primes.- Specified by:
hasMoreElements
in interfacejava.util.Enumeration<java.lang.Integer>
- Returns:
- true, if there is at least one more prime
- See Also:
hasNext()
-
hasNext
public boolean hasNext()
Checks if there are more primes in the sieve in relation to the current position in the collection of all primes.- Specified by:
hasNext
in interfacejava.util.Iterator<java.lang.Integer>
- Returns:
- true, if there is at least one more prime
- See Also:
hasMoreElements()
-
iterator
public java.util.Iterator<java.lang.Integer> iterator()
Resets
the current sieve and returns a reference asIterator
.- Specified by:
iterator
in interfacejava.lang.Iterable<java.lang.Integer>
- Returns:
- An iterator over all primes in this sieve.
- See Also:
Iterator
-
next
public java.lang.Integer next() throws java.util.NoSuchElementException
Tries to find the next prime in the sieve.- Specified by:
next
in interfacejava.util.Iterator<java.lang.Integer>
- Returns:
- The next prime in the list of elements in this sieve beginning at 2. If the end of all elements is reached an exception is thrown.
- Throws:
java.util.NoSuchElementException
- See Also:
nextElement()
-
nextElement
public java.lang.Integer nextElement() throws java.util.NoSuchElementException
Tries to find the next prime in the sieve.- Specified by:
nextElement
in interfacejava.util.Enumeration<java.lang.Integer>
- Returns:
- The next prime in the list of elements in this sieve beginning at 2. If the end of all elements is reached an exception is thrown.
- Throws:
java.util.NoSuchElementException
- See Also:
next()
-
reset
public void reset()
Resets the sieve to its initial values.
-
-