Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modulus not positive :BigInteger

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)
        at euler.BigConCheck1.main(BigConCheck1.java:65)
like image 760
Bharat Gaikwad Avatar asked Jan 19 '23 12:01

Bharat Gaikwad


1 Answers

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.

like image 116
Paŭlo Ebermann Avatar answered Jan 31 '23 06:01

Paŭlo Ebermann