Java – how to create a very large BigInteger

I need to add two very large integers

46376937677490009712648124896970078050417018260538
+ 37107287533902102798797998220837590246510135740250;

What's the problem?

BigInteger f = 37107287533902102798797998220837590246510135740250;

How to use BigInteger to solve this problem in Java?

Solution

The problem here is that 3710... Will be interpreted as int, so it will be out of range Basically all you have to do is create an int and convert it to BigInteger, and the first step will fail because int can't store large numbers

You need to use a constructor with string:

BigInteger f = new BigInteger("37107287533902102798797998220837590246510135740250");

Of course, the same is true for your other BigIntegers

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>