I am trying to write a function in Java that will return the number of factors a specific number has.
The following restrictions should be taken into account.
- It should be done with BigInteger
- Storing the previous generated numbers are not allowed, thus more processing and less memory.(You can not use "Sieve of Atkin" like in this)
- Negative numbers can be ignored.
This is what I have so far, but it is extremely slow.
public static int getNumberOfFactors(BigInteger number) {
// If the number is 1
int numberOfFactors = 1;
if (number.compareTo(BigInteger.ONE) <= 0) {
return numberOfFactors;
}
BigInteger boundry = number.divide(new BigInteger("2"));
BigInteger counter = new BigInteger("2");
while (counter.compareTo(boundry) <= 0) {
if (number.mod(counter).compareTo(BigInteger.ZERO) == 0) {
numberOfFactors++;
}
counter = counter.add(BigInteger.ONE);
}
// For the number it self
numberOfFactors++;
return numberOfFactors;
}