Added javadoc comments
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenes.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-30-21
|
||||
//Modified: 04-13-23
|
||||
//Modified: 08-11-24
|
||||
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
|
||||
/*
|
||||
Copyright (C) 2023 Matthew Ellison
|
||||
Copyright (C) 2024 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
@@ -30,21 +30,62 @@ import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
/**
|
||||
* A generator for prime numbers using the Sieve of Eratosthenes algorithm, which implements the {@link Iterator} interface.
|
||||
*
|
||||
* <p>
|
||||
* This implementation generates prime numbers in an incremental fashion using a modified version of the Sieve of Eratosthenes algorithm.
|
||||
* The algorithm uses a map to keep track of the multiples of found prime numbers to efficiently determine the next prime.
|
||||
* </p>
|
||||
*/
|
||||
public class SieveOfEratosthenes implements Iterator<Long>{
|
||||
/**
|
||||
* The next possible prime to be found
|
||||
*/
|
||||
protected long possiblePrime;
|
||||
/**
|
||||
* A dictionary of the primes that have been found and their next multiple
|
||||
*/
|
||||
protected Map<Long, ArrayList<Long>> dict;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new SieveOfEratosthenes instance with an empty prime dictionary
|
||||
* and starts the search from the first possible prime number, 2.
|
||||
*/
|
||||
public SieveOfEratosthenes(){
|
||||
dict = new HashMap<>();
|
||||
possiblePrime = 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether there is a next prime number available.
|
||||
*
|
||||
* <p>
|
||||
* This method always returns {@code true} as the iterator is designed to
|
||||
* generate primes indefinitely.
|
||||
* </p>
|
||||
*
|
||||
* @return {@code true}, as the iterator can generate an infinite number of primes
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext(){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next prime number.
|
||||
*
|
||||
* <p>
|
||||
* The method generates the next prime number by checking and updating the
|
||||
* internal map of known multiples. The first prime returned is 2, and subsequent
|
||||
* primes are found by incrementing the possible prime number and checking its
|
||||
* primality using the map.
|
||||
* </p>
|
||||
*
|
||||
* @return the next prime number
|
||||
* @throws NoSuchElementException if the next prime cannot be represented by a {@code long}
|
||||
*/
|
||||
@Override
|
||||
public Long next(){
|
||||
long prime;
|
||||
|
||||
Reference in New Issue
Block a user