Reputation: 45
static BigInteger []fact = new BigInteger[1000003];
this array contains the factorial of integers from 0 to 1000003
I have X as a BigInteger variable which is coming from a modulo operation
while(m.compareTo(BigInteger.valueOf(0)) == 1 || n.compareTo(BigInteger.valueOf(0)) == 1){
if(m.compareTo(BigInteger.valueOf(0)) == 1 || m.compareTo(BigInteger.valueOf(0)) == 0)
{
x = m.mod(mod);
m = m.divide(mod);
}
Now when I'm trying "fact[X]" it is giving this error at "fact[X]":
BigInteger cannot be converted to int
temp = (fact[x].multiply(fact[(x.subtract(y)).intValue()])).mod(mod);
after changing X to X.intValue() the value of X is getting Changed.
How can i access fact[X]? help!!
Upvotes: 0
Views: 7452
Reputation: 6033
You have to make the difference between BigInteger
and int
or Integer
. There is no automatic conversion (autoboxing/unboxing) between BigInteger
and int
. Also, the dimension of an array of objects is always an int
. Then each time you want to access an element in your BigInteger
array, you have to explicitly convert the index value into an int
, this is done by calling BigInteger#intValue()
.
I tried to comment your code, assuming there is no problems in the x
and y
computing.
Note that an array of BigInteger
contains only null
references when you create it. Meaning you need to have create and set the array element before trying to do something on it.
final int max = 1000003;
BigInteger [] fact = new BigInteger[max];
BigInteger mod = BigInteger.valueOf(max);
BigInteger x = ...; // computed somewhere
BigInteger y = ...; // computed somewhere
BigInteger temp =
fact[x.intValue()] // x is a BI, take intValue() to access array element
.multiply( // operands are BI
fact[x.subtract(y) // operands are BI
.intValue()]) // take intValue() to access array
.mod(mod); // operands are BI, result is BI
Upvotes: 1
Reputation: 2614
array index should be integer. If you are using fact[X]
then X must be integer not BigInteger
change your this logic if x
and y
is BigInteger
temp = (fact[x].multiply(fact[(x.subtract(y)).intValue()])).mod(mod);
to
temp = (fact[x.intValue()].multiply(fact[(x.subtract(y)).intValue()])).mod(mod);
Upvotes: 1