Updated to improve efficiency

This commit is contained in:
2020-06-15 12:05:19 -04:00
parent b5be5aa4f1
commit dc510c2bc7

View File

@@ -35,7 +35,7 @@ public class Algorithms{
//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
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
//If the number is 0 or negative return an empty list
if(goalNumber <= 1){
@@ -80,7 +80,7 @@ public class Algorithms{
}
public static ArrayList<Long> getPrimes(Long goalNumber){
ArrayList<Long> primes = new ArrayList<Long>(); //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
//If the numebr is 0 or negative return an empty list
if(goalNumber <= 1){
@@ -92,7 +92,7 @@ public class Algorithms{
}
//We cna now start at 3 and skipp all even numbers, because they cannot be prime
for(Long possiblePrime = 3L;possiblePrime <= goalNumber;possiblePrime += 2L){
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
@@ -125,7 +125,7 @@ public class Algorithms{
}
public static ArrayList<BigInteger> getPrimes(BigInteger goalNumber){
ArrayList<BigInteger> primes = new ArrayList<BigInteger>(); //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
//If the number is 1, 0 or negative return an empty list
if(goalNumber.compareTo(BigInteger.valueOf(1)) <= 0){
@@ -171,7 +171,7 @@ public class Algorithms{
//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
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
//If the numebr is 0 or negative return an empty list
if(numberOfPrimes <= 1){
@@ -216,7 +216,7 @@ public class Algorithms{
}
public static ArrayList<Long> getNumPrimes(Long numberOfPrimes){
ArrayList<Long> primes = new ArrayList<Long>(); //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
//If the numebr is 0 or negative return an empty list
if(numberOfPrimes <= 1){
@@ -228,7 +228,7 @@ public class Algorithms{
}
//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){
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
@@ -261,7 +261,7 @@ public class Algorithms{
}
public static ArrayList<BigInteger> getNumPrimes(BigInteger numberOfPrimes){
ArrayList<BigInteger> primes = new ArrayList<BigInteger>(); //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
//If the numebr is 0 or negative return an empty list
if(numberOfPrimes.compareTo(BigInteger.valueOf(1)) <= 0){
@@ -362,8 +362,7 @@ public class Algorithms{
goalNumber /= goalNumber;
}
//If for some reason the goalNumber is not 1 throw an error
///Need to add the appropriate error here
//TODO: If for some reason the goalNumber is not 1 throw an error
//Return the list of factors
return factors;
@@ -394,8 +393,7 @@ public class Algorithms{
goalNumber.divide(goalNumber);
}
//If for some reason the goalNumber is not 1 throw an error
///Need to add the appropriate error here
//TODO: If for some reason the goalNumber is not 1 throw an error
//Return the list of factors
return factors;
@@ -415,7 +413,7 @@ public class Algorithms{
//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 = 1;possibleDivisor <= topPossibleDivisor;++possibleDivisor){
for(int possibleDivisor = 1;possibleDivisor <= topPossibleDivisor;++possibleDivisor){
//If you find one add it and the number it creates to the list
if((goalNumber % possibleDivisor) == 0){
divisors.add(possibleDivisor);
@@ -449,7 +447,7 @@ public class Algorithms{
//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 = 1L;possibleDivisor <= topPossibleDivisor;++possibleDivisor){
//If you find one add it and the number it creates to the list
if((goalNumber % possibleDivisor) == 0){
divisors.add(possibleDivisor);
@@ -504,9 +502,9 @@ public class Algorithms{
return divisors;
}
//This function returns all the divisors of goalNumber
public static Integer getFib(Integer goalSubscript){
public static int 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
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){
@@ -514,7 +512,7 @@ public class Algorithms{
}
//Loop through the list, generating Fibonacci numbers until it finds the correct subscript
Integer fibLoc = 2;
int fibLoc = 2;
for(fibLoc = 2;fibLoc < goalSubscript;++fibLoc){
fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3];
}
@@ -522,9 +520,9 @@ 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){
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
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){
@@ -532,7 +530,7 @@ public class Algorithms{
}
//Loop through the list, generating Fibonacci numbers until it finds the correct subscript
Integer fibLoc = 2;
int fibLoc = 2;
for(fibLoc = 2;fibLoc < goalSubscript;++fibLoc){
fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3];
}
@@ -550,7 +548,7 @@ public class Algorithms{
}
//Loop through the list, generating Fibonacci numbers until it finds the correct subscript
Integer fibLoc = 2;
int fibLoc = 2;
for(fibLoc = 2;goalSubscript.compareTo(BigInteger.valueOf(fibLoc)) > 0;++fibLoc){
fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3].add(fibNums[(fibLoc - 2) % 3]);
}
@@ -626,29 +624,29 @@ public class Algorithms{
return fibNums;
}
//This function returns the factorial of the number passed to it
public static Integer factorial(Integer num) throws InvalidParameterException{
Integer fact = 1; //The value of the factorial
public static int factorial(int num) throws InvalidParameterException{
int fact = 1; //The value of the factorial
//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(Integer cnt = 2;cnt.compareTo(num) <= 0;++cnt){
for(int cnt = 2;cnt <= num;++cnt){
fact *= cnt;
}
return fact;
}
public static Long factorial(Long num) throws InvalidParameterException{
Long fact = 1L; //The value of the factorial
public static long factorial(long num) throws InvalidParameterException{
long fact = 1L; //The value of the factorial
//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(Long cnt = 2L;cnt.compareTo(num) <= 0;++cnt){
for(long cnt = 2L;cnt <= num;++cnt){
fact *= cnt;
}
@@ -669,34 +667,34 @@ public class Algorithms{
return fact;
}
//This function returns the sum of all elements in the list
public static Integer getSum(ArrayList<Integer> nums){
public static int getSum(ArrayList<Integer> 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;
int sum = 0;
//Loop through every element in the list and add them together
for(Integer num : nums){
for(int num : nums){
sum += num;
}
//Return the sum of all elements
return sum;
}
public static Long getLongSum(ArrayList<Long> nums){
public static long getLongSum(ArrayList<Long> 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;
long sum = 0L;
//Loop through every element in the list and add them together
for(Long num : nums){
for(long num : nums){
sum += num;
}
@@ -728,27 +726,27 @@ public class Algorithms{
}
//Setup the variables
Integer product = 1; //Start at 1 because x * 1 = x
int product = 1; //Start at 1 because x * 1 = x
//Loop through every element in the list and multiply them together
for(Integer num : nums){
for(int num : nums){
product *= num;
}
//Return the product of all elements
return product;
}
public static Long getLongProd(ArrayList<Long> nums){
public static long getLongProd(ArrayList<Long> 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
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){
for(long num : nums){
product *= num;
}
@@ -810,7 +808,7 @@ public class Algorithms{
public static ArrayList<String> getPermutations(String master){
return getPermutations(master, 0);
}
private static ArrayList<String> getPermutations(String master, Integer num){
private static ArrayList<String> getPermutations(String master, int num){
ArrayList<String> perms = new ArrayList<String>();
//Check if the number is out of bounds
if((num >= master.length()) || (num < 0)){
@@ -827,7 +825,7 @@ public class Algorithms{
perms.addAll(temp);
//You need to swap the current letter with every possible letter after it
//The ones needed to swap before will happen automatically when the function recurses
for(Integer cnt = 1;(num + cnt) < master.length();++cnt){
for(int cnt = 1;(num + cnt) < master.length();++cnt){
master = swapString(master, num, (num + cnt));
temp = getPermutations(master, num + 1);
perms.addAll(temp);
@@ -843,7 +841,7 @@ public class Algorithms{
//Return the arraylist that was built
return perms;
}
public static String swapString(String str, Integer first, Integer second){
public static String swapString(String str, int first, int second){
char[] tempStr = str.toCharArray();
char temp = tempStr[first];
tempStr[first] = tempStr[second];