From 4ec13464762cf42bacff5a0393da05b5723a8b0b Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 26 Jun 2022 11:53:01 -0400 Subject: [PATCH] Updated sonarqube findings --- src/main/java/mattrixwv/ArrayAlgorithms.java | 58 ++--- src/main/java/mattrixwv/NumberAlgorithms.java | 228 +++++++----------- .../java/mattrixwv/SieveOfEratosthenes.java | 68 +++--- .../mattrixwv/SieveOfEratosthenesBig.java | 70 +++--- src/main/java/mattrixwv/StringAlgorithms.java | 16 +- .../java/mattrixwv/TestNumberAlgorithms.java | 106 +++++--- .../java/mattrixwv/TestStringAlgorithms.java | 7 +- 7 files changed, 262 insertions(+), 291 deletions(-) diff --git a/src/main/java/mattrixwv/ArrayAlgorithms.java b/src/main/java/mattrixwv/ArrayAlgorithms.java index 3f51191..8a7d04f 100644 --- a/src/main/java/mattrixwv/ArrayAlgorithms.java +++ b/src/main/java/mattrixwv/ArrayAlgorithms.java @@ -1,10 +1,10 @@ //JavaClasses/src/main/java/mattrixwv/ArrayAlgorithms.java //Matthew Ellison // Created: 07-03-21 -//Modified: 07-03-21 +//Modified: 06-25-22 //This class contains algorithms for vectors that I've found it useful to keep around /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -23,17 +23,13 @@ package mattrixwv; import java.math.BigInteger; -import java.util.ArrayList; +import java.util.StringJoiner; public class ArrayAlgorithms{ + private ArrayAlgorithms(){} //This function returns the sum of all elements in the list - public static int getSum(ArrayList nums){ - //If a blank list was passed to the function return 0 as the sum - if(nums.size() == 0){ - return 0; - } - + public static int getSum(Iterable nums){ //Setup the variables int sum = 0; @@ -45,14 +41,9 @@ public class ArrayAlgorithms{ //Return the sum of all elements return sum; } - public static long getLongSum(ArrayList nums){ - //If a blank list was passed to the function return 0 as the sum - if(nums.size() == 0){ - return 0L; - } - + public static long getLongSum(Iterable nums){ //Setup the variables - long sum = 0L; + long sum = 0; //Loop through every element in the list and add them together for(long num : nums){ @@ -62,14 +53,9 @@ public class ArrayAlgorithms{ //Return the sum of all elements return sum; } - public static BigInteger getBigSum(ArrayList nums){ - //If a blank list was passed to the function return 0 as the sum - if(nums.size() == 0){ - return BigInteger.valueOf(0); - } - + public static BigInteger getBigSum(Iterable nums){ //Setup the variables - BigInteger sum = BigInteger.valueOf(0); + BigInteger sum = BigInteger.ZERO; //Loop through every element in the list and add them together for(BigInteger num : nums){ @@ -80,9 +66,9 @@ public class ArrayAlgorithms{ return sum; } //This function returns the product of all elements in the list - public static int getProd(ArrayList nums){ + public static int getProd(Iterable nums){ //If a blank list was passed tot he fuction return 0 as the product - if(nums.size() == 0){ + if(!nums.iterator().hasNext()){ return 0; } @@ -97,9 +83,9 @@ public class ArrayAlgorithms{ //Return the product of all elements return product; } - public static long getLongProd(ArrayList nums){ + public static long getLongProd(Iterable nums){ //If a blank list was passed tot he fuction return 0 as the product - if(nums.size() == 0){ + if(!nums.iterator().hasNext()){ return 0L; } @@ -114,9 +100,9 @@ public class ArrayAlgorithms{ //Return the product of all elements return product; } - public static BigInteger getBigProd(ArrayList nums){ + public static BigInteger getBigProd(Iterable nums){ //If a blank list was passed tot he fuction return 0 as the product - if(nums.size() == 0){ + if(!nums.iterator().hasNext()){ return BigInteger.valueOf(0); } @@ -132,15 +118,11 @@ public class ArrayAlgorithms{ return product; } //Print a list - public static String printList(ArrayList list){ - StringBuilder listString = new StringBuilder("["); - for(int cnt = 0;cnt < list.size();++cnt){ - listString.append(list.get(cnt)); - if(cnt < list.size() - 1){ - listString.append(", "); - } + public static String printList(Iterable list){ + StringJoiner returnString = new StringJoiner(", ", "[", "]"); + for(T obj : list){ + returnString.add(obj.toString()); } - listString.append("]"); - return listString.toString(); + return returnString.toString(); } } diff --git a/src/main/java/mattrixwv/NumberAlgorithms.java b/src/main/java/mattrixwv/NumberAlgorithms.java index 9a9ba66..c06c9b7 100644 --- a/src/main/java/mattrixwv/NumberAlgorithms.java +++ b/src/main/java/mattrixwv/NumberAlgorithms.java @@ -1,10 +1,10 @@ //JavaClasses/src/main/java/mattrixwv/NumberAlgorithms.java //Matthew Ellison // Created: 07-03-21 -//Modified: 07-03-21 +//Modified: 06-25-22 //This class contains algorithms for numbers that I've found it useful to keep around /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -26,18 +26,22 @@ import java.math.BigInteger; import java.security.InvalidParameterException; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; +import java.util.List; import mattrixwv.exceptions.InvalidResult; public class NumberAlgorithms{ + private NumberAlgorithms(){} //?This is here just to prove that templates exist and for a possible rewrite at a later time public static T getNum(T num1){ return num1; } + //This function returns a list with all the prime numbers <= goalNumber - public static ArrayList getPrimes(Integer goalNumber){ - ArrayList primes = new ArrayList(); //Holds the prime numbers + public static List getPrimes(Integer goalNumber){ + ArrayList primes = new ArrayList<>(); //Holds the prime numbers boolean foundFactor = false; //A flag for whether a factor of the current number has been found //If the number is 0 or negative return an empty list @@ -54,18 +58,11 @@ public class NumberAlgorithms{ //Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor Double topPossibleFactor = Math.ceil(Math.sqrt(possiblePrime)); //We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this - for(int primesCnt = 0;primes.get(primesCnt) <= topPossibleFactor.intValue();){ + for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt) <= topPossibleFactor.intValue());++primesCnt){ if((possiblePrime % primes.get(primesCnt)) == 0){ foundFactor = true; break; } - else{ - ++primesCnt; - } - //Check if the index has gone out of range - if(primesCnt >= primes.size()){ - break; - } } //If you didn't find a factor then the current number must be prime @@ -81,8 +78,8 @@ public class NumberAlgorithms{ Collections.sort(primes); return primes; } - public static ArrayList getPrimes(Long goalNumber){ - ArrayList primes = new ArrayList(); //Holds the prime numbers + public static List getPrimes(Long goalNumber){ + ArrayList primes = new ArrayList<>(); //Holds the prime numbers boolean foundFactor = false; //A flag for whether a factor of the current number has been found //If the numebr is 0 or negative return an empty list @@ -99,18 +96,11 @@ public class NumberAlgorithms{ //Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor Double topPossibleFactor = Math.ceil(Math.sqrt(possiblePrime)); //We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this - for(int primesCnt = 0;primes.get(primesCnt) <= topPossibleFactor.intValue();){ + for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt) <= topPossibleFactor.intValue());++primesCnt){ if((possiblePrime % primes.get(primesCnt)) == 0){ foundFactor = true; break; } - else{ - ++primesCnt; - } - //Check if the index has gone out of range - if(primesCnt >= primes.size()){ - break; - } } //If you didn't find a factor then the current number must be prime @@ -126,8 +116,8 @@ public class NumberAlgorithms{ Collections.sort(primes); return primes; } - public static ArrayList getPrimes(BigInteger goalNumber){ - ArrayList primes = new ArrayList(); //Holds the prime numbers + public static List getPrimes(BigInteger goalNumber){ + ArrayList primes = new ArrayList<>(); //Holds the prime numbers boolean foundFactor = false; //A flag for whether a factor of the current number has been found //If the number is 1, 0 or negative return an empty list @@ -144,18 +134,11 @@ public class NumberAlgorithms{ //Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor BigInteger topPossibleFactor = possiblePrime.sqrt().add(BigInteger.valueOf(1)); //We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this - for(int primesCnt = 0;primes.get(primesCnt).compareTo(topPossibleFactor) <= 0;){ + for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt).compareTo(topPossibleFactor) <= 0);++primesCnt){ if((possiblePrime.mod(primes.get(primesCnt))) == BigInteger.valueOf(0)){ foundFactor = true; break; } - else{ - ++primesCnt; - } - //Check if the index has gone out of range - if(primesCnt >= primes.size()){ - break; - } } //If you didn't find a factor then the current number must be prime @@ -171,9 +154,11 @@ public class NumberAlgorithms{ Collections.sort(primes); return primes; } + + //This function gets a certain number of primes - public static ArrayList getNumPrimes(Integer numberOfPrimes){ - ArrayList primes = new ArrayList(); //Holds the prime numbers + public static List getNumPrimes(Integer numberOfPrimes){ + ArrayList primes = new ArrayList<>(); //Holds the prime numbers boolean foundFactor = false; //A flag for whether a factor of the current number has been found //If the number is 0 or negative return an empty list @@ -190,18 +175,11 @@ public class NumberAlgorithms{ //Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor Double topPossibleFactor = Math.ceil(Math.sqrt(possiblePrime)); //We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this - for(int primesCnt = 0;primes.get(primesCnt) <= topPossibleFactor.intValue();){ + for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt) <= topPossibleFactor.intValue());++primesCnt){ if((possiblePrime % primes.get(primesCnt)) == 0){ foundFactor = true; break; } - else{ - ++primesCnt; - } - //Check if the index has gone out of bounds - if(primesCnt >= primes.size()){ - break; - } } //If you didn't find a factor then the current number must be prime @@ -217,8 +195,8 @@ public class NumberAlgorithms{ Collections.sort(primes); return primes; } - public static ArrayList getNumPrimes(Long numberOfPrimes){ - ArrayList primes = new ArrayList(); //Holds the prime numbers + public static List getNumPrimes(Long numberOfPrimes){ + ArrayList primes = new ArrayList<>(); //Holds the prime numbers boolean foundFactor = false; //A flag for whether a factor of the current number has been found //If the number is 0 or negative return an empty list @@ -235,18 +213,11 @@ public class NumberAlgorithms{ //Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor Double topPossibleFactor = Math.ceil(Math.sqrt(possiblePrime)); //We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this - for(int primesCnt = 0;primes.get(primesCnt) <= topPossibleFactor.intValue();){ + for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt) <= topPossibleFactor.intValue());++primesCnt){ if((possiblePrime % primes.get(primesCnt)) == 0){ foundFactor = true; break; } - else{ - ++primesCnt; - } - //Check if the index has gone out of bounds - if(primesCnt >= primes.size()){ - break; - } } //If you didn't find a factor then the current number must be prime @@ -262,8 +233,8 @@ public class NumberAlgorithms{ Collections.sort(primes); return primes; } - public static ArrayList getNumPrimes(BigInteger numberOfPrimes){ - ArrayList primes = new ArrayList(); //Holds the prime numbers + public static List getNumPrimes(BigInteger numberOfPrimes){ + ArrayList primes = new ArrayList<>(); //Holds the prime numbers boolean foundFactor = false; //A flag for whether a factor of the current number has been found //If the number is 0 or negative return an empty list @@ -280,18 +251,11 @@ public class NumberAlgorithms{ //Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor BigInteger topPossibleFactor = possiblePrime.sqrt().add(BigInteger.valueOf(1)); //We can safely assume that there will be at least 1 element in the primes list because of 2 being added before this - for(int primesCnt = 0;primes.get(primesCnt).compareTo(topPossibleFactor) <= 0;){ + for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt).compareTo(topPossibleFactor) <= 0);++primesCnt){ if((possiblePrime.mod(primes.get(primesCnt))) == BigInteger.valueOf(0)){ foundFactor = true; break; } - else{ - ++primesCnt; - } - //Check if the index has gone out of bounds - if(primesCnt >= primes.size()){ - break; - } } //If you didn't find a factor then the current number must be prime @@ -307,6 +271,8 @@ public class NumberAlgorithms{ Collections.sort(primes); return primes; } + + //This function return true if the value passed to it is prime public static boolean isPrime(int possiblePrime){ if(possiblePrime <= 3){ @@ -350,12 +316,14 @@ public class NumberAlgorithms{ } return true; } + + //This function returns all factors of goalNumber - public static ArrayList getFactors(Integer goalNumber) throws InvalidResult{ + public static List getFactors(Integer goalNumber) throws InvalidResult{ //You need to get all the primes that could be factors of this number so you can test them Double topPossiblePrime = Math.ceil(Math.sqrt(goalNumber)); - ArrayList primes = getPrimes(topPossiblePrime.intValue()); - ArrayList factors = new ArrayList(); + List primes = getPrimes(topPossiblePrime.intValue()); + ArrayList factors = new ArrayList<>(); //You need to step through each prime and see if it is a factor in the number for(int cnt = 0;cnt < primes.size();){ @@ -372,7 +340,7 @@ public class NumberAlgorithms{ } //If you didn't get any factors the number itself must be a prime - if(factors.size() == 0){ + if(factors.isEmpty()){ factors.add(goalNumber); goalNumber /= goalNumber; } @@ -385,11 +353,11 @@ public class NumberAlgorithms{ //Return the list of factors return factors; } - public static ArrayList getFactors(Long goalNumber) throws InvalidResult{ + public static List getFactors(Long goalNumber) throws InvalidResult{ //You need to get all the primes that could be factors of this number so you can test them Double topPossiblePrime = Math.ceil(Math.sqrt(goalNumber)); - ArrayList primes = getPrimes(topPossiblePrime.longValue()); - ArrayList factors = new ArrayList(); + List primes = getPrimes(topPossiblePrime.longValue()); + ArrayList factors = new ArrayList<>(); //You need to step through each prime and see if it is a factor in the number for(int cnt = 0;cnt < primes.size();){ @@ -406,7 +374,7 @@ public class NumberAlgorithms{ } //If you didn't get any factors the number itself must be a prime - if(factors.size() == 0){ + if(factors.isEmpty()){ factors.add(goalNumber); goalNumber /= goalNumber; } @@ -419,11 +387,11 @@ public class NumberAlgorithms{ //Return the list of factors return factors; } - public static ArrayList getFactors(BigInteger goalNumber) throws InvalidResult{ + public static List getFactors(BigInteger goalNumber) throws InvalidResult{ //You need to get all the primes that could be factors of this number so you can test them BigInteger topPossiblePrime = goalNumber.sqrt(); - ArrayList primes = getPrimes(topPossiblePrime); - ArrayList factors = new ArrayList(); + List primes = getPrimes(topPossiblePrime); + ArrayList factors = new ArrayList<>(); //You need to step through each prime and see if it is a factor in the number for(int cnt = 0;cnt < primes.size();){ @@ -440,9 +408,9 @@ public class NumberAlgorithms{ } //If you didn't get any factors the number itself must be a prime - if(factors.size() == 0){ + if(factors.isEmpty()){ factors.add(goalNumber); - goalNumber.divide(goalNumber); + goalNumber = goalNumber.divide(goalNumber); } //If for some reason the goalNumber is not 1 throw an error @@ -453,109 +421,95 @@ public class NumberAlgorithms{ //Return the list of factors return factors; } + + //This function returns all the divisors of goalNumber - public static ArrayList getDivisors(Integer goalNumber){ - ArrayList divisors = new ArrayList(); + public static List getDivisors(Integer goalNumber){ + HashSet divisors = new HashSet<>(); //Start by checking that the number is positive if(goalNumber <= 0){ - return divisors; + return new ArrayList<>(); } - //If the number is 1 return just itself - else if(goalNumber == 1){ + else{ divisors.add(1); - return divisors; + divisors.add(goalNumber); } //Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly Double topPossibleDivisor = Math.ceil(Math.sqrt(goalNumber)); - for(int possibleDivisor = 1;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ + for(int possibleDivisor = 2;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ //If you find one add it and the number it creates to the list if((goalNumber % possibleDivisor) == 0){ + int possibleDivisor2 = goalNumber / possibleDivisor; divisors.add(possibleDivisor); - //Account for the possibility of sqrt(goalNumber) being a divisor - if(possibleDivisor != topPossibleDivisor.intValue()){ - divisors.add(goalNumber / possibleDivisor); - } - //Take care of a few occations where a number was added twice - if(divisors.get(divisors.size() - 1) == (possibleDivisor + 1)){ - ++possibleDivisor; - } + divisors.add(possibleDivisor2); } } + //Convert the set to a list + ArrayList divisorList = new ArrayList<>(divisors); //Sort the list before returning it for neatness - Collections.sort(divisors); + Collections.sort(divisorList); //Return the list - return divisors; + return divisorList; } - public static ArrayList getDivisors(Long goalNumber){ - ArrayList divisors = new ArrayList(); + public static List getDivisors(Long goalNumber){ + HashSet divisors = new HashSet<>(); //Start by checking that the number is positive if(goalNumber <= 0){ - return divisors; + return new ArrayList<>(); } - //If the number is 1 return just itself - else if(goalNumber == 1){ + else{ divisors.add(1L); - return divisors; + divisors.add(goalNumber); } //Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly Double topPossibleDivisor = Math.ceil(Math.sqrt(goalNumber)); - for(long possibleDivisor = 1L;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ + for(long possibleDivisor = 2L;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ //If you find one add it and the number it creates to the list if((goalNumber % possibleDivisor) == 0){ + long possibleDivisor2 = goalNumber / possibleDivisor; divisors.add(possibleDivisor); - //Account for the possibility of sqrt(goalNumber) being a divisor - if(possibleDivisor != topPossibleDivisor.longValue()){ - divisors.add(goalNumber / possibleDivisor); - } - //Take care of a few occations where a number was added twice - if(divisors.get(divisors.size() - 1) == (possibleDivisor + 1L)){ - ++possibleDivisor; - } + divisors.add(possibleDivisor2); } } + ArrayList divisorList = new ArrayList<>(divisors); //Sort the list before returning it for neatness - Collections.sort(divisors); + Collections.sort(divisorList); //Return the list - return divisors; + return divisorList; } - public static ArrayList getDivisors(BigInteger goalNumber){ - ArrayList divisors = new ArrayList(); + public static List getDivisors(BigInteger goalNumber){ + HashSet divisors = new HashSet<>(); //Start by checking that the number is positive if(goalNumber.compareTo(BigInteger.valueOf(0)) <= 0){ - return divisors; + return new ArrayList<>(); } - //If the number is 1 return just itself - else if(goalNumber.equals(BigInteger.valueOf(1))){ + else{ divisors.add(BigInteger.valueOf(1)); - return divisors; + divisors.add(goalNumber); } //Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly BigInteger topPossibleDivisor = goalNumber.sqrt(); - for(BigInteger possibleDivisor = BigInteger.valueOf(1);possibleDivisor.compareTo(topPossibleDivisor) <= 0;possibleDivisor = possibleDivisor.add(BigInteger.valueOf(1))){ + for(BigInteger possibleDivisor = BigInteger.TWO;possibleDivisor.compareTo(topPossibleDivisor) <= 0;possibleDivisor = possibleDivisor.add(BigInteger.valueOf(1))){ //If you find one add it and the number it creates to the list if(goalNumber.mod(possibleDivisor).equals(BigInteger.valueOf(0))){ + BigInteger possibleDivisor2 = goalNumber.divide(possibleDivisor); divisors.add(possibleDivisor); - //Account for the possibility of sqrt(goalNumber) being a divisor - if(!possibleDivisor.equals(topPossibleDivisor)){ - divisors.add(goalNumber.divide(possibleDivisor)); - } - //Take care of a few occations where a number was added twice - if(divisors.get(divisors.size() - 1).equals(possibleDivisor.add(BigInteger.valueOf(1L)))){ - possibleDivisor = possibleDivisor.add(BigInteger.valueOf(1)); - } + divisors.add(possibleDivisor2); } } + ArrayList divisorList = new ArrayList<>(divisors); //Sort the list before returning it for neatness - Collections.sort(divisors); + Collections.sort(divisorList); //Return the list - return divisors; + return divisorList; } + //This function returns the goalSubscript'th Fibonacci number public static int getFib(int goalSubscript){ //Setup the variables @@ -567,7 +521,7 @@ public class NumberAlgorithms{ } //Loop through the list, generating Fibonacci numbers until it finds the correct subscript - int fibLoc = 2; + int fibLoc; for(fibLoc = 2;fibLoc < goalSubscript;++fibLoc){ fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3]; } @@ -585,7 +539,7 @@ public class NumberAlgorithms{ } //Loop through the list, generating Fibonacci numbers until it finds the correct subscript - int fibLoc = 2; + int fibLoc; for(fibLoc = 2;fibLoc < goalSubscript;++fibLoc){ fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3]; } @@ -603,7 +557,7 @@ public class NumberAlgorithms{ } //Loop through the list, generating Fibonacci numbers until it finds the correct subscript - int fibLoc = 2; + int fibLoc; for(fibLoc = 2;goalSubscript.compareTo(BigInteger.valueOf(fibLoc)) > 0;++fibLoc){ fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3].add(fibNums[(fibLoc - 2) % 3]); } @@ -611,10 +565,11 @@ public class NumberAlgorithms{ //Return the proper number. The location counter is 1 off of the subscript return fibNums[(fibLoc - 1) % 3]; } + //This function returns a list of all Fibonacci numbers <= goalNumber - public static ArrayList getAllFib(Integer goalNumber){ + public static List getAllFib(Integer goalNumber){ //Setup the variables - ArrayList fibNums = new ArrayList(); //A list to save the Fibonacci numbers + ArrayList fibNums = new ArrayList<>(); //A list to save the Fibonacci numbers //If the number is <= 0 return an empty list if(goalNumber <= 0){ @@ -634,9 +589,9 @@ public class NumberAlgorithms{ fibNums.remove(fibNums.size() - 1); return fibNums; } - public static ArrayList getAllFib(Long goalNumber){ + public static List getAllFib(Long goalNumber){ //Setup the variables - ArrayList fibNums = new ArrayList(); //A list to save the Fibonacci numbers + ArrayList fibNums = new ArrayList<>(); //A list to save the Fibonacci numbers //If the number is <= 0 return an empty list if(goalNumber <= 0){ @@ -656,9 +611,9 @@ public class NumberAlgorithms{ fibNums.remove(fibNums.size() - 1); return fibNums; } - public static ArrayList getAllFib(BigInteger goalNumber){ + public static List getAllFib(BigInteger goalNumber){ //Setup the variables - ArrayList fibNums = new ArrayList(); //A list to save the Fibonacci numbers + ArrayList fibNums = new ArrayList<>(); //A list to save the Fibonacci numbers //If the number is <= 0 return an empty list if(goalNumber.compareTo(BigInteger.valueOf(0)) <= 0){ @@ -678,6 +633,7 @@ public class NumberAlgorithms{ fibNums.remove(fibNums.size() - 1); return fibNums; } + //This function returns the factorial of the number passed to it public static int factorial(int num) throws InvalidParameterException{ int fact = 1; //The value of the factorial @@ -721,6 +677,7 @@ public class NumberAlgorithms{ return fact; } + //This function returns the GCD of the two numbers sent to it public static int gcd(int num1, int num2){ while((num1 != 0) && (num2 != 0)){ @@ -755,6 +712,7 @@ public class NumberAlgorithms{ } return num1.or(num2); } + //Converts a number to its binary equivalent public static String toBin(int num){ //Convert the number to a binary string diff --git a/src/main/java/mattrixwv/SieveOfEratosthenes.java b/src/main/java/mattrixwv/SieveOfEratosthenes.java index d9e264d..fd17cf6 100644 --- a/src/main/java/mattrixwv/SieveOfEratosthenes.java +++ b/src/main/java/mattrixwv/SieveOfEratosthenes.java @@ -1,10 +1,10 @@ //JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenes.java //Matthew Ellison // Created: 06-30-21 -//Modified: 06-30-21 +//Modified: 06-25-22 //This class uses to Sieve of Eratosthenes to generate an infinite number of primes /* -Copyright (C) 2021 Matthew Ellison +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 @@ -27,6 +27,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import java.util.NoSuchElementException; public class SieveOfEratosthenes implements Iterator{ @@ -34,7 +35,7 @@ public class SieveOfEratosthenes implements Iterator{ private Map> dict; public SieveOfEratosthenes(){ - dict = new HashMap>(); + dict = new HashMap<>(); possiblePrime = 2; } @Override @@ -44,39 +45,44 @@ public class SieveOfEratosthenes implements Iterator{ @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); - } + + //If this is the first run just return 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 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; + //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{ - //Return 2 and move to 3 - prime = possiblePrime++; + dict.get(prime * 3).add(prime); } + //Move on to the next possible prime + possiblePrime += 2; + return prime; } } diff --git a/src/main/java/mattrixwv/SieveOfEratosthenesBig.java b/src/main/java/mattrixwv/SieveOfEratosthenesBig.java index 1e7db98..b903789 100644 --- a/src/main/java/mattrixwv/SieveOfEratosthenesBig.java +++ b/src/main/java/mattrixwv/SieveOfEratosthenesBig.java @@ -1,10 +1,10 @@ //JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenesBig.java //Matthew Ellison // Created: 06-30-21 -//Modified: 06-30-21 +//Modified: 06-25-22 //This class uses to Sieve of Eratosthenes to generate an infinite number of primes /* -Copyright (C) 2021 Matthew Ellison +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 @@ -35,7 +35,7 @@ public class SieveOfEratosthenesBig implements Iterator{ private Map> dict; public SieveOfEratosthenesBig(){ - dict = new HashMap>(); + dict = new HashMap<>(); possiblePrime = BigInteger.TWO; } @Override @@ -45,41 +45,43 @@ public class SieveOfEratosthenesBig implements Iterator{ @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{ + + if(possiblePrime.compareTo(BigInteger.TWO) <= 0){ //Return 2 and move to 3 prime = possiblePrime; possiblePrime = possiblePrime.add(BigInteger.ONE); + return prime; } + + //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); + return prime; } } diff --git a/src/main/java/mattrixwv/StringAlgorithms.java b/src/main/java/mattrixwv/StringAlgorithms.java index 4f3db35..2b824e9 100644 --- a/src/main/java/mattrixwv/StringAlgorithms.java +++ b/src/main/java/mattrixwv/StringAlgorithms.java @@ -24,15 +24,17 @@ package mattrixwv; import java.util.ArrayList; import java.util.Collections; +import java.util.List; public class StringAlgorithms{ + private StringAlgorithms(){} //This is a function that creates all permutations of a string and returns a vector of those permutations. - public static ArrayList getPermutations(String master){ + public static List getPermutations(String master){ return getPermutations(master, 0); } private static ArrayList getPermutations(String master, int num){ - ArrayList perms = new ArrayList(); + ArrayList perms = new ArrayList<>(); //Check if the number is out of bounds if((num >= master.length()) || (num < 0)){ //Do nothing and return an empty arraylist @@ -69,8 +71,7 @@ public class StringAlgorithms{ tempStr[first] = tempStr[second]; tempStr[second] = temp; - String swappedString = new String(tempStr); - return swappedString; + return new String(tempStr); } //This function returns the number of times the character occurs in the string public static long findNumOccurrence(String str, char c){ @@ -79,12 +80,7 @@ public class StringAlgorithms{ //Returns true if the string passed in is a palindrome public static boolean isPalindrome(String str){ String rev = new StringBuilder(str).reverse().toString(); - if(str.equals(rev)){ - return true; - } - else{ - return false; - } + return str.equals(rev); } //Returns true if the string passed to it is a pandigital public static boolean isPandigital(String str, char bottom, char top){ diff --git a/src/test/java/mattrixwv/TestNumberAlgorithms.java b/src/test/java/mattrixwv/TestNumberAlgorithms.java index 92060b6..f6a2dec 100644 --- a/src/test/java/mattrixwv/TestNumberAlgorithms.java +++ b/src/test/java/mattrixwv/TestNumberAlgorithms.java @@ -1,10 +1,10 @@ //JavaClasses/src/test/java/mattrixwv/TestNumberAlgorithms.java //Matthew Ellison // Created: 07-03-21 -//Modified: 07-03-21 +//Modified: 06-25-22 //This class contains tests for my number algorithms /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -25,8 +25,8 @@ package mattrixwv; import static org.junit.Assert.assertEquals; import java.math.BigInteger; -import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import org.junit.Test; @@ -37,43 +37,45 @@ public class TestNumberAlgorithms{ @Test public void testGetPrimes(){ //Test 1 - ArrayList correctAnswer = new ArrayList(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97)); + List correctAnswer = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97); Integer topNum = 100; - ArrayList answer = NumberAlgorithms.getPrimes(topNum); + List answer = NumberAlgorithms.getPrimes(topNum); assertEquals("getPrimes Integer failed", correctAnswer, answer); //Test 2 - ArrayList longCorrectAnswer = 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)); + List longCorrectAnswer = 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); Long longTopNum = 100L; - ArrayList longAnswer = NumberAlgorithms.getPrimes(longTopNum); + List longAnswer = NumberAlgorithms.getPrimes(longTopNum); assertEquals("getPrimes Long failed", longCorrectAnswer, longAnswer); //Test 3 - ArrayList bigCorrectAnswer = 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))); + List bigCorrectAnswer = 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)); BigInteger bigTopNum = BigInteger.valueOf(100); - ArrayList bigAnswer = NumberAlgorithms.getPrimes(bigTopNum); + List bigAnswer = NumberAlgorithms.getPrimes(bigTopNum); assertEquals("getPrimes BigInteger failed", bigCorrectAnswer, bigAnswer); } + @Test public void testGetNumPrimes(){ //Test 1 - ArrayList correctAnswer = new ArrayList(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97)); + List correctAnswer = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97); Integer numPrimes = 25; - ArrayList answer = NumberAlgorithms.getNumPrimes(numPrimes); + List answer = NumberAlgorithms.getNumPrimes(numPrimes); assertEquals("getNumPrimes Integer failed", correctAnswer, answer); //Test 2 - ArrayList longCorrectAnswer = 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)); + List longCorrectAnswer = 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); Long longNumPrimes = 25L; - ArrayList longAnswer = NumberAlgorithms.getNumPrimes(longNumPrimes); + List longAnswer = NumberAlgorithms.getNumPrimes(longNumPrimes); assertEquals("getNumPrimes Long failed", longCorrectAnswer, longAnswer); //Test 3 - ArrayList bigCorrectAnswer = 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))); + List bigCorrectAnswer = 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)); BigInteger bigNumPrimes = BigInteger.valueOf(25); - ArrayList bigAnswer = NumberAlgorithms.getNumPrimes(bigNumPrimes); + List bigAnswer = NumberAlgorithms.getNumPrimes(bigNumPrimes); assertEquals("getNumPrimes BigInteger failed", bigCorrectAnswer, bigAnswer); } + @Test public void testIsPrime(){ //Test 1 @@ -139,51 +141,73 @@ public class TestNumberAlgorithms{ answer = NumberAlgorithms.isPrime(bigNum); assertEquals("isPrime BigInteger 4 failed", correctAnswer, answer); } + @Test public void testGetFactors() throws InvalidResult{ //Test 1 - ArrayList correctAnswer = new ArrayList(Arrays.asList(2, 2, 5, 5)); + List correctAnswer = Arrays.asList(2, 2, 5, 5); Integer number = 100; - ArrayList answer = NumberAlgorithms.getFactors(number); + List answer = NumberAlgorithms.getFactors(number); assertEquals("getFactors Integer 1 failed", correctAnswer, answer); //Test 2 - correctAnswer = new ArrayList(Arrays.asList(2, 7, 7)); + correctAnswer = Arrays.asList(2, 7, 7); number = 98; answer = NumberAlgorithms.getFactors(number); assertEquals("getFactors Integer 2 failed", correctAnswer, answer); //Test 3 - ArrayList longCorrectAnswer = new ArrayList(Arrays.asList(2L, 2L, 5L, 5L)); + List longCorrectAnswer = Arrays.asList(2L, 2L, 5L, 5L); Long longNumber = 100L; - ArrayList longAnswer = NumberAlgorithms.getFactors(longNumber); + List longAnswer = NumberAlgorithms.getFactors(longNumber); assertEquals("getFactors Long failed", longCorrectAnswer, longAnswer); //Test 4 - ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7))); + List bigCorrectAnswer = Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7)); BigInteger bigNumber = BigInteger.valueOf(98); - ArrayList bigAnswer = NumberAlgorithms.getFactors(bigNumber); + List bigAnswer = NumberAlgorithms.getFactors(bigNumber); assertEquals("getFactors BigInteger failed", bigCorrectAnswer, bigAnswer); } + @Test public void testGetDivisors(){ + Stopwatch timer = new Stopwatch(); + timer.start(); //Test 1 - ArrayList correctAnswer = new ArrayList(Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100)); + List correctAnswer = Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100); Integer topNum = 100; - ArrayList answer = NumberAlgorithms.getDivisors(topNum); - assertEquals("getDivisors Integer failed", correctAnswer, answer); - + List answer = NumberAlgorithms.getDivisors(topNum); + assertEquals("getDivisors Integer 1 failed", correctAnswer, answer); //Test 2 - ArrayList longCorrectAnswer = new ArrayList(Arrays.asList(1L, 2L, 4L, 5L, 10L, 20L, 25L, 50L, 100L)); - Long longTopNum = 100L; - ArrayList longAnswer = NumberAlgorithms.getDivisors(longTopNum); - assertEquals("getDivisors Long failed", longCorrectAnswer, longAnswer); + correctAnswer = Arrays.asList(1, 2, 3, 6); + topNum = 6; + answer = NumberAlgorithms.getDivisors(topNum); + assertEquals("getDivisors Integer 2 failed", correctAnswer, answer); //Test 3 - ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(10), BigInteger.valueOf(20), BigInteger.valueOf(25), BigInteger.valueOf(50), BigInteger.valueOf(100))); + List longCorrectAnswer = Arrays.asList(1L, 2L, 4L, 5L, 10L, 20L, 25L, 50L, 100L); + long longTopNum = 100; + List longAnswer = NumberAlgorithms.getDivisors(longTopNum); + assertEquals("getDivisors Long 1 failed", longCorrectAnswer, longAnswer); + //Test 4 + longCorrectAnswer = Arrays.asList(1L, 2L, 3L, 6L); + longTopNum = 6; + longAnswer = NumberAlgorithms.getDivisors(longTopNum); + assertEquals("getDivisors Long 2 failed", longCorrectAnswer, longAnswer); + + //Test 5 + List bigCorrectAnswer = Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(10), BigInteger.valueOf(20), BigInteger.valueOf(25), BigInteger.valueOf(50), BigInteger.valueOf(100)); BigInteger bigTopNum = BigInteger.valueOf(100); - ArrayList bigAnswer = NumberAlgorithms.getDivisors(bigTopNum); - assertEquals("getDivisors BigInteger failed", bigCorrectAnswer, bigAnswer); + List bigAnswer = NumberAlgorithms.getDivisors(bigTopNum); + assertEquals("getDivisors BigInteger 1 failed", bigCorrectAnswer, bigAnswer); + //Test 6 + bigCorrectAnswer = Arrays.asList(BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(6)); + bigTopNum = BigInteger.valueOf(6); + bigAnswer = NumberAlgorithms.getDivisors(bigTopNum); + assertEquals("getDivisors BigInteger 2 failed", bigCorrectAnswer, bigAnswer); + timer.stop(); + System.out.println("timer = " + timer.toString()); } + @Test public void testGetFib(){ //Test 1 @@ -209,31 +233,33 @@ public class TestNumberAlgorithms{ BigInteger bigAnswer = NumberAlgorithms.getFib(bigNumber); assertEquals("getFib BigInteger failed", bigCorrectAnswer, bigAnswer); } + @Test public void testGetAllFib(){ //Test 1 - ArrayList correctAnswer = new ArrayList(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)); + List correctAnswer = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89); Integer highestNumber = 100; - ArrayList answer = NumberAlgorithms.getAllFib(highestNumber); + List answer = NumberAlgorithms.getAllFib(highestNumber); assertEquals("getAllFib Integer 1 failed", correctAnswer, answer); //Test 2 - correctAnswer = new ArrayList(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987)); + correctAnswer = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987); highestNumber = 1000; answer = NumberAlgorithms.getAllFib(highestNumber); assertEquals("getAllFib Integer 2 failed", correctAnswer, answer); //Test 3 - ArrayList longCorrectAnswer = new ArrayList(Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L)); + List longCorrectAnswer = Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L); Long longHighestNumber = 1000L; - ArrayList longAnswer = NumberAlgorithms.getAllFib(longHighestNumber); + List longAnswer = NumberAlgorithms.getAllFib(longHighestNumber); assertEquals("getAllFib Long failed", longCorrectAnswer, longAnswer); //Test 4 - ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(8), BigInteger.valueOf(13), BigInteger.valueOf(21), BigInteger.valueOf(34), BigInteger.valueOf(55), BigInteger.valueOf(89))); + List bigCorrectAnswer = Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(8), BigInteger.valueOf(13), BigInteger.valueOf(21), BigInteger.valueOf(34), BigInteger.valueOf(55), BigInteger.valueOf(89)); BigInteger bigHighestNumber = BigInteger.valueOf(100); - ArrayList bigAnswer = NumberAlgorithms.getAllFib(bigHighestNumber); + List bigAnswer = NumberAlgorithms.getAllFib(bigHighestNumber); assertEquals("getAllFib BigInteger failed", bigCorrectAnswer, bigAnswer); } + @Test public void testFactorial(){ //Integer diff --git a/src/test/java/mattrixwv/TestStringAlgorithms.java b/src/test/java/mattrixwv/TestStringAlgorithms.java index 77dc0ad..948c273 100644 --- a/src/test/java/mattrixwv/TestStringAlgorithms.java +++ b/src/test/java/mattrixwv/TestStringAlgorithms.java @@ -1,10 +1,10 @@ //JavaClasses/src/test/java/mattrixwv/TestStringAlgorithms.java //Matthew Ellison // Created: 07-03-21 -//Modified: 10-11-21 +//Modified: 06-25-22 //This class contains tests for my number algorithms /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -26,6 +26,7 @@ import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import org.junit.Test; @@ -36,7 +37,7 @@ public class TestStringAlgorithms{ //Test 1 String permString = "012"; ArrayList correctAnswer = new ArrayList(Arrays.asList("012", "021", "102", "120", "201", "210")); - ArrayList answer = StringAlgorithms.getPermutations(permString); + List answer = StringAlgorithms.getPermutations(permString); assertEquals("getPermutations failed", correctAnswer, answer); } @Test