如何在java中找到像2 ^(10 ^ 9)这样的数的幂的幂 [英] How to find power of power of a number like 2^(10^9) in java

查看:547
本文介绍了如何在java中找到像2 ^(10 ^ 9)这样的数的幂的幂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Math.pow()返回一个double值,只接受int作为参数... BigInteger没有查找BigInteger的函数^ BigInteger
通过循环执行它需要很长时间...
是还有我失踪的方式吗?

Math.pow() returns a double value and takes only int as parameters...BigInteger as no function for finding BigInteger^BigInteger Doing it through loops takes really long time... Is there any more way i am missing?

Thnx提前...

Thnx in advance...

推荐答案

您可以使用 BigInteger.pow() 获取一个大指数。由于10 9 符合 int ,并且也可以完全表示为 double ,可以这样做:

You can use BigInteger.pow() to take a large exponent. Since 109 fits into an int and is also exactly representable as a double, you can do this:

int exp = (int) Math.pow(10, 9);
BigInteger answer = BigInteger.valueOf(2).pow(exp);

对于大于 Integer.MAX_VALUE 。但是,您可以使用 BigInteger.modPow(BigInteger exponent,BigInteger m) 引发 BigInteger 到另一个 BigInteger 作为电源,模块第三个 BigInteger 。您只需要首先创建一个大于预期答案的 BigInteger 作为模数。

This obviously breaks down for exponents larger than Integer.MAX_VALUE. However, you can then use BigInteger.modPow(BigInteger exponent, BigInteger m) to raise a BigInteger to another BigInteger as a power, module a third BigInteger. You just need to first create a BigInteger that is larger than your expected answer to serve as a modulus.

这篇关于如何在java中找到像2 ^(10 ^ 9)这样的数的幂的幂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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