1 / BigInteger的在C# [英] 1/BigInteger in c#

查看:382
本文介绍了1 / BigInteger的在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要让

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.

我假设你想要的 > A 模 M ,而不是一个小数。这种逆存在当且仅当 A M 是互质数,即 GCD(A,M )= 1

I assume you want to modular multiplicative inverse of a modulo m, and not a fractional number. This inverse exists iff a and m are co-prime, i.e. gcd(a, m) = 1.

所链接的维基百科页面列出了两种标准算法计算模反元素:

The linked wikipedia page lists the two standard algorithms for calculating the modular multiplicative inverse:

  • 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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆