From 906db6815f5e2fa0266c298c0ce2fe3311a01031 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Sun, 3 Mar 2019 18:51:12 -0500 Subject: [PATCH] Added functions and tests for Long type --- mattrixwv/Algorithms.class | Bin 5628 -> 7990 bytes mattrixwv/Algorithms.java | 232 ++++++++++++++++++++++++++++++++++++- testAlgorithms.java | 98 ++++++++++++++-- 3 files changed, 319 insertions(+), 11 deletions(-) diff --git a/mattrixwv/Algorithms.class b/mattrixwv/Algorithms.class index 16ef31416c67f89661e7afd0985f21f9e6760678..56fd36dcdb6f59b32759d1148e9192011bf0208f 100644 GIT binary patch literal 7990 zcmc&&e{@vknSQ=IGr7rdNeCn)nFQ2OAjuG-hyjcw0s#sn!6hsa@z+4cFgVGC$s~e` zVg*H{c15d_LRILRwMTn!x5m`A*e-5sZP%VXyW8&R@18xoo?W*++q1i8Pf^T1?|1K= zxf5n+`_Gocx%c~ZzwdpX_kG^)&Aj{Zo96&5!^=U0(PQG~APn>baV@q7Vc`~e?2xfr zgSZX1%j1q9mf}tsiI`%|HB2DqgKwr~_ zfmF0Fnrx}J$A(j}c+;w6GP1uTHk4{nnBJX=?7F)%GN?B)alpg_+^##;HxNk;C%JXo z@4ej(InHh684JBTbR-7)@>;Xv_gm>bNbe`7G&%5&7xJ}#$a?3*se8-0 z7OAH%T=pyLHss^FG0&72j%&5-9OTaHBD+$FB)vL~o%YyBY)JPCWGc2(VRm}YEO(oD zP@y&-n|i4x&cs25#g}JdDrgfA*#WmI9@jRAG2O%c3YE@AQ=+VXgNaXN9F#qq8+`o+ z8@?E+w{~MF`(rndGJq7^7|LGhJ;n(c$4%aEMMexAeWDvr+55OBP9;Ubji>DOc>~8e z{nC9*+Hg}c(QCV0Y_)q54YbYabSH+ByQ1r2BL0$9@xDYdmfGDvv{;mHVG$Y?X6Y=D zZH|S9amd19JR+@C79PbB6OUOqipMP+lgGpOw1p?|8HLhI(ujpWlP{mdaT8Bjcp9Iz z@C=@nkDgN~&a4-+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&kna45A-{nf@fJ$O+m!B4Q7L|IxuYw0*nsHH1k;^mbZ7Y(BuT9%`!WJa+r}X2 z9eVXElJqV~`W;Dnk0iZMl0LA!8jYmSj!lx(Ub17uNczl(B>jo*{Fx;El_Y&gl0G6y Ue 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; }