1

I was trying this:

BigInteger.ModPow(x, -1, z);

But this method can't use -1 as Pow argument. Is there any already implemented class in C# that can do that, or I need to make my own class ?

Finchsize
  • 935
  • 2
  • 17
  • 34

1 Answers1

4

Keep your highschool math book identity in mind: x ^ -1 == 1/x. So this is simply:

  BigInteger.DivRem(BigInteger.Divide(BigInteger.One, x), z, out remainder);
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • This does not work! We wanted the modular inverse. For example with `BigInteger.ModPow(value: 7, exponent: -1, modulus: 60)` we would like the result ___43___ because 7^(-1) or 1/7 is 43 modulo 60. This is because 7*43 gives 1 modulo 60. Your answer will always fail if `x` is non-trivial, because `BigInteger.Divide(1, x)` is seen as a rational number (between 0 and 1) which will be truncated to zero. So you start by discarding all information about `x`, after which there is no hope we can succeed. – Jeppe Stig Nielsen Jul 03 '17 at 14:34