Updated algorithm to get factors of number

This commit is contained in:
2022-12-04 13:09:50 -05:00
parent a845925f99
commit 86935cd0bf
2 changed files with 3 additions and 3 deletions

View File

@@ -238,7 +238,7 @@ public class NumberAlgorithms{
} }
public static List<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 //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)); Double topPossiblePrime = Math.ceil(goalNumber / 2.0);
List<Long> primes = getPrimes(topPossiblePrime.longValue()); List<Long> primes = getPrimes(topPossiblePrime.longValue());
ArrayList<Long> factors = new ArrayList<>(); ArrayList<Long> factors = new ArrayList<>();
@@ -272,7 +272,7 @@ public class NumberAlgorithms{
} }
public static List<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 //You need to get all the primes that could be factors of this number so you can test them
BigInteger topPossiblePrime = goalNumber.sqrt(); BigInteger topPossiblePrime = goalNumber.divide(BigInteger.TWO);
List<BigInteger> primes = getPrimes(topPossiblePrime); List<BigInteger> primes = getPrimes(topPossiblePrime);
ArrayList<BigInteger> factors = new ArrayList<>(); ArrayList<BigInteger> factors = new ArrayList<>();

View File

@@ -6,7 +6,7 @@
package com.mattrixwv.exceptions; package com.mattrixwv.exceptions;
public class InvalidResult extends Exception{ public class InvalidResult extends RuntimeException{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public InvalidResult(){ public InvalidResult(){
super(); super();