我正在尝试在 C# 中使用 2 个 BigIntegers 计算模数。
这里是有问题的方法:
static List<int> Get(BigInteger possibleColours, BigInteger length, BigInteger i)
{
List<int> ret = new List<int>();
BigInteger sizes = 1;
for (int j = 0; j < length; j++)
{
BigInteger index = (i/sizes) % possibleColours;
ret.Add((int)index);
sizes *= possibleColours;
}
return ret;
}
这一行就是问题所在:
BigInteger index = (i/sizes) % possibleColours;
除法操作似乎工作正常,但模数似乎总是将索引设置为 0,无论我以何种方式分解它,即每行执行一个操作。
如有任何帮助,我们将不胜感激!
最佳答案
您提供的以下数据样本:
possibleColours: 65536,
i: 20
您在此处增加大小
:
sizes *= possibleColours;
所以在第二次迭代中你已经有了(应用提供的示例数据):
i sizes possibleColours
BigInteger index = (20/65536) % 65536;
其中 20/65536 == 0
当您分配 integers
时,因此分配的结果也是 integer
本身。
我们得到之后:
BigInteger index = 0 % 65536;
,所以 index==0
。
这意味着在第一次迭代之后,您将始终得到 0
,这就是您的实际体验。
关于c# - BigInteger 返回 0 的模运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392245/