Added Sieve of Eratosthenes implementations
This commit is contained in:
61
src/main/java/mattrixwv/SieveOfEratosthenes.java
Normal file
61
src/main/java/mattrixwv/SieveOfEratosthenes.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package mattrixwv;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class SieveOfEratosthenes implements Iterator<Long>{
|
||||
long possiblePrime;
|
||||
private Map<Long, ArrayList<Long>> dict;
|
||||
|
||||
public SieveOfEratosthenes(){
|
||||
dict = new HashMap<Long, ArrayList<Long>>();
|
||||
possiblePrime = 2;
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext(){
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public Long next(){
|
||||
long prime;
|
||||
if(possiblePrime > 2){
|
||||
//Loop until you find a prime number
|
||||
for(;dict.containsKey(possiblePrime);possiblePrime += 2){
|
||||
//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<Long>(Arrays.asList(num));
|
||||
dict.put(possiblePrime + num + num, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(possiblePrime + num + num).add(num);
|
||||
}
|
||||
}
|
||||
//Delete the current entry
|
||||
dict.remove(possiblePrime);
|
||||
}
|
||||
//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<Long>(Arrays.asList(prime));
|
||||
dict.put(prime * 3, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(prime * 3).add(prime);
|
||||
}
|
||||
//Move on to the next possible prime
|
||||
possiblePrime += 2;
|
||||
}
|
||||
else{
|
||||
//Return 2 and move to 3
|
||||
prime = possiblePrime++;
|
||||
}
|
||||
return prime;
|
||||
}
|
||||
}
|
||||
64
src/main/java/mattrixwv/SieveOfEratosthenesBig.java
Normal file
64
src/main/java/mattrixwv/SieveOfEratosthenesBig.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package mattrixwv;
|
||||
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
|
||||
BigInteger possiblePrime;
|
||||
private Map<BigInteger, ArrayList<BigInteger>> dict;
|
||||
|
||||
public SieveOfEratosthenesBig(){
|
||||
dict = new HashMap<BigInteger, ArrayList<BigInteger>>();
|
||||
possiblePrime = BigInteger.TWO;
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext(){
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public BigInteger next(){
|
||||
BigInteger prime;
|
||||
if(possiblePrime.compareTo(BigInteger.TWO) > 0){
|
||||
//Loop until you find a prime number
|
||||
for(;dict.containsKey(possiblePrime);possiblePrime = possiblePrime.add(BigInteger.TWO)){
|
||||
//Create the next entry for all entries in the map
|
||||
for(BigInteger num : dict.get(possiblePrime)){
|
||||
BigInteger loc = possiblePrime.add(num).add(num);
|
||||
if(!dict.containsKey(loc)){
|
||||
ArrayList<BigInteger> tempArray = new ArrayList<BigInteger>(Arrays.asList(num));
|
||||
dict.put(loc, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(loc).add(num);
|
||||
}
|
||||
}
|
||||
//Delete the current entry
|
||||
dict.remove(possiblePrime);
|
||||
}
|
||||
//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<BigInteger>(Arrays.asList(prime));
|
||||
dict.put(loc, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(loc).add(prime);
|
||||
}
|
||||
//Move on to the next possible prime
|
||||
possiblePrime = possiblePrime.add(BigInteger.TWO);
|
||||
}
|
||||
else{
|
||||
//Return 2 and move to 3
|
||||
prime = possiblePrime;
|
||||
possiblePrime = possiblePrime.add(BigInteger.ONE);
|
||||
}
|
||||
return prime;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user