mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-07 07:23:57 -05:00
Added javadoc comments
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenesBig.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
|
||||
@@ -28,23 +28,65 @@ import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
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 SieveOfEratosthenesBig implements Iterator<BigInteger>{
|
||||
/**
|
||||
* The next possible prime to be found
|
||||
*/
|
||||
protected BigInteger possiblePrime;
|
||||
/**
|
||||
* A dictionary of the primes that have been found and their next multiple
|
||||
*/
|
||||
protected Map<BigInteger, ArrayList<BigInteger>> dict;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new SieveOfEratosthenes instance with an empty prime dictionary
|
||||
* and starts the search from the first possible prime number, 2.
|
||||
*/
|
||||
public SieveOfEratosthenesBig(){
|
||||
dict = new HashMap<>();
|
||||
possiblePrime = BigInteger.TWO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 BigInteger next(){
|
||||
BigInteger prime;
|
||||
@@ -72,6 +114,10 @@ public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
|
||||
//Delete the current entry
|
||||
dict.remove(possiblePrime);
|
||||
}
|
||||
//Protect against overflows
|
||||
if(possiblePrime.compareTo(BigInteger.ZERO) < 0){
|
||||
throw new NoSuchElementException("the next prime cannot be described by a long");
|
||||
}
|
||||
//Save that the number is a prime
|
||||
prime = possiblePrime;
|
||||
//Add the next entry to the prime dictionary
|
||||
|
||||
Reference in New Issue
Block a user