Updated sonarqube findings
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/ArrayAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-03-21
|
||||
//Modified: 07-03-21
|
||||
//Modified: 06-25-22
|
||||
//This class contains algorithms for vectors 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
|
||||
@@ -23,17 +23,13 @@ package mattrixwv;
|
||||
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
|
||||
public class ArrayAlgorithms{
|
||||
private ArrayAlgorithms(){}
|
||||
//This function returns the sum of all elements in the list
|
||||
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;
|
||||
}
|
||||
|
||||
public static int getSum(Iterable<Integer> nums){
|
||||
//Setup the variables
|
||||
int sum = 0;
|
||||
|
||||
@@ -45,14 +41,9 @@ public class ArrayAlgorithms{
|
||||
//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;
|
||||
}
|
||||
|
||||
public static long getLongSum(Iterable<Long> nums){
|
||||
//Setup the variables
|
||||
long sum = 0L;
|
||||
long sum = 0;
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(long num : nums){
|
||||
@@ -62,14 +53,9 @@ public class ArrayAlgorithms{
|
||||
//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){
|
||||
return BigInteger.valueOf(0);
|
||||
}
|
||||
|
||||
public static BigInteger getBigSum(Iterable<BigInteger> nums){
|
||||
//Setup the variables
|
||||
BigInteger sum = BigInteger.valueOf(0);
|
||||
BigInteger sum = BigInteger.ZERO;
|
||||
|
||||
//Loop through every element in the list and add them together
|
||||
for(BigInteger num : nums){
|
||||
@@ -80,9 +66,9 @@ public class ArrayAlgorithms{
|
||||
return sum;
|
||||
}
|
||||
//This function returns the product of all elements in the list
|
||||
public static int getProd(ArrayList<Integer> nums){
|
||||
public static int getProd(Iterable<Integer> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(nums.size() == 0){
|
||||
if(!nums.iterator().hasNext()){
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -97,9 +83,9 @@ public class ArrayAlgorithms{
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
public static long getLongProd(ArrayList<Long> nums){
|
||||
public static long getLongProd(Iterable<Long> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(nums.size() == 0){
|
||||
if(!nums.iterator().hasNext()){
|
||||
return 0L;
|
||||
}
|
||||
|
||||
@@ -114,9 +100,9 @@ public class ArrayAlgorithms{
|
||||
//Return the product of all elements
|
||||
return product;
|
||||
}
|
||||
public static BigInteger getBigProd(ArrayList<BigInteger> nums){
|
||||
public static BigInteger getBigProd(Iterable<BigInteger> nums){
|
||||
//If a blank list was passed tot he fuction return 0 as the product
|
||||
if(nums.size() == 0){
|
||||
if(!nums.iterator().hasNext()){
|
||||
return BigInteger.valueOf(0);
|
||||
}
|
||||
|
||||
@@ -132,15 +118,11 @@ public class ArrayAlgorithms{
|
||||
return product;
|
||||
}
|
||||
//Print a list
|
||||
public static <T> String printList(ArrayList<T> list){
|
||||
StringBuilder listString = new StringBuilder("[");
|
||||
for(int cnt = 0;cnt < list.size();++cnt){
|
||||
listString.append(list.get(cnt));
|
||||
if(cnt < list.size() - 1){
|
||||
listString.append(", ");
|
||||
}
|
||||
public static <T> String printList(Iterable<T> list){
|
||||
StringJoiner returnString = new StringJoiner(", ", "[", "]");
|
||||
for(T obj : list){
|
||||
returnString.add(obj.toString());
|
||||
}
|
||||
listString.append("]");
|
||||
return listString.toString();
|
||||
return returnString.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenes.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-30-21
|
||||
//Modified: 06-30-21
|
||||
//Modified: 06-25-22
|
||||
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
|
||||
/*
|
||||
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
|
||||
@@ -27,6 +27,7 @@ import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
public class SieveOfEratosthenes implements Iterator<Long>{
|
||||
@@ -34,7 +35,7 @@ public class SieveOfEratosthenes implements Iterator<Long>{
|
||||
private Map<Long, ArrayList<Long>> dict;
|
||||
|
||||
public SieveOfEratosthenes(){
|
||||
dict = new HashMap<Long, ArrayList<Long>>();
|
||||
dict = new HashMap<>();
|
||||
possiblePrime = 2;
|
||||
}
|
||||
@Override
|
||||
@@ -44,39 +45,44 @@ public class SieveOfEratosthenes implements Iterator<Long>{
|
||||
@Override
|
||||
public Long next(){
|
||||
long prime;
|
||||
if(possiblePrime > 2){
|
||||
//Loop until you find a prime number
|
||||
for(;dict.containsKey(possiblePrime);possiblePrime += 2){
|
||||
//Create the next entry for all entries in the map
|
||||
for(long num : dict.get(possiblePrime)){
|
||||
if(!dict.containsKey(possiblePrime + num + num)){
|
||||
ArrayList<Long> tempArray = new ArrayList<Long>(Arrays.asList(num));
|
||||
dict.put(possiblePrime + num + num, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(possiblePrime + num + num).add(num);
|
||||
}
|
||||
|
||||
//If this is the first run just return 2
|
||||
if(possiblePrime <= 2){
|
||||
prime = possiblePrime++;
|
||||
return prime;
|
||||
}
|
||||
|
||||
//Loop until you find a prime number
|
||||
for(;dict.containsKey(possiblePrime);possiblePrime += 2){
|
||||
if(possiblePrime < 0){
|
||||
throw new NoSuchElementException("the next prime cannot be described by a long");
|
||||
}
|
||||
//Create the next entry for all entries in the map
|
||||
for(long num : dict.get(possiblePrime)){
|
||||
if(!dict.containsKey(possiblePrime + num + num)){
|
||||
ArrayList<Long> tempArray = new ArrayList<>(Arrays.asList(num));
|
||||
dict.put(possiblePrime + num + num, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(possiblePrime + num + num).add(num);
|
||||
}
|
||||
//Delete the current entry
|
||||
dict.remove(possiblePrime);
|
||||
}
|
||||
//Save that the number is a prime
|
||||
prime = possiblePrime;
|
||||
//Add the next entry to the prime
|
||||
if(!dict.containsKey(prime * 3)){
|
||||
ArrayList<Long> tempArray = new ArrayList<Long>(Arrays.asList(prime));
|
||||
dict.put(prime * 3, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(prime * 3).add(prime);
|
||||
}
|
||||
//Move on to the next possible prime
|
||||
possiblePrime += 2;
|
||||
//Delete the current entry
|
||||
dict.remove(possiblePrime);
|
||||
}
|
||||
//Save that the number is a prime
|
||||
prime = possiblePrime;
|
||||
//Add the next entry to the prime
|
||||
if(!dict.containsKey(prime * 3)){
|
||||
ArrayList<Long> tempArray = new ArrayList<>(Arrays.asList(prime));
|
||||
dict.put(prime * 3, tempArray);
|
||||
}
|
||||
else{
|
||||
//Return 2 and move to 3
|
||||
prime = possiblePrime++;
|
||||
dict.get(prime * 3).add(prime);
|
||||
}
|
||||
//Move on to the next possible prime
|
||||
possiblePrime += 2;
|
||||
|
||||
return prime;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenesBig.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-30-21
|
||||
//Modified: 06-30-21
|
||||
//Modified: 06-25-22
|
||||
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
|
||||
/*
|
||||
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
|
||||
@@ -35,7 +35,7 @@ public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
|
||||
private Map<BigInteger, ArrayList<BigInteger>> dict;
|
||||
|
||||
public SieveOfEratosthenesBig(){
|
||||
dict = new HashMap<BigInteger, ArrayList<BigInteger>>();
|
||||
dict = new HashMap<>();
|
||||
possiblePrime = BigInteger.TWO;
|
||||
}
|
||||
@Override
|
||||
@@ -45,41 +45,43 @@ public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
|
||||
@Override
|
||||
public BigInteger next(){
|
||||
BigInteger prime;
|
||||
if(possiblePrime.compareTo(BigInteger.TWO) > 0){
|
||||
//Loop until you find a prime number
|
||||
for(;dict.containsKey(possiblePrime);possiblePrime = possiblePrime.add(BigInteger.TWO)){
|
||||
//Create the next entry for all entries in the map
|
||||
for(BigInteger num : dict.get(possiblePrime)){
|
||||
BigInteger loc = possiblePrime.add(num).add(num);
|
||||
if(!dict.containsKey(loc)){
|
||||
ArrayList<BigInteger> tempArray = new ArrayList<BigInteger>(Arrays.asList(num));
|
||||
dict.put(loc, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(loc).add(num);
|
||||
}
|
||||
}
|
||||
//Delete the current entry
|
||||
dict.remove(possiblePrime);
|
||||
}
|
||||
//Save that the number is a prime
|
||||
prime = possiblePrime;
|
||||
BigInteger loc = prime.multiply(BigInteger.valueOf(3));
|
||||
if(!dict.containsKey(loc)){
|
||||
ArrayList<BigInteger> tempArray = new ArrayList<BigInteger>(Arrays.asList(prime));
|
||||
dict.put(loc, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(loc).add(prime);
|
||||
}
|
||||
//Move on to the next possible prime
|
||||
possiblePrime = possiblePrime.add(BigInteger.TWO);
|
||||
}
|
||||
else{
|
||||
|
||||
if(possiblePrime.compareTo(BigInteger.TWO) <= 0){
|
||||
//Return 2 and move to 3
|
||||
prime = possiblePrime;
|
||||
possiblePrime = possiblePrime.add(BigInteger.ONE);
|
||||
return prime;
|
||||
}
|
||||
|
||||
//Loop until you find a prime number
|
||||
for(;dict.containsKey(possiblePrime);possiblePrime = possiblePrime.add(BigInteger.TWO)){
|
||||
//Create the next entry for all entries in the map
|
||||
for(BigInteger num : dict.get(possiblePrime)){
|
||||
BigInteger loc = possiblePrime.add(num).add(num);
|
||||
if(!dict.containsKey(loc)){
|
||||
ArrayList<BigInteger> tempArray = new ArrayList<>(Arrays.asList(num));
|
||||
dict.put(loc, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(loc).add(num);
|
||||
}
|
||||
}
|
||||
//Delete the current entry
|
||||
dict.remove(possiblePrime);
|
||||
}
|
||||
//Save that the number is a prime
|
||||
prime = possiblePrime;
|
||||
BigInteger loc = prime.multiply(BigInteger.valueOf(3));
|
||||
if(!dict.containsKey(loc)){
|
||||
ArrayList<BigInteger> tempArray = new ArrayList<>(Arrays.asList(prime));
|
||||
dict.put(loc, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(loc).add(prime);
|
||||
}
|
||||
//Move on to the next possible prime
|
||||
possiblePrime = possiblePrime.add(BigInteger.TWO);
|
||||
|
||||
return prime;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,15 +24,17 @@ package mattrixwv;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class StringAlgorithms{
|
||||
private StringAlgorithms(){}
|
||||
//This is a function that creates all permutations of a string and returns a vector of those permutations.
|
||||
public static ArrayList<String> getPermutations(String master){
|
||||
public static List<String> getPermutations(String master){
|
||||
return getPermutations(master, 0);
|
||||
}
|
||||
private static ArrayList<String> getPermutations(String master, int num){
|
||||
ArrayList<String> perms = new ArrayList<String>();
|
||||
ArrayList<String> perms = new ArrayList<>();
|
||||
//Check if the number is out of bounds
|
||||
if((num >= master.length()) || (num < 0)){
|
||||
//Do nothing and return an empty arraylist
|
||||
@@ -69,8 +71,7 @@ public class StringAlgorithms{
|
||||
tempStr[first] = tempStr[second];
|
||||
tempStr[second] = temp;
|
||||
|
||||
String swappedString = new String(tempStr);
|
||||
return swappedString;
|
||||
return new String(tempStr);
|
||||
}
|
||||
//This function returns the number of times the character occurs in the string
|
||||
public static long findNumOccurrence(String str, char c){
|
||||
@@ -79,12 +80,7 @@ public class StringAlgorithms{
|
||||
//Returns true if the string passed in is a palindrome
|
||||
public static boolean isPalindrome(String str){
|
||||
String rev = new StringBuilder(str).reverse().toString();
|
||||
if(str.equals(rev)){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
return str.equals(rev);
|
||||
}
|
||||
//Returns true if the string passed to it is a pandigital
|
||||
public static boolean isPandigital(String str, char bottom, char top){
|
||||
|
||||
Reference in New Issue
Block a user