From 1396f412c04909eb7d3861591d3ede85867a0725 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 30 Jun 2021 16:16:22 -0400 Subject: [PATCH] Added Sieve of Eratosthenes implementations --- .../java/mattrixwv/SieveOfEratosthenes.java | 61 ++++++++++++++++++ .../mattrixwv/SieveOfEratosthenesBig.java | 64 +++++++++++++++++++ .../mattrixwv/TestSieveOfEratosthenes.java | 38 +++++++++++ 3 files changed, 163 insertions(+) create mode 100644 src/main/java/mattrixwv/SieveOfEratosthenes.java create mode 100644 src/main/java/mattrixwv/SieveOfEratosthenesBig.java create mode 100644 src/test/java/mattrixwv/TestSieveOfEratosthenes.java diff --git a/src/main/java/mattrixwv/SieveOfEratosthenes.java b/src/main/java/mattrixwv/SieveOfEratosthenes.java new file mode 100644 index 0000000..153a04a --- /dev/null +++ b/src/main/java/mattrixwv/SieveOfEratosthenes.java @@ -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 possiblePrime; + private Map> dict; + + public SieveOfEratosthenes(){ + dict = new HashMap>(); + 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 tempArray = new ArrayList(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 tempArray = new ArrayList(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; + } +} diff --git a/src/main/java/mattrixwv/SieveOfEratosthenesBig.java b/src/main/java/mattrixwv/SieveOfEratosthenesBig.java new file mode 100644 index 0000000..7e62ff4 --- /dev/null +++ b/src/main/java/mattrixwv/SieveOfEratosthenesBig.java @@ -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 possiblePrime; + private Map> dict; + + public SieveOfEratosthenesBig(){ + dict = new HashMap>(); + 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 tempArray = new ArrayList(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 tempArray = new ArrayList(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; + } +} diff --git a/src/test/java/mattrixwv/TestSieveOfEratosthenes.java b/src/test/java/mattrixwv/TestSieveOfEratosthenes.java new file mode 100644 index 0000000..a218ba4 --- /dev/null +++ b/src/test/java/mattrixwv/TestSieveOfEratosthenes.java @@ -0,0 +1,38 @@ +package mattrixwv; + + +import static org.junit.Assert.assertEquals; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Arrays; + +import org.junit.Test; + + +public class TestSieveOfEratosthenes{ + @Test + public void testSieveOfEratosthenes(){ + //Test 1 + SieveOfEratosthenes sieve = new SieveOfEratosthenes(); + ArrayList correctAnswer = new ArrayList(Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L)); + ArrayList answer = new ArrayList(); + for(int cnt = 0;cnt < 25;++cnt){ + long prime = sieve.next(); + answer.add(prime); + } + assertEquals("SieveOfEratosthenes failed", correctAnswer, answer); + } + @Test + public void testSieveOfEratosthenesBig(){ + //Test 1 + SieveOfEratosthenesBig sieve = new SieveOfEratosthenesBig(); + ArrayList correctAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(7), BigInteger.valueOf(11), BigInteger.valueOf(13), BigInteger.valueOf(17), BigInteger.valueOf(19), BigInteger.valueOf(23), BigInteger.valueOf(29), BigInteger.valueOf(31), BigInteger.valueOf(37), BigInteger.valueOf(41), BigInteger.valueOf(43), BigInteger.valueOf(47), BigInteger.valueOf(53), BigInteger.valueOf(59), BigInteger.valueOf(61), BigInteger.valueOf(67), BigInteger.valueOf(71), BigInteger.valueOf(73), BigInteger.valueOf(79), BigInteger.valueOf(83), BigInteger.valueOf(89), BigInteger.valueOf(97))); + ArrayList answer = new ArrayList(); + for(int cnt = 0;cnt < 25;++cnt){ + BigInteger prime = sieve.next(); + answer.add(prime); + } + assertEquals("SieveOfEratosthenesBig failed", correctAnswer, answer); + } +}