diff --git a/src/main/java/com/mattrixwv/NumberAlgorithms.java b/src/main/java/com/mattrixwv/NumberAlgorithms.java index 91e253f..0db0f1b 100644 --- a/src/main/java/com/mattrixwv/NumberAlgorithms.java +++ b/src/main/java/com/mattrixwv/NumberAlgorithms.java @@ -238,7 +238,7 @@ public class NumberAlgorithms{ } public static List 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(goalNumber / 2.0); + Double topPossiblePrime = Math.ceil(Math.sqrt(goalNumber)); List primes = getPrimes(topPossiblePrime.longValue()); ArrayList factors = new ArrayList<>(); @@ -264,7 +264,12 @@ public class NumberAlgorithms{ //If for some reason the goalNumber is not 1 throw an error if(goalNumber != 1){ - throw new InvalidResult("The factor was not 1: " + goalNumber); + if(isPrime(goalNumber)){ + factors.add(goalNumber); + } + else{ + throw new InvalidResult("The factor was not 1: " + goalNumber); + } } //Return the list of factors