From 00d8e71a885fcb29b73a7ba1c3683a5227ad4f62 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Sat, 2 Mar 2019 19:34:31 -0500 Subject: [PATCH] Added some algorithms to a class --- mattrixwv/Algorithms.class | Bin 0 -> 5628 bytes mattrixwv/Algorithms.java | 503 +++++++++++++++++++++++++++++++++++++ testAlgorithms.java | 372 +++++++++++++++++++++++++++ 3 files changed, 875 insertions(+) create mode 100644 mattrixwv/Algorithms.class create mode 100644 mattrixwv/Algorithms.java create mode 100644 testAlgorithms.java diff --git a/mattrixwv/Algorithms.class b/mattrixwv/Algorithms.class new file mode 100644 index 0000000000000000000000000000000000000000..16ef31416c67f89661e7afd0985f21f9e6760678 GIT binary patch literal 5628 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 Ue. +*/ + + +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