Updated libraries and increased test coverage

This commit is contained in:
2023-04-13 19:12:33 -04:00
parent 783fc7009c
commit f4dbf4e4dc
20 changed files with 560 additions and 227 deletions

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenes.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,37 +31,36 @@ import java.util.NoSuchElementException;
public class SieveOfEratosthenes implements Iterator<Long>{
long possiblePrime;
private Map<Long, ArrayList<Long>> dict;
protected long possiblePrime;
protected Map<Long, ArrayList<Long>> dict;
public SieveOfEratosthenes(){
dict = new HashMap<>();
possiblePrime = 2;
}
@Override
public boolean hasNext(){
return true;
}
@Override
public Long next(){
long prime;
//If this is the first run just return 2
if(possiblePrime <= 2){
if(possiblePrime == 2){
prime = possiblePrime++;
return prime;
}
//Loop until you find a prime number
for(;dict.containsKey(possiblePrime);possiblePrime += 2){
if(possiblePrime < 0){
throw new NoSuchElementException("the next prime cannot be described by a long");
}
//Create the next entry for all entries in the map
for(long num : dict.get(possiblePrime)){
if(!dict.containsKey(possiblePrime + num + num)){
ArrayList<Long> tempArray = new ArrayList<>(Arrays.asList(num));
dict.put(possiblePrime + num + num, tempArray);
dict.put(possiblePrime + num + num, new ArrayList<>(Arrays.asList(num)));
}
else{
dict.get(possiblePrime + num + num).add(num);
@@ -70,16 +69,14 @@ public class SieveOfEratosthenes implements Iterator<Long>{
//Delete the current entry
dict.remove(possiblePrime);
}
//Protect against overflows
if(possiblePrime < 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
if(!dict.containsKey(prime * 3)){
ArrayList<Long> tempArray = new ArrayList<>(Arrays.asList(prime));
dict.put(prime * 3, tempArray);
}
else{
dict.get(prime * 3).add(prime);
}
//Add the next entry to the prime dictionary
dict.put(prime * 3, new ArrayList<>(Arrays.asList(prime)));
//Move on to the next possible prime
possiblePrime += 2;