问题描述
我要让
BigInteger.ModPow(1/BigInteger, 2,5);
但 1 / BigInteger的
总是返回 0
,这将导致,那结果是 0
太。我试图寻找一些的BigDecimal
类C#,但我一无所获。有没有什么办法,如果没有的BigDecimal
?
but 1/BigInteger
always return 0
, which causes, that the result is 0
too. I tried to look for some BigDecimal
class for c# but I have found nothing. Is there any way how to count this even if there is no BigDecimal
?
推荐答案
1 / A
0为| A |> 1,因为 BigIntegers
使用整数除法,其中小数部分一个部门将被忽略。我不知道是什么导致你期待这一点。
1/a
is 0 for |a|>1, since BigIntegers
use integer division where the fractional part of a division is ignored. I'm not sure what result you're expecting for this.
我假设你想要的,该工程的任意模结果
它速度快,但输入相关的运行时间。
Extended Euclidean algorithm, which works for arbitrary moduli
It's fast, but has input dependent runtime.
我没有手头的C#代码,但是从维基百科移植伪代码应该是直线前进
I don't have C# code at hand, but porting the pseudo code from wikipedia should be straight forward.
使用欧拉定理:结果
结果,
这需要φ的知识(米)即你要知道m的首要因素。这是一个流行的选择,当 M
是一个素数,因此φ(M)= M-1时,它只是变得。如果你需要不断的运行,你知道φ(M),这是要走的路。
Using Euler's theorem:
This requires knowledge of φ(m) i.e. you need to know the prime factors of m. It's a popular choice when m
is a prime and thus φ(m) = m-1 when it simply becomes . If you need constant runtime and you know φ(m), this is the way to go.
在C#这成为 BigInteger.ModPow(一,phiOfM-1,M)
这篇关于1 / BigInteger的在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!