BigInteger.pow(BigInteger)?
我正在用 Java 处理数字,想看看我能算出多大的数字。据我了解,BigInteger可以容纳无限大小的数字,只要我的计算机有足够的内存来容纳这样的数字,对吗?
我的问题是 BigInteger.pow 只接受一个 int,而不接受另一个 BigInteger,这意味着我只能使用最大 2,147,483,647 的数字作为指数。是否可以像这样使用 BigInteger 类?
BigInteger.pow(BigInteger)
谢谢。
I'm playing with numbers in Java, and want to see how big a number I can make. It is my understanding that BigInteger can hold a number of infinite size, so long as my computer has enough Memory to hold such a number, correct?
My problem is that BigInteger.pow accepts only an int, not another BigInteger, which means I can only use a number up to 2,147,483,647 as the exponent. Is it possible to use the BigInteger class as such?
BigInteger.pow(BigInteger)
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用重复平方编写自己的:
可能不适用于负基数或指数。
You can write your own, using repeated squaring:
might not work for negative bases or exponents.
您只能在Java中通过模算术来完成此操作,这意味着您可以执行a^b mod c,其中a,b ,c 是
BigInteger
数字。这是使用以下命令完成的:
在此处阅读
BigInteger.modPow
文档。You can only do this in Java by modular arithmetic, meaning you can do a a^b mod c, where a,b,c are
BigInteger
numbers.This is done using:
Read the
BigInteger.modPow
documentation here.BigInteger 的底层实现仅限于 (2^31-1) * 32 位值。几乎是 2^36 位。您将需要 8 GB 内存来存储它,并且需要很多倍的内存才能对其执行任何操作,例如 toString()。
顺便说一句:你永远无法读取这样的数字。如果你想把它打印出来,你可能会花一辈子的时间来阅读它。
The underlying implementation of BigInteger is limited to (2^31-1) * 32-bit values. which is almost 2^36 bits. You will need 8 GB of memory to store it and many times this to perform any operation on it like toString().
BTW: You will never be able to read such a number. If you tried to print it out it would take a life time to read it.
请务必阅读前面的答案和评论,并理解为什么不应在生产级别应用程序上尝试这样做。以下是仅可用于测试目的的工作解决方案:
指数大于或等于 0
这适用于正基和负基。您可能希望根据您的需要处理 0 的 0 次方,因为这在技术上是未定义的。
指数可以是正数也可以是负数
这重复使用第一种方法返回具有 2 位小数的 BigDecimal,您可以根据需要定义比例和舍入模式。
同样,您不应该在现实生活中的生产级系统中执行此操作。
Please be sure to read the previous answers and comments and understand why this should not be attempted on a production level application. The following is a working solution that can be used for testing purposes only:
Exponent greater than or equal to 0
This will work for both positive and negative bases. You might want to handle 0 to the power of 0 according to your needs, since that's technically undefined.
Exponent can be both positive or negative
This re-uses the first method to return a BigDecimal with 2 decimal places, you can define the scale and rounding mode as per your needs.
Again, you should not do this in a real-life, production-level system.
java 不会让您执行 BigInteger.Pow(BigInteger) 但您可以将其放入循环中的最大整数,然后查看在何处抛出 ArithmeticException 或由于内存不足而引发的其他错误。
java wont let you do BigInteger.Pow(BigInteger) but you can just put it to the max integer in a loop and see where a ArithmeticException is thrown or some other error due to running out of memory.
2^2,147,483,647 至少有 500000000 个数字,实际上计算 pow 是 NPC 问题,[Pow 是 NPC 输入的长度,2 个输入(m,n),它们可以在 O(logm + logn) 中编码,并且可以占用nlog(m) (最后答案需要 n log(m) 空间),这不是输入和计算大小之间的多项式关系],有一些简单的问题实际上并不容易,例如 sqrt(2) 是其中的某种,你不能指定真正的精度(所有精度),即 BigDecimal 说可以计算所有精度但它不能(事实上)因为到目前为止还没有人解决这个问题。
2^2,147,483,647 has at least 500000000 digit, in fact computing pow is NPC problem, [Pow is NPC in the length of input, 2 input (m,n) which they can be coded in O(logm + logn) and can take upto nlog(m) (at last the answer takes n log(m) space) which is not polynomial relation between input and computation size], there are some simple problems which are not easy in fact for example sqrt(2) is some kind of them, you can't specify true precision (all precisions), i.e BigDecimal says can compute all precisions but it can't (in fact) because no one solved this up to now.
我可以建议你利用
BigInteger modPow(BigInteger exponent, BigInteger m)
假设您有 BigInteger X 和 BigInteger Y,并且您想要计算 BigInteger Z = X^Y。
获取大 Prime P>>>>> X^Y 并执行 Z = X.modPow(Y,P);
I can suggest you make use of
BigInteger modPow(BigInteger exponent, BigInteger m)
Suppose you have BigInteger X, and BigInteger Y and you want to calculate BigInteger Z = X^Y.
Get a large Prime P >>>> X^Y and do Z = X.modPow(Y,P);
对于任何从 Groovy 方面偶然发现这一点的人来说,完全有可能将 BigInteger 传递给 BigInteger.pow()。
http://docs.groovy-lang.org/2.4.3/html/groovy-jdk/java/math/BigInteger.html#power%28java.math.BigInteger%29
For anyone who stumbles upon this from the Groovy side of things, it is totally possible to pass a BigInteger to BigInteger.pow().
http://docs.groovy-lang.org/2.4.3/html/groovy-jdk/java/math/BigInteger.html#power%28java.math.BigInteger%29
只需使用 .intValue()
如果您的 BigInteger 名为 BigValue2,那么它将是 BigValue2.intValue()
所以要回答您的问题,它是
Just use .intValue()
If your BigInteger is named BigValue2, then it would be BigValue2.intValue()
So to answer your question, it's