error ---Modulus not positive
BigInteger is taking vaule 0, or -ve, but i can't figure out where
public int[] conCheck(BigInteger big)
{
int i=0,mul=1;
int a[]= new int[10];
int b[]= new int[10];
BigInteger rem[]= new BigInteger[11];
BigInteger num[]= new BigInteger[11];
String s="100000000";//,g="9";
//for(i=0;i<5;i++)
//s=s.concat(g);
BigInteger divi[]= new BigInteger[11];
divi[0]=new BigInteger(s);
num[0]=big;
for(i=0;i<10;i++)
{
int z = (int)Math.pow((double)10,(double)(i+1));
BigInteger zz = new BigInteger(String.valueOf(z));
divi[i+1]=divi[i].divide(zz);
num[i+1]=num[i].divide(zz);
}
{ for(i=0;i<10;i++)
{
rem[i] = num[i].mod(divi[i]);
b[i]=rem[i].intValue();
if(i>=4)
{
mul= b[i]*b[i-1]*b[i-2]*b[i-3]*b[i-4];
}
a[i]=mul;
}
}
return a;
}
Error as on Console
C:\jdk1.6.0_07\bin>java euler/BigConCheck1
Exception in thread "main" java.lang.ArithmeticException: BigInteger: modulus no
t positive
at java.math.BigInteger.mod(BigInteger.java:1506)
at euler.BigConCheck1.conCheck(BigConCheck1.java:31)
a开发者_如何学Ct euler.BigConCheck1.main(BigConCheck1.java:65)
You are dividing your big integers.
Let's see what values of divi
are calculated in your loop:
i divi[i] zz divi[i+1]
0 100000000 10 10000000
1 10000000 100 100000
2 100000 1000 100
3 100 10000 0
4 0 100000 0
5 0 1000000 0
6 0 10000000 0
7 0 100000000 0
8 0 1000000000 0
9 0 10000000000 0
And then you try to divide something by divi[4] (= 0), which will obviously fail with the exception you posted.
Take a look at the definition of the mod method.
http://download.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html#mod%28java.math.BigInteger%29
It throws an ArithmeticException in public BigInteger mod(BigInteger m)
if m <= 0.
m is 0. Thats why you are getting the exception.
It means what it says, the modulus (the number on the right hand side) must NOT be negative. It must be greater than 0
精彩评论