Get the App
SLTechnology News&Howtos  ›  Development  › 

Common functions and usage of BigInteger in Java

Shulou Source: shulou.com Published: 2022-06-03 15:05:25 11月13日 Update

This article mainly explains "the common functions and usage of BigInteger in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the common functions and usage of BigInteger in Java.

1. Introduction of BigInteger

If an integer data exceeds the maximum type length long of an integer at the time of operation, the data cannot be loaded, so use the BigInteger class to operate at this time. These large numbers are passed in the form of a string.

BigInteger can indeed be described as big compared to Integer. It is used for scientific calculation, Integer can only hold one int, so the maximum value is 31 visits of 2 minus 1, and the decimal system is 2147483647. However, if we need to calculate a larger number, 31 bits are obviously not enough, then BigInteger can meet our needs at this time.

The number of bits that the BigInteger can hold is large. I tried it briefly, and there is no problem with thousands of bits. In addition to the large capacity, BigInteger also encapsulates some common operations, such as the basic operation of ±* /, as well as the operation of absolute value, opposite number, maximum common divisor, prime number, and so on.

2. Common functions of BigInteger

BigInteger (String value): construction method

BigInteger add (BigInteger value): addition

BigInteger subtract (BigInteger value): subtraction

BigInteger multiply (BigInteger value): multiplication

BigInteger divide (BigInteger divisor): division

BigInteger modInverse (BigInteger m): finding the module

BigInteger pow (int exponent): multiplier

BigInteger max (BigInteger value): maximum number

BigInteger min (BigInteger value): minimum

BigInteger abs (): absolute value

BigInteger negate (): inverse number

Int intValue (): converts int and converts BigInteger type data to int.

BigInteger valueOf (long val): convert to BigInteger, convert long type to BigIntege type

Sample code

[title]

Enter an integer n 1

Tags: Common function maximum type sum element data integer input output number content method opposite number empty result absolute value learning deeper minimum Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei Docker NVidia macOS