如何创建一个非常大的BigInteger [英] How to create a very large BigInteger
本文介绍了如何创建一个非常大的BigInteger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要添加两个非常大的整数
46376937677490009712648124896970078050417018260538
+ 37107287533902102798797998220837590246510135740250;
这是怎么了?
BigInteger f = 37107287533902102798797998220837590246510135740250;
如何使用BigInteger
在Java中解决此问题?
解决方案
此处的问题是3710...
将被解释为int
,因此将超出范围.本质上,您要执行的操作是创建一个int
,然后将其转换为BigInteger
,而第一步将失败,因为int
不能存储那么大的数字.>
您需要使用采用String
的构造函数:
BigInteger f = new BigInteger("37107287533902102798797998220837590246510135740250");
,当然也适用于您的其他BigInteger
.
I need to add two very big integers
46376937677490009712648124896970078050417018260538
+ 37107287533902102798797998220837590246510135740250;
What's wrong with this?
BigInteger f = 37107287533902102798797998220837590246510135740250;
How I can solve this problem in Java using BigInteger
?
解决方案
The problem here is that 3710...
will be interpreted as an int
, and so it will be out of range. Essentially what you're trying to do is to create an int
, and then convert it to a BigInteger
, and the first step of this will fail because an int
can't store a number that large.
You need to use the constructor that takes a String
:
BigInteger f = new BigInteger("37107287533902102798797998220837590246510135740250");
and similarly for your other BigInteger
, of course.
这篇关于如何创建一个非常大的BigInteger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文