Java如何逆一个大整数?

时间:2022-01-02 16:47:39

I need to invert a BigInteger.

我需要逆一个大整数。

Let's say i have BigInteger x; and i need to calculate x.modPow(new BigInteger("-1"), p).

假设有BigInteger x;我需要计算x。先导入BigInteger(“1”modPow(新),p)。

I receive the following error: java.lang.ArithmeticException: BigInteger not invertible.

我收到以下错误:java.lang。先导入BigInteger不是ArithmeticException:可逆的。

2 个解决方案

#1


4  

Use BigInteger.modInverse() -- it will do what you want.

使用BigInteger.modInverse()——它将做你想做的事情。

If you read the docs for BigInteger.modInverse() (which performs the identical calculation, but more efficiently than your code; in fact presumably BigInteger.modPow() calls modInverse() for negative inputs before raising to a power), you'll see:

如果您阅读了BigInteger.modInverse()的文档(它执行相同的计算,但比您的代码更高效;事实上,据推测,BigInteger.modPow()在提升为幂之前会调用modInverse()作为负输入),您将看到:

Throws: ArithmeticException - m <= 0, or this BigInteger has no multiplicative inverse mod m (that is, this BigInteger is not relatively prime to m).

抛出:math exception - m <= 0,或者这个BigInteger没有乘法逆mod m(也就是说,这个BigInteger不是相对于m的素数)。

If you're getting "BigInteger not invertible" this means that x and p are not relatively prime, so there is no mathematically defined inverse for the pair of numbers x and p given as input.

如果你得到的是"BigInteger not可逆"这意味着x和p不是相对素数,所以x和p作为输入时没有数学定义的逆。

Possibilities:

可能性:

  • p is prime, and x is 0 or a multiple of p
  • p是质数,x是0或者是p的倍数。
  • p is not prime, and x and p have a common factor
  • p不是质数,x和p有一个公因式
  • p is not a positive integer (0 or negative), which violates the requirements of modPow() and modInverse()
  • p不是正整数(0或负),这违反了modPow()和modInverse()的要求

#2


-2  

Just put return BigInteger.ZERO. Any time you invert a number greater than one, your result is between 0 and 1. When this number is represented as an integer, it ends up being 0...

把BigInteger.ZERO返回。当一个数大于1时,结果都在0和1之间。当这个数字表示为整数时,结果是0……

#1


4  

Use BigInteger.modInverse() -- it will do what you want.

使用BigInteger.modInverse()——它将做你想做的事情。

If you read the docs for BigInteger.modInverse() (which performs the identical calculation, but more efficiently than your code; in fact presumably BigInteger.modPow() calls modInverse() for negative inputs before raising to a power), you'll see:

如果您阅读了BigInteger.modInverse()的文档(它执行相同的计算,但比您的代码更高效;事实上,据推测,BigInteger.modPow()在提升为幂之前会调用modInverse()作为负输入),您将看到:

Throws: ArithmeticException - m <= 0, or this BigInteger has no multiplicative inverse mod m (that is, this BigInteger is not relatively prime to m).

抛出:math exception - m <= 0,或者这个BigInteger没有乘法逆mod m(也就是说,这个BigInteger不是相对于m的素数)。

If you're getting "BigInteger not invertible" this means that x and p are not relatively prime, so there is no mathematically defined inverse for the pair of numbers x and p given as input.

如果你得到的是"BigInteger not可逆"这意味着x和p不是相对素数,所以x和p作为输入时没有数学定义的逆。

Possibilities:

可能性:

  • p is prime, and x is 0 or a multiple of p
  • p是质数,x是0或者是p的倍数。
  • p is not prime, and x and p have a common factor
  • p不是质数,x和p有一个公因式
  • p is not a positive integer (0 or negative), which violates the requirements of modPow() and modInverse()
  • p不是正整数(0或负),这违反了modPow()和modInverse()的要求

#2


-2  

Just put return BigInteger.ZERO. Any time you invert a number greater than one, your result is between 0 and 1. When this number is represented as an integer, it ends up being 0...

把BigInteger.ZERO返回。当一个数大于1时,结果都在0和1之间。当这个数字表示为整数时,结果是0……