mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-06 23:13:57 -05:00
Added functions and tests for Long type
This commit is contained in:
Binary file not shown.
@@ -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<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
|
||||
|
||||
//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<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
|
||||
@@ -169,6 +214,51 @@ public class Algorithms{
|
||||
Collections.sort(primes);
|
||||
return primes;
|
||||
}
|
||||
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
|
||||
|
||||
//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<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
|
||||
@@ -247,6 +337,38 @@ public class Algorithms{
|
||||
//Return the list of factors
|
||||
return factors;
|
||||
}
|
||||
public static ArrayList<Long> 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<Long> primes = getPrimes(topPossiblePrime.longValue());
|
||||
ArrayList<Long> factors = new ArrayList<Long>();
|
||||
|
||||
//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<BigInteger> 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<Long> getDivisors(Long goalNumber){
|
||||
ArrayList<Long> divisors = new ArrayList<Long>();
|
||||
//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<BigInteger> getDivisors(BigInteger goalNumber){
|
||||
ArrayList<BigInteger> divisors = new ArrayList<BigInteger>();
|
||||
//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<Long> getAllFib(Long goalNumber){
|
||||
//Setup the variables
|
||||
ArrayList<Long> fibNums = new ArrayList<Long>(); //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<BigInteger> getAllFib(BigInteger goalNumber){
|
||||
//Setup the variables
|
||||
ArrayList<BigInteger> fibNums = new ArrayList<BigInteger>(); //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<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;
|
||||
|
||||
//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<BigInteger> 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<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
|
||||
|
||||
//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<BigInteger> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(nums.size() == 0){
|
||||
|
||||
Reference in New Issue
Block a user