I am creating a small game for students, and in a place, it has to display the value of 27830457+1
如果数字不是那么大,我可以调用 BigInteger pow() 方法。由于数量很大,这个方法没有什么用。我怎样才能发现这种数字的巨大力量?请帮忙!
那么在二进制中它只是10000...01
有 7830456 个零。
以十进制表示,大约有 200 万位数字,大约需要 2 兆字节的存储空间。这完全是在可行的范围之内BigInteger
具有默认的堆大小。
在实践中,它甚至使用通过平方求幂 http://en.wikipedia.org/wiki/Exponentiation_by_squaring快速计算它(尽管规范不保证)。然而转换为String
由于它是线性时间操作,因此需要一些时间。
import java.math.BigInteger;
public class BigPow {
public static void main(String[] args) {
BigInteger result = (new BigInteger("2")).pow(27830457).add(BigInteger.ONE);
System.out.println(result);
}
}
这是一个会慢慢打印出数字的版本:
import java.math.BigInteger;
public class BigPow {
public static void main(String[] args) {
BigInteger result = (new BigInteger("2")).pow(27830457).add(BigInteger.ONE);
BigInteger powten = BigInteger.TEN.pow(2357202);
while(powten.compareTo(BigInteger.TEN) > 0) {
BigInteger digit = result.divide(powten).mod(BigInteger.TEN);
System.out.print(digit);
powten = powten.divide(BigInteger.TEN);
}
}
}
第一位数字是:
2733738639062831355730724885773203300816855642973807879176160716054994495451063785500541771864696516354635136598485776179 684795037788083629143424452902991927170627198252340568713433469269134447753848945097109143746316094037162464703006474196843 640156671125528435369044827054540244464154703039922824374331519360871014872164887908559269991329974578539260930177418542736 743078283429062926585907381446668771440843602580986046292627561008735459599243600018721615295454277499150999237498553887988 089790263960045162791492304348343651441954441330639127852930365011277329750209061945916788856327407158784862308588006709196 891123629673211925293749715276954157951615065942499704196821312245056836412197647426909791063564122792292339809224240975555 411598585583101545920478039147059154328126737371655627225938668386453826392239872360221017380015140533210027591361955956357 582949836980695703152607725823630518625426905681113413513335093692429410134529433569886633956191885758422974427790118079202 918015648500008652817440087865700464572689281694358996970105315876021051217151696981334508089413466320798896218242645912857 728293494879091169132947503432465638423841323048505060766698830193266049087016724601689700783586669170539979424774621381966 2270451531049826029606671683482160663572103374
经证实沃尔夫勒姆·阿尔法 http://www.wolframalpha.com/input/?i=2%5E7830457%2b1.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)