Another BigInteger problem. My code works for int and long, but since the test cases in UVa are larger, I need to use BigInteger. But I don’t know how to use BigInteger and it drives me nuts! The code doesn’t even enter the for-loop. It seems to be stuck in the condition part.
I tried using for or while, and they have the same problem.
public class Main{
public static void main(String[] asdf){
Scanner pp = new Scanner(System.in);
int testCases = pp.nextInt();
while(testCases-- > 0){
//BigInteger a = pp.nextBigInteger();
BigInteger low = pp.nextBigInteger();
BigInteger upp = pp.nextBigInteger();
BigInteger max = BigInteger.ZERO;
BigInteger i = low;
//((i.compareTo(upp)==-1)||
//(i.compareTo(upp)==0));
//i.add(BigInteger.ONE))
while((i.compareTo(upp))<0){
if(divCount(i).compareTo(divCount(max))==1){
max = i;
}
i.add(BigInteger.ONE);
}
System.out.println("Between "+low+" and "+upp+", "+max+" has a maximum of "+divCount(max)+" divisors.");
}
}
public static BigInteger divCount(BigInteger n){
BigInteger lim = n;
BigInteger size = BigInteger.ZERO;
BigInteger i = BigInteger.ONE;
while(i.compareTo(lim)<0){
if((n.mod(i).compareTo(BigInteger.ZERO))==0){
lim = n.divide(i);
if(!(lim.equals(i))){
size.add(BigInteger.ONE);
}
size.add(BigInteger.ONE);
}
i.add(BigInteger.ONE);
}
//return size;
return BigInteger.ONE;
}
}
i.add(BigInteger.ONE)does not modify i. Instead it returns a new object. Assign the value to i to get the effect you want. Same holds true for the other similar calls in your code.