mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-07 07:23:57 -05:00
Updated libraries and increased test coverage
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenesBig.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-30-21
|
||||
//Modified: 06-25-22
|
||||
//Modified: 04-13-23
|
||||
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
|
||||
/*
|
||||
Copyright (C) 2022 Matthew Ellison
|
||||
Copyright (C) 2023 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
|
||||
@@ -31,17 +31,20 @@ import java.util.Map;
|
||||
|
||||
|
||||
public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
|
||||
BigInteger possiblePrime;
|
||||
private Map<BigInteger, ArrayList<BigInteger>> dict;
|
||||
protected BigInteger possiblePrime;
|
||||
protected Map<BigInteger, ArrayList<BigInteger>> dict;
|
||||
|
||||
|
||||
public SieveOfEratosthenesBig(){
|
||||
dict = new HashMap<>();
|
||||
possiblePrime = BigInteger.TWO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext(){
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigInteger next(){
|
||||
BigInteger prime;
|
||||
@@ -71,14 +74,8 @@ public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
|
||||
}
|
||||
//Save that the number is a prime
|
||||
prime = possiblePrime;
|
||||
BigInteger loc = prime.multiply(BigInteger.valueOf(3));
|
||||
if(!dict.containsKey(loc)){
|
||||
ArrayList<BigInteger> tempArray = new ArrayList<>(Arrays.asList(prime));
|
||||
dict.put(loc, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(loc).add(prime);
|
||||
}
|
||||
//Add the next entry to the prime dictionary
|
||||
dict.put(prime.multiply(BigInteger.valueOf(3)), new ArrayList<>(Arrays.asList(prime)));
|
||||
//Move on to the next possible prime
|
||||
possiblePrime = possiblePrime.add(BigInteger.TWO);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user