Java.math.BigInteger Series Tutorials (iv) Reasons for the birth of BigInteger
Why is there a BigInteger type in Java? Believe that many people have this question, in fact, the reason is very simple, it can express a larger range of values, far more than the maximum value of long represents a large majority. Within an integer type, long can represent the maximum value, as follows:
1234567 |
public class Test {public static void main(String[] args) {System. Out. println(Long. Max_value); }} |
The result is: 9223372036854775807
Using BigInteger, you can represent a larger value, theoretically, as long as you have enough memory, as in the following example:
12345678910 |
public class Test {public static void main(String[] args) {BigInteger a= BigInteger. ValueOf(9223372036854775807L); BigInteger b= BigInteger. ValueOf(9223372036854775807L); BigInteger C=a. Add(b); System. Out. println(C. ToString()); }} |
The result is: 18446744073709551614
Because BigInteger does not overload the "+", "-", "*", "/", "%" of these five operators, it is not possible to perform data operations directly, you need to call its corresponding method: Add,subtract,multiply,divide,remainder.
The Java version is:
Packaget0816;ImportJAva.math.BigInteger; Public Final classBign { Public StaticBigInteger Multiply (BigInteger m,intN) {BigInteger sn=NewBigInteger (integer.tostring (n)); BigInteger sqy= M.Multiply(SN);//Big Data is subtraction with add, subtract, multiply, divide,remainder. returnsqy; } Public StaticString CALCNN (intN) {if(n >= 0) {BigInteger one=NewBigInteger (integer.tostring (1));//Initialization of 1 for(intI=1;i <= n;i++) { One=Multiply(One,i);//call the above-mentioned form of the accrual function; } returnone.tostring (); } Else return NULL; } Public Static voidMain (String arg[]) {//Main functionSystem.out.println (CALCNN (13)); It is directly loaded here;}}
Output: 6227020800
hw-horrible factorial n!. __ Note the use of big data functions BigInteger