Updated sonarqube findings

This commit is contained in:
2022-06-26 11:53:01 -04:00
parent c7ac267c25
commit 4ec1346476
7 changed files with 262 additions and 291 deletions

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/NumberAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 07-03-21
//Modified: 06-25-22
//This class contains algorithms for numbers that I've found it useful to keep around
/*
Copyright (C) 2021 Matthew Ellison
Copyright (C) 2022 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
@@ -26,18 +26,22 @@ import java.math.BigInteger;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import mattrixwv.exceptions.InvalidResult;
public class NumberAlgorithms{
private NumberAlgorithms(){}
//?This is here just to prove that templates exist and for a possible rewrite at a later time
public static <T> T getNum(T num1){
return num1;
}
//This function returns a list with all the prime numbers <= goalNumber
public static ArrayList<Integer> getPrimes(Integer goalNumber){
ArrayList<Integer> primes = new ArrayList<Integer>(); //Holds the prime numbers
public static List<Integer> getPrimes(Integer goalNumber){
ArrayList<Integer> 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 number is 0 or negative return an empty list
@@ -54,18 +58,11 @@ public class NumberAlgorithms{
//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();){
for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt) <= topPossibleFactor.intValue());++primesCnt){
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
@@ -81,8 +78,8 @@ public class NumberAlgorithms{
Collections.sort(primes);
return primes;
}
public static ArrayList<Long> getPrimes(Long goalNumber){
ArrayList<Long> primes = new ArrayList<Long>(); //Holds the prime numbers
public static List<Long> getPrimes(Long goalNumber){
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
//If the numebr is 0 or negative return an empty list
@@ -99,18 +96,11 @@ public class NumberAlgorithms{
//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();){
for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt) <= topPossibleFactor.intValue());++primesCnt){
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
@@ -126,8 +116,8 @@ public class NumberAlgorithms{
Collections.sort(primes);
return primes;
}
public static ArrayList<BigInteger> getPrimes(BigInteger goalNumber){
ArrayList<BigInteger> primes = new ArrayList<BigInteger>(); //Holds the prime numbers
public static List<BigInteger> getPrimes(BigInteger goalNumber){
ArrayList<BigInteger> 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 number is 1, 0 or negative return an empty list
@@ -144,18 +134,11 @@ public class NumberAlgorithms{
//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;){
for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt).compareTo(topPossibleFactor) <= 0);++primesCnt){
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
@@ -171,9 +154,11 @@ public class NumberAlgorithms{
Collections.sort(primes);
return primes;
}
//This function gets a certain number of primes
public static ArrayList<Integer> getNumPrimes(Integer numberOfPrimes){
ArrayList<Integer> primes = new ArrayList<Integer>(); //Holds the prime numbers
public static List<Integer> getNumPrimes(Integer numberOfPrimes){
ArrayList<Integer> 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 number is 0 or negative return an empty list
@@ -190,18 +175,11 @@ public class NumberAlgorithms{
//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();){
for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt) <= topPossibleFactor.intValue());++primesCnt){
if((possiblePrime % primes.get(primesCnt)) == 0){
foundFactor = true;
break;
}
else{
++primesCnt;
}
//Check if the index has gone out of bounds
if(primesCnt >= primes.size()){
break;
}
}
//If you didn't find a factor then the current number must be prime
@@ -217,8 +195,8 @@ public class NumberAlgorithms{
Collections.sort(primes);
return primes;
}
public static ArrayList<Long> getNumPrimes(Long numberOfPrimes){
ArrayList<Long> primes = new ArrayList<Long>(); //Holds the prime numbers
public static List<Long> getNumPrimes(Long numberOfPrimes){
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
//If the number is 0 or negative return an empty list
@@ -235,18 +213,11 @@ public class NumberAlgorithms{
//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();){
for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt) <= topPossibleFactor.intValue());++primesCnt){
if((possiblePrime % primes.get(primesCnt)) == 0){
foundFactor = true;
break;
}
else{
++primesCnt;
}
//Check if the index has gone out of bounds
if(primesCnt >= primes.size()){
break;
}
}
//If you didn't find a factor then the current number must be prime
@@ -262,8 +233,8 @@ public class NumberAlgorithms{
Collections.sort(primes);
return primes;
}
public static ArrayList<BigInteger> getNumPrimes(BigInteger numberOfPrimes){
ArrayList<BigInteger> primes = new ArrayList<BigInteger>(); //Holds the prime numbers
public static List<BigInteger> getNumPrimes(BigInteger numberOfPrimes){
ArrayList<BigInteger> 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 number is 0 or negative return an empty list
@@ -280,18 +251,11 @@ public class NumberAlgorithms{
//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;){
for(int primesCnt = 0;(primesCnt < primes.size()) && (primes.get(primesCnt).compareTo(topPossibleFactor) <= 0);++primesCnt){
if((possiblePrime.mod(primes.get(primesCnt))) == BigInteger.valueOf(0)){
foundFactor = true;
break;
}
else{
++primesCnt;
}
//Check if the index has gone out of bounds
if(primesCnt >= primes.size()){
break;
}
}
//If you didn't find a factor then the current number must be prime
@@ -307,6 +271,8 @@ public class NumberAlgorithms{
Collections.sort(primes);
return primes;
}
//This function return true if the value passed to it is prime
public static boolean isPrime(int possiblePrime){
if(possiblePrime <= 3){
@@ -350,12 +316,14 @@ public class NumberAlgorithms{
}
return true;
}
//This function returns all factors of goalNumber
public static ArrayList<Integer> getFactors(Integer goalNumber) throws InvalidResult{
public static List<Integer> getFactors(Integer goalNumber) throws InvalidResult{
//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<Integer> primes = getPrimes(topPossiblePrime.intValue());
ArrayList<Integer> factors = new ArrayList<Integer>();
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();){
@@ -372,7 +340,7 @@ public class NumberAlgorithms{
}
//If you didn't get any factors the number itself must be a prime
if(factors.size() == 0){
if(factors.isEmpty()){
factors.add(goalNumber);
goalNumber /= goalNumber;
}
@@ -385,11 +353,11 @@ public class NumberAlgorithms{
//Return the list of factors
return factors;
}
public static ArrayList<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
Double topPossiblePrime = Math.ceil(Math.sqrt(goalNumber));
ArrayList<Long> primes = getPrimes(topPossiblePrime.longValue());
ArrayList<Long> factors = new ArrayList<Long>();
List<Long> primes = getPrimes(topPossiblePrime.longValue());
ArrayList<Long> 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();){
@@ -406,7 +374,7 @@ public class NumberAlgorithms{
}
//If you didn't get any factors the number itself must be a prime
if(factors.size() == 0){
if(factors.isEmpty()){
factors.add(goalNumber);
goalNumber /= goalNumber;
}
@@ -419,11 +387,11 @@ public class NumberAlgorithms{
//Return the list of factors
return factors;
}
public static ArrayList<BigInteger> getFactors(BigInteger goalNumber) throws InvalidResult{
public static List<BigInteger> getFactors(BigInteger goalNumber) throws InvalidResult{
//You need to get all the primes that could be factors of this number so you can test them
BigInteger topPossiblePrime = goalNumber.sqrt();
ArrayList<BigInteger> primes = getPrimes(topPossiblePrime);
ArrayList<BigInteger> factors = new ArrayList<BigInteger>();
List<BigInteger> primes = getPrimes(topPossiblePrime);
ArrayList<BigInteger> 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();){
@@ -440,9 +408,9 @@ public class NumberAlgorithms{
}
//If you didn't get any factors the number itself must be a prime
if(factors.size() == 0){
if(factors.isEmpty()){
factors.add(goalNumber);
goalNumber.divide(goalNumber);
goalNumber = goalNumber.divide(goalNumber);
}
//If for some reason the goalNumber is not 1 throw an error
@@ -453,109 +421,95 @@ public class NumberAlgorithms{
//Return the list of factors
return factors;
}
//This function returns all the divisors of goalNumber
public static ArrayList<Integer> getDivisors(Integer goalNumber){
ArrayList<Integer> divisors = new ArrayList<Integer>();
public static List<Integer> getDivisors(Integer goalNumber){
HashSet<Integer> divisors = new HashSet<>();
//Start by checking that the number is positive
if(goalNumber <= 0){
return divisors;
return new ArrayList<>();
}
//If the number is 1 return just itself
else if(goalNumber == 1){
else{
divisors.add(1);
return divisors;
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 = 1;possibleDivisor <= topPossibleDivisor;++possibleDivisor){
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);
//Account for the possibility of sqrt(goalNumber) being a divisor
if(possibleDivisor != topPossibleDivisor.intValue()){
divisors.add(goalNumber / possibleDivisor);
}
//Take care of a few occations where a number was added twice
if(divisors.get(divisors.size() - 1) == (possibleDivisor + 1)){
++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(divisors);
Collections.sort(divisorList);
//Return the list
return divisors;
return divisorList;
}
public static ArrayList<Long> getDivisors(Long goalNumber){
ArrayList<Long> divisors = new ArrayList<Long>();
public static List<Long> getDivisors(Long goalNumber){
HashSet<Long> divisors = new HashSet<>();
//Start by checking that the number is positive
if(goalNumber <= 0){
return divisors;
return new ArrayList<>();
}
//If the number is 1 return just itself
else if(goalNumber == 1){
else{
divisors.add(1L);
return divisors;
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 = 1L;possibleDivisor <= topPossibleDivisor;++possibleDivisor){
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){
long possibleDivisor2 = goalNumber / possibleDivisor;
divisors.add(possibleDivisor);
//Account for the possibility of sqrt(goalNumber) being a divisor
if(possibleDivisor != topPossibleDivisor.longValue()){
divisors.add(goalNumber / possibleDivisor);
}
//Take care of a few occations where a number was added twice
if(divisors.get(divisors.size() - 1) == (possibleDivisor + 1L)){
++possibleDivisor;
}
divisors.add(possibleDivisor2);
}
}
ArrayList<Long> divisorList = new ArrayList<>(divisors);
//Sort the list before returning it for neatness
Collections.sort(divisors);
Collections.sort(divisorList);
//Return the list
return divisors;
return divisorList;
}
public static ArrayList<BigInteger> getDivisors(BigInteger goalNumber){
ArrayList<BigInteger> divisors = new ArrayList<BigInteger>();
public static List<BigInteger> getDivisors(BigInteger goalNumber){
HashSet<BigInteger> divisors = new HashSet<>();
//Start by checking that the number is positive
if(goalNumber.compareTo(BigInteger.valueOf(0)) <= 0){
return divisors;
return new ArrayList<>();
}
//If the number is 1 return just itself
else if(goalNumber.equals(BigInteger.valueOf(1))){
else{
divisors.add(BigInteger.valueOf(1));
return divisors;
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(1);possibleDivisor.compareTo(topPossibleDivisor) <= 0;possibleDivisor = possibleDivisor.add(BigInteger.valueOf(1))){
for(BigInteger possibleDivisor = BigInteger.TWO;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))){
BigInteger possibleDivisor2 = goalNumber.divide(possibleDivisor);
divisors.add(possibleDivisor);
//Account for the possibility of sqrt(goalNumber) being a divisor
if(!possibleDivisor.equals(topPossibleDivisor)){
divisors.add(goalNumber.divide(possibleDivisor));
}
//Take care of a few occations where a number was added twice
if(divisors.get(divisors.size() - 1).equals(possibleDivisor.add(BigInteger.valueOf(1L)))){
possibleDivisor = possibleDivisor.add(BigInteger.valueOf(1));
}
divisors.add(possibleDivisor2);
}
}
ArrayList<BigInteger> divisorList = new ArrayList<>(divisors);
//Sort the list before returning it for neatness
Collections.sort(divisors);
Collections.sort(divisorList);
//Return the list
return divisors;
return divisorList;
}
//This function returns the goalSubscript'th Fibonacci number
public static int getFib(int goalSubscript){
//Setup the variables
@@ -567,7 +521,7 @@ public class NumberAlgorithms{
}
//Loop through the list, generating Fibonacci numbers until it finds the correct subscript
int fibLoc = 2;
int fibLoc;
for(fibLoc = 2;fibLoc < goalSubscript;++fibLoc){
fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3];
}
@@ -585,7 +539,7 @@ public class NumberAlgorithms{
}
//Loop through the list, generating Fibonacci numbers until it finds the correct subscript
int fibLoc = 2;
int fibLoc;
for(fibLoc = 2;fibLoc < goalSubscript;++fibLoc){
fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3];
}
@@ -603,7 +557,7 @@ public class NumberAlgorithms{
}
//Loop through the list, generating Fibonacci numbers until it finds the correct subscript
int fibLoc = 2;
int fibLoc;
for(fibLoc = 2;goalSubscript.compareTo(BigInteger.valueOf(fibLoc)) > 0;++fibLoc){
fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3].add(fibNums[(fibLoc - 2) % 3]);
}
@@ -611,10 +565,11 @@ public class NumberAlgorithms{
//Return the proper 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<Integer> getAllFib(Integer goalNumber){
public static List<Integer> getAllFib(Integer goalNumber){
//Setup the variables
ArrayList<Integer> fibNums = new ArrayList<Integer>(); //A list to save the Fibonacci numbers
ArrayList<Integer> fibNums = new ArrayList<>(); //A list to save the Fibonacci numbers
//If the number is <= 0 return an empty list
if(goalNumber <= 0){
@@ -634,9 +589,9 @@ public class NumberAlgorithms{
fibNums.remove(fibNums.size() - 1);
return fibNums;
}
public static ArrayList<Long> getAllFib(Long goalNumber){
public static List<Long> getAllFib(Long goalNumber){
//Setup the variables
ArrayList<Long> fibNums = new ArrayList<Long>(); //A list to save the Fibonacci numbers
ArrayList<Long> fibNums = new ArrayList<>(); //A list to save the Fibonacci numbers
//If the number is <= 0 return an empty list
if(goalNumber <= 0){
@@ -656,9 +611,9 @@ public class NumberAlgorithms{
fibNums.remove(fibNums.size() - 1);
return fibNums;
}
public static ArrayList<BigInteger> getAllFib(BigInteger goalNumber){
public static List<BigInteger> getAllFib(BigInteger goalNumber){
//Setup the variables
ArrayList<BigInteger> fibNums = new ArrayList<BigInteger>(); //A list to save the Fibonacci numbers
ArrayList<BigInteger> 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){
@@ -678,6 +633,7 @@ public class NumberAlgorithms{
fibNums.remove(fibNums.size() - 1);
return fibNums;
}
//This function returns the factorial of the number passed to it
public static int factorial(int num) throws InvalidParameterException{
int fact = 1; //The value of the factorial
@@ -721,6 +677,7 @@ public class NumberAlgorithms{
return fact;
}
//This function returns the GCD of the two numbers sent to it
public static int gcd(int num1, int num2){
while((num1 != 0) && (num2 != 0)){
@@ -755,6 +712,7 @@ public class NumberAlgorithms{
}
return num1.or(num2);
}
//Converts a number to its binary equivalent
public static String toBin(int num){
//Convert the number to a binary string