用于将 bigInteger 转换为整数的 intValue() 方法给出了错误的结果
intValue() method for conversion of bigInteger to integer is giving wrong results
static BigInteger []fact = new BigInteger[1000003];
此数组包含从 0 到 1000003 的整数的阶乘
我将 X 作为来自模运算的 BigInteger 变量
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);
}
现在,当我尝试 "fact[X]" 时,它在 "fact[X]" 处出现此错误:
BigInteger cannot be converted to int
temp = (fact[x].multiply(fact[(x.subtract(y)).intValue()])).mod(mod);
将 X 更改为 X.intValue() 后,X 的值正在发生变化。
我怎样才能访问事实[X]?
求助!!
数组索引应该是整数。如果您使用 fact[X]
那么 X 必须是 整数 而不是 BigInteger
如果 x
和 y
是 BigInteger
,请更改您的逻辑
temp = (fact[x].multiply(fact[(x.subtract(y)).intValue()])).mod(mod);
到
temp = (fact[x.intValue()].multiply(fact[(x.subtract(y)).intValue()])).mod(mod);
您必须区分 BigInteger
和 int
或 Integer
。 BigInteger
和 int
之间没有自动转换 (autoboxing/unboxing)。此外,对象数组的维数始终为 int
。然后每次你想访问你的 BigInteger
数组中的元素时,你必须显式地将索引值转换为 int
,这是通过调用 BigInteger#intValue()
.
来完成的
我试着评论你的代码,假设 x
和 y
计算没有问题。
请注意,BigInteger
的数组在创建时仅包含 null
references。这意味着您需要先创建并设置数组元素,然后再尝试对其进行操作。
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
static BigInteger []fact = new BigInteger[1000003];
此数组包含从 0 到 1000003 的整数的阶乘
我将 X 作为来自模运算的 BigInteger 变量
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);
}
现在,当我尝试 "fact[X]" 时,它在 "fact[X]" 处出现此错误:
BigInteger cannot be converted to int
temp = (fact[x].multiply(fact[(x.subtract(y)).intValue()])).mod(mod);
将 X 更改为 X.intValue() 后,X 的值正在发生变化。
我怎样才能访问事实[X]? 求助!!
数组索引应该是整数。如果您使用 fact[X]
那么 X 必须是 整数 而不是 BigInteger
如果 x
和 y
是 BigInteger
temp = (fact[x].multiply(fact[(x.subtract(y)).intValue()])).mod(mod);
到
temp = (fact[x.intValue()].multiply(fact[(x.subtract(y)).intValue()])).mod(mod);
您必须区分 BigInteger
和 int
或 Integer
。 BigInteger
和 int
之间没有自动转换 (autoboxing/unboxing)。此外,对象数组的维数始终为 int
。然后每次你想访问你的 BigInteger
数组中的元素时,你必须显式地将索引值转换为 int
,这是通过调用 BigInteger#intValue()
.
来完成的
我试着评论你的代码,假设 x
和 y
计算没有问题。
请注意,BigInteger
的数组在创建时仅包含 null
references。这意味着您需要先创建并设置数组元素,然后再尝试对其进行操作。
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