Updated sonarqube findings

This commit is contained in:
2022-06-26 11:53:01 -04:00
parent c7ac267c25
commit 4ec1346476
7 changed files with 262 additions and 291 deletions

View File

@@ -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<Integer> 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<Integer> 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<Long> 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<Long> 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<BigInteger> 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<BigInteger> 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<Integer> nums){
public static int getProd(Iterable<Integer> 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<Long> nums){
public static long getLongProd(Iterable<Long> 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<BigInteger> nums){
public static BigInteger getBigProd(Iterable<BigInteger> 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 <T> String printList(ArrayList<T> 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 <T> String printList(Iterable<T> list){
StringJoiner returnString = new StringJoiner(", ", "[", "]");
for(T obj : list){
returnString.add(obj.toString());
}
listString.append("]");
return listString.toString();
return returnString.toString();
}
}

View File

@@ -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> T getNum(T num1){
return num1;
}
//This function returns a list with all the prime numbers <= goalNumber
public static ArrayList<Integer> getPrimes(Integer goalNumber){
ArrayList<Integer> primes = new ArrayList<Integer>(); //Holds the prime numbers
public static List<Integer> getPrimes(Integer goalNumber){
ArrayList<Integer> 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<Long> getPrimes(Long goalNumber){
ArrayList<Long> primes = new ArrayList<Long>(); //Holds the prime numbers
public static List<Long> getPrimes(Long goalNumber){
ArrayList<Long> 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<BigInteger> getPrimes(BigInteger goalNumber){
ArrayList<BigInteger> primes = new ArrayList<BigInteger>(); //Holds the prime numbers
public static List<BigInteger> getPrimes(BigInteger goalNumber){
ArrayList<BigInteger> 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<Integer> getNumPrimes(Integer numberOfPrimes){
ArrayList<Integer> primes = new ArrayList<Integer>(); //Holds the prime numbers
public static List<Integer> getNumPrimes(Integer numberOfPrimes){
ArrayList<Integer> 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<Long> getNumPrimes(Long numberOfPrimes){
ArrayList<Long> primes = new ArrayList<Long>(); //Holds the prime numbers
public static List<Long> getNumPrimes(Long numberOfPrimes){
ArrayList<Long> 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<BigInteger> getNumPrimes(BigInteger numberOfPrimes){
ArrayList<BigInteger> primes = new ArrayList<BigInteger>(); //Holds the prime numbers
public static List<BigInteger> getNumPrimes(BigInteger numberOfPrimes){
ArrayList<BigInteger> 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<Integer> getFactors(Integer goalNumber) throws InvalidResult{
public static List<Integer> 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<Integer> primes = getPrimes(topPossiblePrime.intValue());
ArrayList<Integer> factors = new ArrayList<Integer>();
List<Integer> primes = getPrimes(topPossiblePrime.intValue());
ArrayList<Integer> 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<Long> getFactors(Long goalNumber) throws InvalidResult{
public static List<Long> 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<Long> primes = getPrimes(topPossiblePrime.longValue());
ArrayList<Long> factors = new ArrayList<Long>();
List<Long> primes = getPrimes(topPossiblePrime.longValue());
ArrayList<Long> 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<BigInteger> getFactors(BigInteger goalNumber) throws InvalidResult{
public static List<BigInteger> 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<BigInteger> primes = getPrimes(topPossiblePrime);
ArrayList<BigInteger> factors = new ArrayList<BigInteger>();
List<BigInteger> primes = getPrimes(topPossiblePrime);
ArrayList<BigInteger> 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<Integer> getDivisors(Integer goalNumber){
ArrayList<Integer> divisors = new ArrayList<Integer>();
public static List<Integer> getDivisors(Integer goalNumber){
HashSet<Integer> 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<Integer> 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<Long> getDivisors(Long goalNumber){
ArrayList<Long> divisors = new ArrayList<Long>();
public static List<Long> getDivisors(Long goalNumber){
HashSet<Long> 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<Long> 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<BigInteger> getDivisors(BigInteger goalNumber){
ArrayList<BigInteger> divisors = new ArrayList<BigInteger>();
public static List<BigInteger> getDivisors(BigInteger goalNumber){
HashSet<BigInteger> 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<BigInteger> 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<Integer> getAllFib(Integer goalNumber){
public static List<Integer> getAllFib(Integer goalNumber){
//Setup the variables
ArrayList<Integer> fibNums = new ArrayList<Integer>(); //A list to save the Fibonacci numbers
ArrayList<Integer> 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<Long> getAllFib(Long goalNumber){
public static List<Long> getAllFib(Long goalNumber){
//Setup the variables
ArrayList<Long> fibNums = new ArrayList<Long>(); //A list to save the Fibonacci numbers
ArrayList<Long> 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<BigInteger> getAllFib(BigInteger goalNumber){
public static List<BigInteger> getAllFib(BigInteger goalNumber){
//Setup the variables
ArrayList<BigInteger> fibNums = new ArrayList<BigInteger>(); //A list to save the Fibonacci numbers
ArrayList<BigInteger> 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

View File

@@ -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<Long>{
@@ -34,7 +35,7 @@ public class SieveOfEratosthenes implements Iterator<Long>{
private Map<Long, ArrayList<Long>> dict;
public SieveOfEratosthenes(){
dict = new HashMap<Long, ArrayList<Long>>();
dict = new HashMap<>();
possiblePrime = 2;
}
@Override
@@ -44,39 +45,44 @@ public class SieveOfEratosthenes implements Iterator<Long>{
@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);
}
//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<Long> 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<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;
//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<>(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;
}
}

View File

@@ -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<BigInteger>{
private Map<BigInteger, ArrayList<BigInteger>> dict;
public SieveOfEratosthenesBig(){
dict = new HashMap<BigInteger, ArrayList<BigInteger>>();
dict = new HashMap<>();
possiblePrime = BigInteger.TWO;
}
@Override
@@ -45,41 +45,43 @@ public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
@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{
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<BigInteger> 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<BigInteger> 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;
}
}

View File

@@ -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<String> getPermutations(String master){
public static List<String> getPermutations(String master){
return getPermutations(master, 0);
}
private static ArrayList<String> getPermutations(String master, int num){
ArrayList<String> perms = new ArrayList<String>();
ArrayList<String> 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){

View File

@@ -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<Integer> correctAnswer = new ArrayList<Integer>(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<Integer> 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<Integer> answer = NumberAlgorithms.getPrimes(topNum);
List<Integer> answer = NumberAlgorithms.getPrimes(topNum);
assertEquals("getPrimes Integer failed", correctAnswer, answer);
//Test 2
ArrayList<Long> longCorrectAnswer = new ArrayList<Long>(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<Long> 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<Long> longAnswer = NumberAlgorithms.getPrimes(longTopNum);
List<Long> longAnswer = NumberAlgorithms.getPrimes(longTopNum);
assertEquals("getPrimes Long failed", longCorrectAnswer, longAnswer);
//Test 3
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> 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<BigInteger> bigAnswer = NumberAlgorithms.getPrimes(bigTopNum);
List<BigInteger> bigAnswer = NumberAlgorithms.getPrimes(bigTopNum);
assertEquals("getPrimes BigInteger failed", bigCorrectAnswer, bigAnswer);
}
@Test
public void testGetNumPrimes(){
//Test 1
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(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<Integer> 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<Integer> answer = NumberAlgorithms.getNumPrimes(numPrimes);
List<Integer> answer = NumberAlgorithms.getNumPrimes(numPrimes);
assertEquals("getNumPrimes Integer failed", correctAnswer, answer);
//Test 2
ArrayList<Long> longCorrectAnswer = new ArrayList<Long>(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<Long> 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<Long> longAnswer = NumberAlgorithms.getNumPrimes(longNumPrimes);
List<Long> longAnswer = NumberAlgorithms.getNumPrimes(longNumPrimes);
assertEquals("getNumPrimes Long failed", longCorrectAnswer, longAnswer);
//Test 3
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> 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<BigInteger> bigAnswer = NumberAlgorithms.getNumPrimes(bigNumPrimes);
List<BigInteger> 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<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(2, 2, 5, 5));
List<Integer> correctAnswer = Arrays.asList(2, 2, 5, 5);
Integer number = 100;
ArrayList<Integer> answer = NumberAlgorithms.getFactors(number);
List<Integer> answer = NumberAlgorithms.getFactors(number);
assertEquals("getFactors Integer 1 failed", correctAnswer, answer);
//Test 2
correctAnswer = new ArrayList<Integer>(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<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(2L, 2L, 5L, 5L));
List<Long> longCorrectAnswer = Arrays.asList(2L, 2L, 5L, 5L);
Long longNumber = 100L;
ArrayList<Long> longAnswer = NumberAlgorithms.getFactors(longNumber);
List<Long> longAnswer = NumberAlgorithms.getFactors(longNumber);
assertEquals("getFactors Long failed", longCorrectAnswer, longAnswer);
//Test 4
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7)));
List<BigInteger> bigCorrectAnswer = Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7));
BigInteger bigNumber = BigInteger.valueOf(98);
ArrayList<BigInteger> bigAnswer = NumberAlgorithms.getFactors(bigNumber);
List<BigInteger> bigAnswer = NumberAlgorithms.getFactors(bigNumber);
assertEquals("getFactors BigInteger failed", bigCorrectAnswer, bigAnswer);
}
@Test
public void testGetDivisors(){
Stopwatch timer = new Stopwatch();
timer.start();
//Test 1
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100));
List<Integer> correctAnswer = Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100);
Integer topNum = 100;
ArrayList<Integer> answer = NumberAlgorithms.getDivisors(topNum);
assertEquals("getDivisors Integer failed", correctAnswer, answer);
List<Integer> answer = NumberAlgorithms.getDivisors(topNum);
assertEquals("getDivisors Integer 1 failed", correctAnswer, answer);
//Test 2
ArrayList<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(1L, 2L, 4L, 5L, 10L, 20L, 25L, 50L, 100L));
Long longTopNum = 100L;
ArrayList<Long> 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<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<Long> longCorrectAnswer = Arrays.asList(1L, 2L, 4L, 5L, 10L, 20L, 25L, 50L, 100L);
long longTopNum = 100;
List<Long> 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<BigInteger> 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<BigInteger> bigAnswer = NumberAlgorithms.getDivisors(bigTopNum);
assertEquals("getDivisors BigInteger failed", bigCorrectAnswer, bigAnswer);
List<BigInteger> 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<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89));
List<Integer> correctAnswer = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89);
Integer highestNumber = 100;
ArrayList<Integer> answer = NumberAlgorithms.getAllFib(highestNumber);
List<Integer> answer = NumberAlgorithms.getAllFib(highestNumber);
assertEquals("getAllFib Integer 1 failed", correctAnswer, answer);
//Test 2
correctAnswer = new ArrayList<Integer>(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<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L));
List<Long> longCorrectAnswer = Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L);
Long longHighestNumber = 1000L;
ArrayList<Long> longAnswer = NumberAlgorithms.getAllFib(longHighestNumber);
List<Long> longAnswer = NumberAlgorithms.getAllFib(longHighestNumber);
assertEquals("getAllFib Long failed", longCorrectAnswer, longAnswer);
//Test 4
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> 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<BigInteger> bigAnswer = NumberAlgorithms.getAllFib(bigHighestNumber);
List<BigInteger> bigAnswer = NumberAlgorithms.getAllFib(bigHighestNumber);
assertEquals("getAllFib BigInteger failed", bigCorrectAnswer, bigAnswer);
}
@Test
public void testFactorial(){
//Integer

View File

@@ -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<String> correctAnswer = new ArrayList<String>(Arrays.asList("012", "021", "102", "120", "201", "210"));
ArrayList<String> answer = StringAlgorithms.getPermutations(permString);
List<String> answer = StringAlgorithms.getPermutations(permString);
assertEquals("getPermutations failed", correctAnswer, answer);
}
@Test