Added number generators

This commit is contained in:
2022-08-20 13:46:58 -04:00
parent e0825fe96e
commit 8f35397177
21 changed files with 441 additions and 17 deletions

View File

@@ -0,0 +1,63 @@
//JavaClasses/src/test/java/mattrixwv/TestSieveOfEratosthenes.java
//Matthew Ellison
// Created: 06-30-21
//Modified: 06-26-22
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
/*
Copyright (C) 2022 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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.mattrixwv.generators;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TestSieveOfEratosthenes{
@Test
public void testSieveOfEratosthenes(){
//Test 1
SieveOfEratosthenes sieve = new SieveOfEratosthenes();
List<Long> correctAnswer = 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<Long> answer = new ArrayList<Long>();
for(int cnt = 0;cnt < 25;++cnt){
long prime = sieve.next();
answer.add(prime);
}
assertEquals(correctAnswer, answer, "SieveOfEratosthenes failed");
assertTrue(sieve.hasNext());
}
@Test
public void testSieveOfEratosthenesBig(){
//Test 1
SieveOfEratosthenesBig sieve = new SieveOfEratosthenesBig();
List<BigInteger> correctAnswer = 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<BigInteger> answer = new ArrayList<BigInteger>();
for(int cnt = 0;cnt < 25;++cnt){
BigInteger prime = sieve.next();
answer.add(prime);
}
assertEquals(correctAnswer, answer, "SieveOfEratosthenesBig failed");
assertTrue(sieve.hasNext());
}
}