Fixed bug in getDivisors where number could be added to list twice

This commit is contained in:
2019-03-22 17:37:05 -04:00
parent caebee7e6d
commit 8f4d8664f4
2 changed files with 20 additions and 20 deletions

View File

@@ -401,7 +401,7 @@ public class Algorithms{
return factors;
}
//This function returns all the divisors of goalNumber
public static ArrayList<Integer> getDivisors(int goalNumber){
public static ArrayList<Integer> getDivisors(Integer goalNumber){
ArrayList<Integer> divisors = new ArrayList<Integer>();
//Start by checking that the number is positive
if(goalNumber <= 0){
@@ -410,16 +410,12 @@ public class Algorithms{
//If the number is 1 return just itself
else if(goalNumber == 1){
divisors.add(1);
}
//Otherwise add 1 and itself to the list
else{
divisors.add(1);
divisors.add(goalNumber);
return divisors;
}
//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 = 2;possibleDivisor <= topPossibleDivisor;++possibleDivisor){
for(Integer 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);
@@ -427,6 +423,10 @@ public class Algorithms{
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;
}
}
}
@@ -444,23 +444,23 @@ public class Algorithms{
//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);
return divisors;
}
//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){
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);
//Accound for the possibility of sqrt(goalNumber) being a divisor
if(possibleDivisor != topPossibleDivisor.intValue()){
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;
}
}
}
@@ -478,16 +478,12 @@ public class Algorithms{
//If the number is 1 return just itself
else if(goalNumber.equals(BigInteger.valueOf(1))){
divisors.add(BigInteger.valueOf(1));
}
//Otherwise add 1 and itself to the list
else{
divisors.add(BigInteger.valueOf(1));
divisors.add(goalNumber);
return divisors;
}
//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(2);possibleDivisor.compareTo(topPossibleDivisor) <= 0;possibleDivisor = possibleDivisor.add(BigInteger.valueOf(1))){
for(BigInteger possibleDivisor = BigInteger.valueOf(1);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))){
divisors.add(possibleDivisor);
@@ -495,6 +491,10 @@ public class Algorithms{
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));
}
}
}