diff --git a/mattrixwv/Algorithms.class b/mattrixwv/Algorithms.class index 16ef314..56fd36d 100644 Binary files a/mattrixwv/Algorithms.class and b/mattrixwv/Algorithms.class differ diff --git a/mattrixwv/Algorithms.java b/mattrixwv/Algorithms.java index ff2be17..38e336a 100644 --- a/mattrixwv/Algorithms.java +++ b/mattrixwv/Algorithms.java @@ -1,7 +1,7 @@ //Java/JavaClasses/Algorithms.java //Matthew Ellison // Created: 03-02-19 -//Modified: 03-02-19 +//Modified: 03-03-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 /* @@ -78,6 +78,51 @@ public class Algorithms{ Collections.sort(primes); return primes; } + public static ArrayList getPrimes(Long goalNumber){ + ArrayList primes = new ArrayList(); //Holds the prime numbers + Boolean foundFactor = false; //A flag for whether a factor of the current number has been found + + //If the numebr is 0 or negative return an empty list + if(goalNumber <= 1){ + return primes; + } + //Otherwise the number is at least 2, so 2 should be added to the list + else{ + primes.add(2L); + } + + //We cna now start at 3 and skipp all even numbers, because they cannot be prime + for(Long possiblePrime = 3L;possiblePrime <= goalNumber;possiblePrime += 2L){ + //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 @@ -169,6 +214,51 @@ public class Algorithms{ Collections.sort(primes); return primes; } + public static ArrayList getNumPrimes(Long numberOfPrimes){ + ArrayList primes = new ArrayList(); //Holds the prime numbers + Boolean foundFactor = false; //A flag for whether a factor of the current number has been found + + //If the 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(2L); + } + + //We cna now start at 3 and skipp all even numbers, because they cannot be prime + for(Long possiblePrime = 3L;primes.size() < numberOfPrimes;possiblePrime += 2L){ + //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 @@ -247,6 +337,38 @@ public class Algorithms{ //Return the list of factors return factors; } + public static ArrayList getFactors(Long 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.longValue()); + ArrayList factors = new ArrayList(); + + //You need to step through each prime and see if it is a factor in the number + for(int cnt = 0;cnt < primes.size();){ + //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(); @@ -314,6 +436,40 @@ public class Algorithms{ //Return the list return divisors; } + public static ArrayList getDivisors(Long 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(1L); + } + //Otherwise add 1 and itself to the list + else{ + divisors.add(1L); + 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 = 2L;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 @@ -367,6 +523,24 @@ public class Algorithms{ //Return the propper number. The location counter is 1 off of the subscript return fibNums[(fibLoc - 1) % 3]; } + public static Long getFib(Long goalSubscript){ + //Setup the variables + Long[] fibNums = {1L, 1L, 0L}; //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 0L; + } + + //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 @@ -408,6 +582,28 @@ public class Algorithms{ fibNums.remove(fibNums.size() - 1); return fibNums; } + public static ArrayList getAllFib(Long 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(1L); + fibNums.add(1L); + + //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 @@ -448,6 +644,23 @@ public class Algorithms{ //Return the sum of all elements return sum; } + public static Long getLongSum(ArrayList nums){ + //If a blank list was passed to the function return 0 as the sum + if(nums.size() == 0){ + return 0L; + } + + //Setup the variables + Long sum = 0L; + + //Loop through every element in the list and add them together + for(Long 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){ @@ -483,6 +696,23 @@ public class Algorithms{ //Return the product of all elements return product; } + public static Long getLongProd(ArrayList nums){ + //If a blank list was passed tot he fuction return 0 as the product + if(nums.size() == 0){ + return 0L; + } + + //Setup the variables + Long product = 1L; //Start at 1 because x * 1 = x + + //Loop through every element in the list and multiply them together + for(Long 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){ diff --git a/testAlgorithms.java b/testAlgorithms.java index f5b252c..f800c44 100644 --- a/testAlgorithms.java +++ b/testAlgorithms.java @@ -100,12 +100,22 @@ public class testAlgorithms{ } //Test 2 + ArrayList longCorrectAnswer = new ArrayList(Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L)); + Long longTopNum = 100L; + ArrayList longAnswer = Algorithms.getPrimes(longTopNum); + //Print an error message if the function returned the wrong answer + if(!longCorrectAnswer.equals(longAnswer)){ + System.out.println("getPrimes failed the second test"); + failed = true; + } + + //Test 3 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(7), BigInteger.valueOf(11), BigInteger.valueOf(13), BigInteger.valueOf(17), BigInteger.valueOf(19), BigInteger.valueOf(23), BigInteger.valueOf(29), BigInteger.valueOf(31), BigInteger.valueOf(37), BigInteger.valueOf(41), BigInteger.valueOf(43), BigInteger.valueOf(47), BigInteger.valueOf(53), BigInteger.valueOf(59), BigInteger.valueOf(61), BigInteger.valueOf(67), BigInteger.valueOf(71), BigInteger.valueOf(73), BigInteger.valueOf(79), BigInteger.valueOf(83), BigInteger.valueOf(89), BigInteger.valueOf(97))); 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"); + System.out.println("getPrimes failed the third test"); failed = true; } @@ -129,14 +139,22 @@ public class testAlgorithms{ } //Test 2 + ArrayList longCorrectAnswer = new ArrayList(Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L)); + Long longNumPrimes = 25L; + ArrayList longAnswer = Algorithms.getNumPrimes(longNumPrimes); + //Print an error message if the function return the wrong answer + if(!longCorrectAnswer.equals(longAnswer)){ + System.out.println("getNumPrimes failed at the second test"); + failed = true; + } + + //Test 3 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(7), BigInteger.valueOf(11), BigInteger.valueOf(13), BigInteger.valueOf(17), BigInteger.valueOf(19), BigInteger.valueOf(23), BigInteger.valueOf(29), BigInteger.valueOf(31), BigInteger.valueOf(37), BigInteger.valueOf(41), BigInteger.valueOf(43), BigInteger.valueOf(47), BigInteger.valueOf(53), BigInteger.valueOf(59), BigInteger.valueOf(61), BigInteger.valueOf(67), BigInteger.valueOf(71), BigInteger.valueOf(73), BigInteger.valueOf(79), BigInteger.valueOf(83), BigInteger.valueOf(89), BigInteger.valueOf(97))); 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()); + System.out.println("getPrimes failed the third test"); failed = true; } @@ -170,12 +188,22 @@ public class testAlgorithms{ } //Test 3 + ArrayList longCorrectAnswer = new ArrayList(Arrays.asList(2L, 2L, 5L, 5L)); + Long longNumber = 100L; + ArrayList longAnswer = Algorithms.getFactors(longNumber); + //Print an error message if the function returned the wrong answer + if(!longCorrectAnswer.equals(longAnswer)){ + System.out.println("getFactors failed the third test"); + failed = true; + } + + //Test 4 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"); + System.out.println("getFactors failed the fourth test"); failed = true; } @@ -199,12 +227,22 @@ public class testAlgorithms{ } //Test 2 + ArrayList longCorrectAnswer = new ArrayList(Arrays.asList(1L, 2L, 4L, 5L, 10L, 20L, 25L, 50L, 100L)); + Long longTopNum = 100L; + ArrayList longAnswer = Algorithms.getDivisors(longTopNum); + //Print an error message if the function returned the wrong answer + if(!longCorrectAnswer.equals(longAnswer)){ + System.out.println("getDivisors failed the second test"); + failed = true; + } + + //Test 3 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(10), BigInteger.valueOf(20), BigInteger.valueOf(25), BigInteger.valueOf(50), BigInteger.valueOf(100))); 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"); + System.out.println("getDivisors failed the third test"); failed = true; } @@ -238,12 +276,22 @@ public class testAlgorithms{ } //Test 3 + Long longCorrectAnswer = 6765L; + Long longNumber = 20L; + Long longAnswer = Algorithms.getFib(longNumber); + //Print an error message if the function returned the wrong answer + if(!longCorrectAnswer.equals(longAnswer)){ + System.out.println("getFib failed the third test"); + failed = true; + } + + //Test 4 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"); + System.out.println("getFib failed the fourth test"); failed = true; } @@ -277,12 +325,22 @@ public class testAlgorithms{ } //Test 3 + ArrayList longCorrectAnswer = new ArrayList(Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L)); + Long longHighestNumber = 1000L; + ArrayList longAnswer = Algorithms.getAllFib(longHighestNumber); + //Print an error message if the function returned the wrong answer + if(!longCorrectAnswer.equals(longAnswer)){ + System.out.println("getAllFib failed the third test"); + failed = true; + } + + //Test 4 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(8), BigInteger.valueOf(13), BigInteger.valueOf(21), BigInteger.valueOf(34), BigInteger.valueOf(55), BigInteger.valueOf(89))); 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"); + System.out.println("getAllFib failed the fourth test"); failed = true; } @@ -316,12 +374,22 @@ public class testAlgorithms{ } //Test 3 + Long longCorrectAnswer = 118L; + ArrayList longNumbers = new ArrayList(Arrays.asList(2L, 2L, 3L, 3L, 4L, 4L, 100L)); + Long longAnswer = Algorithms.getLongSum(longNumbers); + //Print a message if the function returned the wrong answer + if(!longCorrectAnswer.equals(longAnswer)){ + System.out.println("getSum failed the third test"); + failed = true; + } + + //Test 4 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"); + System.out.println("getSum failed the fourth test"); failed = true; } @@ -355,12 +423,22 @@ public class testAlgorithms{ } //Test 3 + Long longCorrectAnswer = 57600L; + ArrayList longNumbers = new ArrayList(Arrays.asList(2L, 2L, 3L, 3L, 4L, 4L, 100L)); + Long longAnswer = Algorithms.getLongProd(longNumbers); + //Print a message if the function returned the wrong answer + if(!longCorrectAnswer.equals(longAnswer)){ + System.out.println("getProd failed the third test"); + failed = true; + } + + //Test 4 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"); + System.out.println("getProd failed the fourth test"); failed = true; }