C#ModInverse功能 [英] C# ModInverse Function

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

问题描述

有一个内置的功能,可以让我计算(MOD n)的模逆?
例如19 ^ -1 = 11(模30),在这种情况下,19 ^ -1 == -11 == 19;


解决方案

由于净4.0+实现BigInteger的一个特殊的同余功能ModPow(产生 X 功率以Z ),你并不需要一个第三方库效仿ModInverse。如果 M 是素数,所有你需要做的就是计算:



<预类=郎-CS prettyprint-覆盖> a_inverse = BigInteger.ModPow(A,N - 2,N)

有关详细信息,请查看维基百科:模反元素,部分的Using欧拉定理,特殊情况下的当m是一个素数的。顺便说一句,有一个更近的SO关于这个主题: 1 / BigInteger的在C#,用同样的方法通过的 CodesInChaos


建议

Is there a built in function that would allow me to calculate the modular inverse of a(mod n)? e.g. 19^-1 = 11 (mod 30), in this case the 19^-1 == -11==19;

解决方案

Since .Net 4.0+ implements BigInteger with a special modular arithmetics function ModPow (which produces "X power Y modulo Z"), you don't need a third-party library to emulate ModInverse. If m is a prime, all you need to do is to compute:

a_inverse = BigInteger.ModPow(a, n - 2, n)

For more details, look in Wikipedia: Modular multiplicative inverse, section Using Euler's theorem, the special case "when m is a prime". By the way, there is a more recent SO topic on this: 1/BigInteger in c#, with the same approach suggested by CodesInChaos.

这篇关于C#ModInverse功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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