diff --git a/mattrixwv/Algorithms.class b/mattrixwv/Algorithms.class new file mode 100644 index 0000000..16ef314 Binary files /dev/null and b/mattrixwv/Algorithms.class differ diff --git a/mattrixwv/Algorithms.java b/mattrixwv/Algorithms.java new file mode 100644 index 0000000..ff2be17 --- /dev/null +++ b/mattrixwv/Algorithms.java @@ -0,0 +1,503 @@ +//Java/JavaClasses/Algorithms.java +//Matthew Ellison +// Created: 03-02-19 +//Modified: 03-02-19 +//This class holds many algorithms that I have found it useful to keep around +//As such all of the functions in here are static and meant to be used as stand alone functions +/* +Copyright (C) 2019 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + + +package mattrixwv; + + +import java.util.Arrays; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Collections; + + +public class Algorithms{ + //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 + 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 + if(goalNumber <= 1){ + return primes; + } + //Otherwise the number is at least 2, so 2 should be added to the list + else{ + primes.add(2); + } + + //We cna now start at 3 and skipp all even numbers, because they cannot be prime + for(int possiblePrime = 3;possiblePrime <= goalNumber;possiblePrime += 2){ + //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();){ + 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 + if(!foundFactor){ + primes.add(possiblePrime); + } + else{ + foundFactor = false; + } + } + + //Sort the list before returning it + Collections.sort(primes); + return primes; + } + public static ArrayList 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 numeber is 1, 0 or negative return an empty list + if(goalNumber.compareTo(BigInteger.valueOf(1)) <= 0){ + return primes; + } + //Otherwise the number is at least 2, so 2 should be added to the list + else{ + primes.add(BigInteger.valueOf(2)); + } + + //We cna now start at 3 and skipp all even numbers, because they cannot be prime + for(BigInteger possiblePrime = BigInteger.valueOf(3);possiblePrime.compareTo(goalNumber) <= 0;possiblePrime = possiblePrime.add(BigInteger.valueOf(2))){ + //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;){ + 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 + if(!foundFactor){ + primes.add(possiblePrime); + } + else{ + foundFactor = false; + } + } + + //Sort the list before returning it + Collections.sort(primes); + return primes; + } + //This function gets a certain number of primes + public static ArrayList getNumPrimes(int 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 numebr is 0 or negative return an empty list + if(numberOfPrimes <= 1){ + return primes; + } + //Otherwise the number is at least 2, so 2 should be added to the list + else{ + primes.add(2); + } + + //We cna now start at 3 and skipp all even numbers, because they cannot be prime + for(int possiblePrime = 3;primes.size() < numberOfPrimes;possiblePrime += 2){ + //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();){ + 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 + if(!foundFactor){ + primes.add(possiblePrime); + } + else{ + foundFactor = false; + } + } + + //Sort the list before returning it + Collections.sort(primes); + return primes; + } + public static ArrayList 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 numebr is 0 or negative return an empty list + if(numberOfPrimes.compareTo(BigInteger.valueOf(1)) <= 0){ + return primes; + } + //Otherwise the number is at least 2, so 2 should be added to the list + else{ + primes.add(BigInteger.valueOf(2)); + } + + //We cna now start at 3 and skipp all even numbers, because they cannot be prime + for(BigInteger possiblePrime = BigInteger.valueOf(3);numberOfPrimes.compareTo((BigInteger.valueOf(primes.size()))) > 0;possiblePrime = possiblePrime.add(BigInteger.valueOf(2))){ + //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;){ + 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 + if(!foundFactor){ + primes.add(possiblePrime); + } + else{ + foundFactor = false; + } + } + + //Sort the list before returning it + Collections.sort(primes); + return primes; + } + //This function returns all factors of goalNumber + public static ArrayList getFactors(int goalNumber){ + //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(); + + //You need to step through each prime and see if it is a factor in the number + for(int cnt = 0;cnt < primes.size();){ + //If the prime is a factor you need to add it to the factor list + if((goalNumber % primes.get(cnt)) == 0){ + factors.add(primes.get(cnt)); + goalNumber /= primes.get(cnt); + } + //Otherwise advance the location in primes you are looking at + //By not advancing if the prime is a factor you allow for multiple of the same prime number as a factor + else{ + ++cnt; + } + } + + //If you didn't get any factors the number itself must be a prime + if(factors.size() == 0){ + factors.add(goalNumber); + goalNumber /= goalNumber; + } + + //If for some reason the goalNumber is not 1 throw an error + ///Need to add the appropriate error here + + //Return the list of factors + return factors; + } + public static ArrayList getFactors(BigInteger goalNumber){ + //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(); + + //You need to step through each prime and see if it is a factor in the number + for(int cnt = 0;cnt < primes.size();){ + //If the prime is a factor you need to add it to the factor list + if((goalNumber.mod(primes.get(cnt))).compareTo(BigInteger.valueOf(0)) == 0){ + factors.add(primes.get(cnt)); + goalNumber = goalNumber.divide(primes.get(cnt)); + } + //Otherwise advance the location in primes you are looking at + //By not advancing if the prime is a factor you allow for multiple of the same prime number as a factor + else{ + ++cnt; + } + } + + //If you didn't get any factors the number itself must be a prime + if(factors.size() == 0){ + factors.add(goalNumber); + goalNumber.divide(goalNumber); + } + + //If for some reason the goalNumber is not 1 throw an error + ///Need to add the appropriate error here + + //Return the list of factors + return factors; + } + //This function returns all the divisors of goalNumber + public static ArrayList getDivisors(int goalNumber){ + ArrayList divisors = new ArrayList(); + //Start by checking that the number is positive + if(goalNumber <= 0){ + return divisors; + } + //If the number is 1 return just itself + else if(goalNumber == 1){ + divisors.add(1); + } + //Otherwise add 1 and itself to the list + else{ + divisors.add(1); + 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(Integer possibleDivisor = 2;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ + //If you find one add it and the number it creates to the list + if((goalNumber % possibleDivisor) == 0){ + divisors.add(possibleDivisor); + //Accound for the possibility of sqrt(goalNumber) being a divisor + if(possibleDivisor != topPossibleDivisor.intValue()){ + divisors.add(goalNumber / possibleDivisor); + } + } + } + + //Sort the list before returning it for neatness + Collections.sort(divisors); + //Return the list + return divisors; + } + public static ArrayList getDivisors(BigInteger goalNumber){ + ArrayList divisors = new ArrayList(); + //Start by checking that the number is positive + if(goalNumber.compareTo(BigInteger.valueOf(0)) <= 0){ + return divisors; + } + //If the number is 1 return just itself + else if(goalNumber.equals(BigInteger.valueOf(1))){ + divisors.add(BigInteger.valueOf(1)); + } + //Otherwise add 1 and itself to the list + else{ + divisors.add(BigInteger.valueOf(1)); + 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(2);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))){ + divisors.add(possibleDivisor); + //Accound for the possibility of sqrt(goalNumber) being a divisor + if(!possibleDivisor.equals(topPossibleDivisor)){ + divisors.add(goalNumber.divide(possibleDivisor)); + } + } + } + + //Sort the list before returning it for neatness + Collections.sort(divisors); + //Return the list + return divisors; + } + //This function returns all the divisors of goalNumber + public static Integer getFib(int goalSubscript){ + //Setup the variables + Integer[] fibNums = {1, 1, 0}; //A list to keep track of the Fibonacci numbers. It need only be 3 long because we only need the one we are working on and the last 2 + + //If the number is <= 0 return 0 + if(goalSubscript <= 0){ + return 0; + } + + //Loop through the list, generating Fibonacci numbers until it finds the correct subscript + Integer fibLoc = 2; + for(fibLoc = 2;fibLoc < goalSubscript;++fibLoc){ + fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3]; + } + + //Return the propper number. The location counter is 1 off of the subscript + return fibNums[(fibLoc - 1) % 3]; + } + public static BigInteger getFib(BigInteger goalSubscript){ + //Setup the variables + BigInteger[] fibNums = {BigInteger.valueOf(1), BigInteger.valueOf(1), BigInteger.valueOf(0)}; //A list to keep track of the Fibonacci numbers. It need only be 3 long because we only need the one we are working on and the last 2 + + //If the number is <= 0 return 0 + if(goalSubscript.compareTo(BigInteger.valueOf(0)) <= 0){ + return BigInteger.valueOf(0); + } + + //Loop through the list, generating Fibonacci numbers until it finds the correct subscript + Integer fibLoc = 2; + for(fibLoc = 2;goalSubscript.compareTo(BigInteger.valueOf(fibLoc)) > 0;++fibLoc){ + fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3].add(fibNums[(fibLoc - 2) % 3]); + } + + //Return the propper 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(int goalNumber){ + //Setup the variables + ArrayList fibNums = new ArrayList(); //A list to save the Fibonacci numbers + + //If the number is <= 0 return an empty list + if(goalNumber <= 0){ + return fibNums; + } + + //This means that at least 2 1's are elements + fibNums.add(1); + fibNums.add(1); + + //Loop to generate the rest of the Fibonacci numbers + while(fibNums.get(fibNums.size() - 1) <= goalNumber){ + fibNums.add(fibNums.get(fibNums.size() - 1) + fibNums.get(fibNums.size() - 2)); + } + + //At this point the most recent number is > goalNumber, so remove it and return the rest of the list + fibNums.remove(fibNums.size() - 1); + return fibNums; + } + public static ArrayList getAllFib(BigInteger goalNumber){ + //Setup the variables + 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){ + return fibNums; + } + + //This means that at least 2 1's are elements + fibNums.add(BigInteger.valueOf(1)); + fibNums.add(BigInteger.valueOf(1)); + + //Loop to generate the rest of the Fibonacci numbers + while(fibNums.get(fibNums.size() - 1).compareTo(goalNumber) <= 0){ + fibNums.add(fibNums.get(fibNums.size() - 1).add(fibNums.get(fibNums.size() - 2))); + } + + //At this point the most recent number is > goalNumber, so remove it and return the rest of the list + fibNums.remove(fibNums.size() - 1); + return fibNums; + } + //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; + } + + //Setup the variables + Integer sum = 0; + + //Loop through every element in the list and add them together + for(Integer num : nums){ + sum += num; + } + + //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); + } + + //Setup the variables + BigInteger sum = BigInteger.valueOf(0); + + //Loop through every element in the list and add them together + for(BigInteger num : nums){ + sum = sum.add(num); + } + + //Return the sum of all elements + return sum; + } + //This function returns the product of all elements in the list + public static int getProd(ArrayList nums){ + //If a blank list was passed tot he fuction return 0 as the product + if(nums.size() == 0){ + return 0; + } + + //Setup the variables + Integer product = 1; //Start at 1 because x * 1 = x + + //Loop through every element in the list and multiply them together + for(Integer num : nums){ + product *= num; + } + + //Return the product of all elements + return product; + } + public static BigInteger getBigProd(ArrayList nums){ + //If a blank list was passed tot he fuction return 0 as the product + if(nums.size() == 0){ + return BigInteger.valueOf(0); + } + + //Setup the variables + BigInteger product = BigInteger.valueOf(1); //Start at 1 because x * 1 = x + + //Loop through every element in the list and multiply them together + for(BigInteger num : nums){ + product = product.multiply(num); + } + + //Return the product of all elements + return product; + } +} diff --git a/testAlgorithms.java b/testAlgorithms.java new file mode 100644 index 0000000..f5b252c --- /dev/null +++ b/testAlgorithms.java @@ -0,0 +1,372 @@ +//Java/JavaClasses/testAlgorithms.java +//Matthew Ellison +// Created: 03-02-19 +//Modified: 03-02-19 +//This program runs tests on all function in the Algorithms library +/* +Copyright (C) 2019 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + + +import mattrixwv.Algorithms; +import mattrixwv.Stopwatch; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Arrays; + + +public class testAlgorithms{ + public static void main(String[] argv){ + //Create a timer so you can time the execution time of the tests + Stopwatch timer = new Stopwatch(); + + timer.start(); + //Test getPrimes + testGetPrimes(); + timer.stop(); + System.out.println("It took " + timer.getStr() + " to run this test\n"); + + //Test getNumPrimes + timer.start(); + testGetNumPrimes(); + timer.stop(); + System.out.println("It took " + timer.getStr() + " to run this test\n"); + + //Test getFactors + timer.start(); + testGetFactors(); + timer.stop(); + System.out.println("It took " + timer.getStr() + " to run this test\n"); + + //Test getDivisors + timer.start(); + testGetDivisors(); + timer.stop(); + System.out.println("It took " + timer.getStr() + " to run this test\n"); + + //Test getFib + timer.start(); + testGetFib(); + timer.stop(); + System.out.println("It took " + timer.getStr() + " to run this test\n"); + + //Test getAllFib + timer.start(); + testGetAllFib(); + timer.stop(); + System.out.println("It took " + timer.getStr() + " to run this test\n"); + + //Tets getSum + timer.start(); + testGetSum(); + timer.stop(); + System.out.println("It took " + timer.getStr() + " to run this test\n"); + + //Test getProd + timer.start(); + testGetProd(); + timer.stop(); + System.out.println("It took " + timer.getStr() + " to run this test\n"); + + //Print a closing message + System.out.println("Tests completed"); + } + //This function tests the getPrimes function + private static void testGetPrimes(){ + Boolean failed = false; //Holds whether a test was failed + + //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)); + Integer topNum = 100; + ArrayList answer = Algorithms.getPrimes(topNum); + //Print an error message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getPrimes failed the first test"); + failed = true; + } + + //Test 2 + 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))); + BigInteger bigTopNum = BigInteger.valueOf(100); + ArrayList bigAnswer = Algorithms.getPrimes(bigTopNum); + //Print an error message if the function returned the wrong answer + if(!bigCorrectAnswer.equals(bigAnswer)){ + System.out.println("getPrimes failed the second test"); + failed = true; + } + + //Print a message if all of the tests passed + if(!failed){ + System.out.println("getPrimes passed all tests"); + } + } + //This function tests the getNumPrimes function + private static void testGetNumPrimes(){ + Boolean failed = false; //Holds whether a test was failed + + //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)); + Integer numPrimes = 25; + ArrayList answer = Algorithms.getNumPrimes(numPrimes); + //Print an error message if the function return the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getNumPrimes failed at the first test"); + failed = true; + } + + //Test 2 + 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))); + BigInteger bigTopNum = BigInteger.valueOf(25); + ArrayList bigAnswer = Algorithms.getNumPrimes(bigTopNum); + //Print an error message if the function returned the wrong answer + if(!bigCorrectAnswer.equals(bigAnswer)){ + System.out.println("getPrimes failed the second test"); + System.out.println("Correct Answer = " + bigCorrectAnswer.toString()); + System.out.println("Answer = " + bigAnswer.toString()); + failed = true; + } + + //Print a message if all of the tests passed + if(!failed){ + System.out.println("getNumPrimes passed all tests"); + } + } + //This function tests the getFactors function + private static void testGetFactors(){ + Boolean failed = false; //Holds whether a test was failed + + //Test 1 + ArrayList correctAnswer = new ArrayList(Arrays.asList(2, 2, 5, 5)); + Integer number = 100; + ArrayList answer = Algorithms.getFactors(number); + //Print an error message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getFactors failed the first test"); + failed = true; + } + + //Test 2 + correctAnswer = new ArrayList(Arrays.asList(2, 7, 7)); + number = 98; + answer = Algorithms.getFactors(number); + //Print an error message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getFactors failed the second test"); + failed = true; + } + + //Test 3 + ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7))); + BigInteger bigNumber = BigInteger.valueOf(98); + ArrayList bigAnswer = Algorithms.getFactors(bigNumber); + //Print an error message if the function returned the wrong answer + if(!bigCorrectAnswer.equals(bigAnswer)){ + System.out.println("getFactors failed the third test"); + failed = true; + } + + //Print a message if all of the tests passed + if(!failed){ + System.out.println("getFactors passed all tests"); + } + } + //This function tests the getDivisors function + private static void testGetDivisors(){ + Boolean failed = false; //Holds whether a test was failed + + //Test 1 + ArrayList correctAnswer = new ArrayList(Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100)); + Integer topNum = 100; + ArrayList answer = Algorithms.getDivisors(topNum); + //Print an error message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getDivisors failed the first test"); + failed = true; + } + + //Test 2 + 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))); + BigInteger bigTopNum = BigInteger.valueOf(100); + ArrayList bigAnswer = Algorithms.getDivisors(bigTopNum); + //Print an error message if the function returned the wrong answer + if(!bigCorrectAnswer.equals(bigAnswer)){ + System.out.println("getDivisors failed the second test"); + failed = true; + } + + //Print a message if all of the tests passed + if(!failed){ + System.out.println("getDivisors passed all tests"); + } + } + //This function tests the getFib function + private static void testGetFib(){ + Boolean failed = false; //Holds whether a test was failed + + //Test 1 + Integer correctAnswer = 144; + Integer number = 12; + Integer answer = Algorithms.getFib(number); + //Print an error message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getFib failed the first test"); + failed = true; + } + + //Test 2 + correctAnswer = 6765; + number = 20; + answer = Algorithms.getFib(number); + //Print an error message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getFib failed the seconds test"); + failed = true; + } + + //Test 3 + BigInteger bigCorrectAnswer = new BigInteger("1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816"); + BigInteger bigNumber = BigInteger.valueOf(4782); + BigInteger bigAnswer = Algorithms.getFib(bigNumber); + //Print an error message if the function returned the wrong answer + if(!bigCorrectAnswer.equals(bigAnswer)){ + System.out.println("getFib failed the third test"); + failed = true; + } + + //Print a message if all of the tests passed + if(!failed){ + System.out.println("getFib passed all tests"); + } + } + //This function tests the getAllFib function + private static void testGetAllFib(){ + Boolean failed = false; //Holds whether a test was failed + + //Test 1 + ArrayList correctAnswer = new ArrayList(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)); + Integer highestNumber = 100; + ArrayList answer = Algorithms.getAllFib(highestNumber); + //Print an error message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getAllFib failed the first test"); + failed = true; + } + + //Test 2 + correctAnswer = new ArrayList(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987)); + highestNumber = 1000; + answer = Algorithms.getAllFib(highestNumber); + //Print an error message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getAllFib failed the second test"); + failed = true; + } + + //Test 3 + 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))); + BigInteger bigHighestNumber = BigInteger.valueOf(100); + ArrayList bigAnswer = Algorithms.getAllFib(bigHighestNumber); + //Print an error message if the function returned the wrong answer + if(!bigCorrectAnswer.equals(bigAnswer)){ + System.out.println("getAllFib failed the third test"); + failed = true; + } + + //Print a message if all of the tests passed + if(!failed){ + System.out.println("getAllFib passed all tests"); + } + } + //This function tests the getSum function + private static void testGetSum(){ + Boolean failed = false; //Holds whether a test was failed + + //Test 1 + Integer correctAnswer = 0; + ArrayList numbers = new ArrayList(); + Integer answer = Algorithms.getSum(numbers); + //Print a message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getSum failed the first test"); + failed = true; + } + + //Test 2 + correctAnswer = 118; + numbers = new ArrayList(Arrays.asList(2, 2, 3, 3, 4, 4, 100)); + answer = Algorithms.getSum(numbers); + //Print a message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getSum failed the second test"); + failed = true; + } + + //Test 3 + BigInteger bigCorrectAnswer = BigInteger.valueOf(118); + ArrayList bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(100))); + BigInteger bigAnswer = Algorithms.getBigSum(bigNumbers); + //Print a message if the function returned the wrong answer + if(!bigCorrectAnswer.equals(bigAnswer)){ + System.out.println("getSum failed the third test"); + failed = true; + } + + //Print a message if all of the tests passed + if(!failed){ + System.out.println("getSum passed all tests"); + } + } + //This function tests the getProd function + private static void testGetProd(){ + Boolean failed = false; //Holds whether a test was failed + + //Test 1 + Integer correctAnswer = 0; + ArrayList numbers = new ArrayList(); + Integer answer = Algorithms.getProd(numbers); + //Print a message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getProd failed the first test"); + failed = true; + } + + //Test 2 + correctAnswer = 57600; + numbers = new ArrayList(Arrays.asList(2, 2, 3, 3, 4, 4, 100)); + answer = Algorithms.getProd(numbers); + //Print a message if the function returned the wrong answer + if(!correctAnswer.equals(answer)){ + System.out.println("getProd failed the second test"); + failed = true; + } + + //Test 3 + BigInteger bigCorrectAnswer = BigInteger.valueOf(57600); + ArrayList bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(100))); + BigInteger bigAnswer = Algorithms.getBigProd(bigNumbers); + //Print a message if the function returned the wrong answer + if(!bigCorrectAnswer.equals(bigAnswer)){ + System.out.println("getProd failed the third test"); + failed = true; + } + + //Print a message if all of the tests passed + if(!failed){ + System.out.println("getProd passed all tests"); + } + } +} \ No newline at end of file