Updated more sonarqube findings

This commit is contained in:
2022-06-26 12:50:50 -04:00
parent 51456c7533
commit e0c2f1f0fe
4 changed files with 51 additions and 238 deletions

View File

@@ -23,6 +23,8 @@ package mattrixwv;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner; import java.util.StringJoiner;
@@ -125,4 +127,12 @@ public class ArrayAlgorithms{
} }
return returnString.toString(); return returnString.toString();
} }
//Convert lists
public static List<Integer> longToInt(List<Long> originalList){
ArrayList<Integer> newList = new ArrayList<>(originalList.size());
for(Long num : originalList){
newList.add(num.intValue());
}
return newList;
}
} }

View File

@@ -40,45 +40,10 @@ public class NumberAlgorithms{
} }
//This function returns a list with all the prime numbers <= goalNumber //This function returns a list with all the prime numbers <= goalNumber
public static List<Integer> getPrimes(Integer goalNumber){ public static List<Integer> getPrimes(int goalNumber){
ArrayList<Integer> primes = new ArrayList<>(); //Holds the prime numbers return ArrayAlgorithms.longToInt(getPrimes((long) goalNumber));
boolean foundFactor = false; //A flag for whether a factor of the current number has been found
//If the number is 0 or negative return an empty list
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 can now start at 3 and skip 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;(primesCnt < primes.size()) && (primes.get(primesCnt) <= topPossibleFactor.intValue());++primesCnt){
if((possiblePrime % primes.get(primesCnt)) == 0){
foundFactor = true;
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 List<Long> getPrimes(Long goalNumber){ public static List<Long> getPrimes(long goalNumber){
ArrayList<Long> primes = new ArrayList<>(); //Holds the prime numbers ArrayList<Long> primes = new ArrayList<>(); //Holds the prime numbers
boolean foundFactor = false; //A flag for whether a factor of the current number has been found boolean foundFactor = false; //A flag for whether a factor of the current number has been found
@@ -157,45 +122,10 @@ public class NumberAlgorithms{
//This function gets a certain number of primes //This function gets a certain number of primes
public static List<Integer> getNumPrimes(Integer numberOfPrimes){ public static List<Integer> getNumPrimes(int numberOfPrimes){
ArrayList<Integer> primes = new ArrayList<>(); //Holds the prime numbers return ArrayAlgorithms.longToInt(getNumPrimes((long)numberOfPrimes));
boolean foundFactor = false; //A flag for whether a factor of the current number has been found
//If the number is 0 or negative return an empty list
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 can now start at 3 and skip 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;(primesCnt < primes.size()) && (primes.get(primesCnt) <= topPossibleFactor.intValue());++primesCnt){
if((possiblePrime % primes.get(primesCnt)) == 0){
foundFactor = true;
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 List<Long> getNumPrimes(Long numberOfPrimes){ public static List<Long> getNumPrimes(long numberOfPrimes){
ArrayList<Long> primes = new ArrayList<>(); //Holds the prime numbers ArrayList<Long> primes = new ArrayList<>(); //Holds the prime numbers
boolean foundFactor = false; //A flag for whether a factor of the current number has been found boolean foundFactor = false; //A flag for whether a factor of the current number has been found
@@ -274,20 +204,6 @@ public class NumberAlgorithms{
//This function return true if the value passed to it is prime //This function return true if the value passed to it is prime
public static boolean isPrime(int possiblePrime){
if(possiblePrime <= 3){
return possiblePrime > 1;
}
else if(((possiblePrime % 2) == 0) || ((possiblePrime % 3) == 0)){
return false;
}
for(int cnt = 5;(cnt * cnt) <= possiblePrime;cnt += 6){
if(((possiblePrime % cnt) == 0) || ((possiblePrime % (cnt + 2)) == 0)){
return false;
}
}
return true;
}
public static boolean isPrime(long possiblePrime){ public static boolean isPrime(long possiblePrime){
if(possiblePrime <= 3){ if(possiblePrime <= 3){
return possiblePrime > 1; return possiblePrime > 1;
@@ -319,41 +235,10 @@ public class NumberAlgorithms{
//This function returns all factors of goalNumber //This function returns all factors of goalNumber
public static List<Integer> getFactors(Integer goalNumber) throws InvalidResult{ public static List<Integer> getFactors(int goalNumber) throws InvalidResult{
//You need to get all the primes that could be factors of this number so you can test them return ArrayAlgorithms.longToInt(getFactors((long)goalNumber));
Double topPossiblePrime = Math.ceil(Math.sqrt(goalNumber));
List<Integer> primes = getPrimes(topPossiblePrime.intValue());
ArrayList<Integer> factors = new ArrayList<>();
//You need to step through each prime and see if it is a factor in the number
for(int cnt = 0;cnt < primes.size();){
//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.isEmpty()){
factors.add(goalNumber);
goalNumber /= goalNumber;
}
//If for some reason the goalNumber is not 1 throw an error
if(goalNumber != 1){
throw new InvalidResult("The factor was not 1: " + goalNumber);
}
//Return the list of factors
return factors;
} }
public static List<Long> getFactors(Long goalNumber) throws InvalidResult{ public static List<Long> getFactors(long goalNumber) throws InvalidResult{
//You need to get all the primes that could be factors of this number so you can test them //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)); Double topPossiblePrime = Math.ceil(Math.sqrt(goalNumber));
List<Long> primes = getPrimes(topPossiblePrime.longValue()); List<Long> primes = getPrimes(topPossiblePrime.longValue());
@@ -424,36 +309,10 @@ public class NumberAlgorithms{
//This function returns all the divisors of goalNumber //This function returns all the divisors of goalNumber
public static List<Integer> getDivisors(Integer goalNumber){ public static List<Integer> getDivisors(int goalNumber){
HashSet<Integer> divisors = new HashSet<>(); return ArrayAlgorithms.longToInt(getDivisors((long)goalNumber));
//Start by checking that the number is positive
if(goalNumber <= 0){
return new ArrayList<>();
}
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(int possibleDivisor = 2;possibleDivisor <= topPossibleDivisor;++possibleDivisor){
//If you find one add it and the number it creates to the list
if((goalNumber % possibleDivisor) == 0){
int possibleDivisor2 = goalNumber / possibleDivisor;
divisors.add(possibleDivisor);
divisors.add(possibleDivisor2);
}
}
//Convert the set to a list
ArrayList<Integer> divisorList = new ArrayList<>(divisors);
//Sort the list before returning it for neatness
Collections.sort(divisorList);
//Return the list
return divisorList;
} }
public static List<Long> getDivisors(Long goalNumber){ public static List<Long> getDivisors(long goalNumber){
HashSet<Long> divisors = new HashSet<>(); HashSet<Long> divisors = new HashSet<>();
//Start by checking that the number is positive //Start by checking that the number is positive
if(goalNumber <= 0){ if(goalNumber <= 0){
@@ -512,22 +371,7 @@ public class NumberAlgorithms{
//This function returns the goalSubscript'th Fibonacci number //This function returns the goalSubscript'th Fibonacci number
public static int getFib(int goalSubscript){ public static int getFib(int goalSubscript){
//Setup the variables return (int)getFib((long)goalSubscript);
int[] 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
int fibLoc;
for(fibLoc = 2;fibLoc < goalSubscript;++fibLoc){
fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3];
}
//Return the proper number. The location counter is 1 off of the subscript
return fibNums[(fibLoc - 1) % 3];
} }
public static long getFib(long goalSubscript){ public static long getFib(long goalSubscript){
//Setup the variables //Setup the variables
@@ -567,29 +411,10 @@ public class NumberAlgorithms{
} }
//This function returns a list of all Fibonacci numbers <= goalNumber //This function returns a list of all Fibonacci numbers <= goalNumber
public static List<Integer> getAllFib(Integer goalNumber){ public static List<Integer> getAllFib(int goalNumber){
//Setup the variables return ArrayAlgorithms.longToInt(getAllFib((long) goalNumber));
ArrayList<Integer> 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 List<Long> getAllFib(Long goalNumber){ public static List<Long> getAllFib(long goalNumber){
//Setup the variables //Setup the variables
ArrayList<Long> fibNums = new ArrayList<>(); //A list to save the Fibonacci numbers ArrayList<Long> fibNums = new ArrayList<>(); //A list to save the Fibonacci numbers
@@ -636,18 +461,7 @@ public class NumberAlgorithms{
//This function returns the factorial of the number passed to it //This function returns the factorial of the number passed to it
public static int factorial(int num) throws InvalidParameterException{ public static int factorial(int num) throws InvalidParameterException{
int fact = 1; //The value of the factorial return (int)factorial((long)num);
//If the number passed in is < 0 throw an exception
if(num < 0){
throw new InvalidParameterException("n! cannot be negative");
}
//Loop through every number up to and including num and add the product to the factorial
for(int cnt = 2;cnt <= num;++cnt){
fact *= cnt;
}
return fact;
} }
public static long factorial(long num) throws InvalidParameterException{ public static long factorial(long num) throws InvalidParameterException{
long fact = 1L; //The value of the factorial long fact = 1L; //The value of the factorial
@@ -680,15 +494,7 @@ public class NumberAlgorithms{
//This function returns the GCD of the two numbers sent to it //This function returns the GCD of the two numbers sent to it
public static int gcd(int num1, int num2){ public static int gcd(int num1, int num2){
while((num1 != 0) && (num2 != 0)){ return (int)gcd((long)num1, (long)num2);
if(num1 > num2){
num1 %= num2;
}
else{
num2 %= num1;
}
}
return num1 | num2;
} }
public static long gcd(long num1, long num2){ public static long gcd(long num1, long num2){
while((num1 != 0) && (num2 != 0)){ while((num1 != 0) && (num2 != 0)){
@@ -714,10 +520,6 @@ public class NumberAlgorithms{
} }
//Converts a number to its binary equivalent //Converts a number to its binary equivalent
public static String toBin(int num){
//Convert the number to a binary string
return Integer.toBinaryString(num);
}
public static String toBin(long num){ public static String toBin(long num){
//Convert the number to binary string //Convert the number to binary string
return Long.toBinaryString(num); return Long.toBinaryString(num);

View File

@@ -38,13 +38,13 @@ public class TestNumberAlgorithms{
public void testGetPrimes(){ public void testGetPrimes(){
//Test 1 //Test 1
List<Integer> correctAnswer = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97); List<Integer> correctAnswer = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97);
Integer topNum = 100; int topNum = 100;
List<Integer> answer = NumberAlgorithms.getPrimes(topNum); List<Integer> answer = NumberAlgorithms.getPrimes(topNum);
assertEquals("getPrimes Integer failed", correctAnswer, answer); assertEquals("getPrimes Integer failed", correctAnswer, answer);
//Test 2 //Test 2
List<Long> longCorrectAnswer = Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L); List<Long> longCorrectAnswer = Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L);
Long longTopNum = 100L; long longTopNum = 100L;
List<Long> longAnswer = NumberAlgorithms.getPrimes(longTopNum); List<Long> longAnswer = NumberAlgorithms.getPrimes(longTopNum);
assertEquals("getPrimes Long failed", longCorrectAnswer, longAnswer); assertEquals("getPrimes Long failed", longCorrectAnswer, longAnswer);
@@ -59,13 +59,13 @@ public class TestNumberAlgorithms{
public void testGetNumPrimes(){ public void testGetNumPrimes(){
//Test 1 //Test 1
List<Integer> correctAnswer = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97); List<Integer> correctAnswer = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97);
Integer numPrimes = 25; int numPrimes = 25;
List<Integer> answer = NumberAlgorithms.getNumPrimes(numPrimes); List<Integer> answer = NumberAlgorithms.getNumPrimes(numPrimes);
assertEquals("getNumPrimes Integer failed", correctAnswer, answer); assertEquals("getNumPrimes Integer failed", correctAnswer, answer);
//Test 2 //Test 2
List<Long> longCorrectAnswer = Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L); List<Long> longCorrectAnswer = Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L);
Long longNumPrimes = 25L; long longNumPrimes = 25L;
List<Long> longAnswer = NumberAlgorithms.getNumPrimes(longNumPrimes); List<Long> longAnswer = NumberAlgorithms.getNumPrimes(longNumPrimes);
assertEquals("getNumPrimes Long failed", longCorrectAnswer, longAnswer); assertEquals("getNumPrimes Long failed", longCorrectAnswer, longAnswer);
@@ -146,7 +146,7 @@ public class TestNumberAlgorithms{
public void testGetFactors() throws InvalidResult{ public void testGetFactors() throws InvalidResult{
//Test 1 //Test 1
List<Integer> correctAnswer = Arrays.asList(2, 2, 5, 5); List<Integer> correctAnswer = Arrays.asList(2, 2, 5, 5);
Integer number = 100; int number = 100;
List<Integer> answer = NumberAlgorithms.getFactors(number); List<Integer> answer = NumberAlgorithms.getFactors(number);
assertEquals("getFactors Integer 1 failed", correctAnswer, answer); assertEquals("getFactors Integer 1 failed", correctAnswer, answer);
//Test 2 //Test 2
@@ -157,7 +157,7 @@ public class TestNumberAlgorithms{
//Test 3 //Test 3
List<Long> longCorrectAnswer = Arrays.asList(2L, 2L, 5L, 5L); List<Long> longCorrectAnswer = Arrays.asList(2L, 2L, 5L, 5L);
Long longNumber = 100L; long longNumber = 100L;
List<Long> longAnswer = NumberAlgorithms.getFactors(longNumber); List<Long> longAnswer = NumberAlgorithms.getFactors(longNumber);
assertEquals("getFactors Long failed", longCorrectAnswer, longAnswer); assertEquals("getFactors Long failed", longCorrectAnswer, longAnswer);
@@ -172,7 +172,7 @@ public class TestNumberAlgorithms{
public void testGetDivisors(){ public void testGetDivisors(){
//Test 1 //Test 1
List<Integer> correctAnswer = Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100); List<Integer> correctAnswer = Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100);
Integer topNum = 100; int topNum = 100;
List<Integer> answer = NumberAlgorithms.getDivisors(topNum); List<Integer> answer = NumberAlgorithms.getDivisors(topNum);
assertEquals("getDivisors Integer 1 failed", correctAnswer, answer); assertEquals("getDivisors Integer 1 failed", correctAnswer, answer);
//Test 2 //Test 2
@@ -207,9 +207,9 @@ public class TestNumberAlgorithms{
@Test @Test
public void testGetFib(){ public void testGetFib(){
//Test 1 //Test 1
Integer correctAnswer = 144; int correctAnswer = 144;
Integer number = 12; int number = 12;
Integer answer = NumberAlgorithms.getFib(number); int answer = NumberAlgorithms.getFib(number);
assertEquals("getFib Integer 1 failed", correctAnswer, answer); assertEquals("getFib Integer 1 failed", correctAnswer, answer);
//Test 2 //Test 2
correctAnswer = 6765; correctAnswer = 6765;
@@ -218,9 +218,9 @@ public class TestNumberAlgorithms{
assertEquals("getFib Integer 2 failed", correctAnswer, answer); assertEquals("getFib Integer 2 failed", correctAnswer, answer);
//Test 3 //Test 3
Long longCorrectAnswer = 6765L; long longCorrectAnswer = 6765L;
Long longNumber = 20L; long longNumber = 20L;
Long longAnswer = NumberAlgorithms.getFib(longNumber); long longAnswer = NumberAlgorithms.getFib(longNumber);
assertEquals("getFib Long failed", longCorrectAnswer, longAnswer); assertEquals("getFib Long failed", longCorrectAnswer, longAnswer);
//Test 4 //Test 4
@@ -234,7 +234,7 @@ public class TestNumberAlgorithms{
public void testGetAllFib(){ public void testGetAllFib(){
//Test 1 //Test 1
List<Integer> correctAnswer = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89); List<Integer> correctAnswer = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89);
Integer highestNumber = 100; int highestNumber = 100;
List<Integer> answer = NumberAlgorithms.getAllFib(highestNumber); List<Integer> answer = NumberAlgorithms.getAllFib(highestNumber);
assertEquals("getAllFib Integer 1 failed", correctAnswer, answer); assertEquals("getAllFib Integer 1 failed", correctAnswer, answer);
//Test 2 //Test 2
@@ -245,7 +245,7 @@ public class TestNumberAlgorithms{
//Test 3 //Test 3
List<Long> longCorrectAnswer = Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L); List<Long> longCorrectAnswer = Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L);
Long longHighestNumber = 1000L; long longHighestNumber = 1000L;
List<Long> longAnswer = NumberAlgorithms.getAllFib(longHighestNumber); List<Long> longAnswer = NumberAlgorithms.getAllFib(longHighestNumber);
assertEquals("getAllFib Long failed", longCorrectAnswer, longAnswer); assertEquals("getAllFib Long failed", longCorrectAnswer, longAnswer);
@@ -260,9 +260,9 @@ public class TestNumberAlgorithms{
public void testFactorial(){ public void testFactorial(){
//Integer //Integer
//Test 1 //Test 1
Integer correctAnswer = 720; int correctAnswer = 720;
Integer number = 6; int number = 6;
Integer answer = NumberAlgorithms.factorial(number); int answer = NumberAlgorithms.factorial(number);
assertEquals("factorial Integer 1 failed", correctAnswer, answer); assertEquals("factorial Integer 1 failed", correctAnswer, answer);
//Test 2 //Test 2
correctAnswer = 479001600; correctAnswer = 479001600;
@@ -272,9 +272,9 @@ public class TestNumberAlgorithms{
//Long //Long
//Test 3 //Test 3
Long correctAnswerLong = 720L; long correctAnswerLong = 720L;
Long numberLong = 6L; long numberLong = 6L;
Long answerLong = NumberAlgorithms.factorial(numberLong); long answerLong = NumberAlgorithms.factorial(numberLong);
assertEquals("factorial Long 1 failed", correctAnswerLong, answerLong); assertEquals("factorial Long 1 failed", correctAnswerLong, answerLong);
//Test 4 //Test 4
correctAnswerLong = 479001600L; correctAnswerLong = 479001600L;
@@ -309,6 +309,7 @@ public class TestNumberAlgorithms{
answerBig = NumberAlgorithms.factorial(numberBig); answerBig = NumberAlgorithms.factorial(numberBig);
assertEquals("factorial BigInteger 4 failed", correctAnswerBig, answerBig); assertEquals("factorial BigInteger 4 failed", correctAnswerBig, answerBig);
} }
@Test @Test
public void testGCD(){ public void testGCD(){
//Test 1 //Test 1
@@ -368,6 +369,7 @@ public class TestNumberAlgorithms{
bigAnswer = NumberAlgorithms.gcd(bigNum1, bigNum2); bigAnswer = NumberAlgorithms.gcd(bigNum1, bigNum2);
assertEquals("GCD BigInteger 3 failed", bigCorrectAnswer, bigAnswer); assertEquals("GCD BigInteger 3 failed", bigCorrectAnswer, bigAnswer);
} }
@Test @Test
public void testToBin(){ public void testToBin(){
//Test 1 //Test 1

View File

@@ -1 +0,0 @@
Create a Fibonacci number generator